mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-10 10:08:18 +00:00
Use std::string
for windows_format_system_message
To make the function easier to use.
This commit is contained in:
parent
f24d755bb2
commit
bc7c347ad4
|
@ -1461,18 +1461,14 @@ static int priv_net_close_all_sockets(NETSOCKET sock)
|
|||
}
|
||||
|
||||
#if defined(CONF_FAMILY_WINDOWS)
|
||||
char *windows_format_system_message(unsigned long error)
|
||||
std::string windows_format_system_message(unsigned long error)
|
||||
{
|
||||
WCHAR *wide_message;
|
||||
const DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_MAX_WIDTH_MASK;
|
||||
if(FormatMessageW(flags, NULL, error, 0, (LPWSTR)&wide_message, 0, NULL) == 0)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
int len = WideCharToMultiByte(CP_UTF8, 0, wide_message, -1, NULL, 0, NULL, NULL);
|
||||
dbg_assert(len > 0, "WideCharToMultiByte failure");
|
||||
char *message = (char *)malloc(len * sizeof(*message));
|
||||
dbg_assert(WideCharToMultiByte(CP_UTF8, 0, wide_message, -1, message, len, NULL, NULL) == len, "WideCharToMultiByte failure");
|
||||
return "unknown error";
|
||||
|
||||
const std::string message = windows_wide_to_utf8(wide_message);
|
||||
LocalFree(wide_message);
|
||||
return message;
|
||||
}
|
||||
|
@ -1488,9 +1484,8 @@ static int priv_net_create_socket(int domain, int type, struct sockaddr *addr, i
|
|||
{
|
||||
#if defined(CONF_FAMILY_WINDOWS)
|
||||
int error = WSAGetLastError();
|
||||
char *message = windows_format_system_message(error);
|
||||
dbg_msg("net", "failed to create socket with domain %d and type %d (%d '%s')", domain, type, error, message == nullptr ? "unknown error" : message);
|
||||
free(message);
|
||||
const std::string message = windows_format_system_message(error);
|
||||
dbg_msg("net", "failed to create socket with domain %d and type %d (%d '%s')", domain, type, error, message.c_str());
|
||||
#else
|
||||
dbg_msg("net", "failed to create socket with domain %d and type %d (%d '%s')", domain, type, errno, strerror(errno));
|
||||
#endif
|
||||
|
@ -1524,9 +1519,8 @@ static int priv_net_create_socket(int domain, int type, struct sockaddr *addr, i
|
|||
{
|
||||
#if defined(CONF_FAMILY_WINDOWS)
|
||||
int error = WSAGetLastError();
|
||||
char *message = windows_format_system_message(error);
|
||||
dbg_msg("net", "failed to bind socket with domain %d and type %d (%d '%s')", domain, type, error, message == nullptr ? "unknown error" : message);
|
||||
free(message);
|
||||
const std::string message = windows_format_system_message(error);
|
||||
dbg_msg("net", "failed to bind socket with domain %d and type %d (%d '%s')", domain, type, error, message.c_str());
|
||||
#else
|
||||
dbg_msg("net", "failed to bind socket with domain %d and type %d (%d '%s')", domain, type, errno, strerror(errno));
|
||||
#endif
|
||||
|
@ -4412,9 +4406,8 @@ CWindowsComLifecycle::~CWindowsComLifecycle()
|
|||
|
||||
static void windows_print_error(const char *system, const char *prefix, HRESULT error)
|
||||
{
|
||||
char *message = windows_format_system_message(error);
|
||||
dbg_msg(system, "%s: %s", prefix, message == nullptr ? "unknown error" : message);
|
||||
free(message);
|
||||
const std::string message = windows_format_system_message(error);
|
||||
dbg_msg(system, "%s: %s", prefix, message.c_str());
|
||||
}
|
||||
|
||||
static std::wstring filename_from_path(const std::wstring &path)
|
||||
|
|
|
@ -1163,12 +1163,9 @@ void net_unix_close(UNIXSOCKET sock);
|
|||
*
|
||||
* @param error The Windows error code.
|
||||
*
|
||||
* @return A new string representing the error code.
|
||||
*
|
||||
* @remark Guarantees that result will contain zero-termination.
|
||||
* @remark The result must be freed after it has been used.
|
||||
* @return A new std::string representing the error code.
|
||||
*/
|
||||
char *windows_format_system_message(unsigned long error);
|
||||
std::string windows_format_system_message(unsigned long error);
|
||||
|
||||
#endif
|
||||
|
||||
|
|
|
@ -111,9 +111,8 @@ void CFifo::Init(IConsole *pConsole, char *pFifoFile, int Flag)
|
|||
if(m_pPipe == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
const DWORD LastError = GetLastError();
|
||||
char *pErrorMsg = windows_format_system_message(LastError);
|
||||
dbg_msg("fifo", "failed to create named pipe '%s' (%ld %s)", m_aFilename, LastError, pErrorMsg);
|
||||
free(pErrorMsg);
|
||||
const std::string ErrorMsg = windows_format_system_message(LastError);
|
||||
dbg_msg("fifo", "failed to create named pipe '%s' (%ld %s)", m_aFilename, LastError, ErrorMsg.c_str());
|
||||
}
|
||||
else
|
||||
dbg_msg("fifo", "created named pipe '%s'", m_aFilename);
|
||||
|
@ -147,9 +146,8 @@ void CFifo::Update()
|
|||
}
|
||||
if(LastError != ERROR_PIPE_CONNECTED) // pipe already connected, not an error
|
||||
{
|
||||
char *pErrorMsg = windows_format_system_message(LastError);
|
||||
dbg_msg("fifo", "failed to connect named pipe '%s' (%ld %s)", m_aFilename, LastError, pErrorMsg);
|
||||
free(pErrorMsg);
|
||||
const std::string ErrorMsg = windows_format_system_message(LastError);
|
||||
dbg_msg("fifo", "failed to connect named pipe '%s' (%ld %s)", m_aFilename, LastError, ErrorMsg.c_str());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -162,9 +160,8 @@ void CFifo::Update()
|
|||
const DWORD LastError = GetLastError();
|
||||
if(LastError != ERROR_BAD_PIPE) // pipe not connected, not an error
|
||||
{
|
||||
char *pErrorMsg = windows_format_system_message(LastError);
|
||||
dbg_msg("fifo", "failed to peek at pipe '%s' (%ld %s)", m_aFilename, LastError, pErrorMsg);
|
||||
free(pErrorMsg);
|
||||
const std::string ErrorMsg = windows_format_system_message(LastError);
|
||||
dbg_msg("fifo", "failed to peek at pipe '%s' (%ld %s)", m_aFilename, LastError, ErrorMsg.c_str());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
@ -176,9 +173,8 @@ void CFifo::Update()
|
|||
if(!ReadFile(m_pPipe, pBuf, BytesAvailable, &Length, NULL))
|
||||
{
|
||||
const DWORD LastError = GetLastError();
|
||||
char *pErrorMsg = windows_format_system_message(LastError);
|
||||
dbg_msg("fifo", "failed to read from pipe '%s' (%ld %s)", m_aFilename, LastError, pErrorMsg);
|
||||
free(pErrorMsg);
|
||||
const std::string ErrorMsg = windows_format_system_message(LastError);
|
||||
dbg_msg("fifo", "failed to read from pipe '%s' (%ld %s)", m_aFilename, LastError, ErrorMsg.c_str());
|
||||
free(pBuf);
|
||||
return;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue