From: Martin Ling Date: Fri, 24 Jan 2020 04:30:22 +0000 (+0000) Subject: windows: Use correct variant of FormatMessage. X-Git-Tag: libserialport-0.1.2~42 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=bf40b1cea9eb2e13d13f05118fe11b43be621485;p=libserialport.git windows: Use correct variant of FormatMessage. When built with MSVC and unicode enabled, using 'message' gave: warning C4133: 'initializing': incompatible types - from 'TCHAR *' to 'char *' FormatMessage expands to either FormatMessageA or FormatMessageW depending if unicode is enabled, and generates either a char (8-bit) or WCHAR (UTF-16) string accordingly. Since sp_last_error_message() returns char *, we must use the 8-bit variant. The message will be encoded in the current code page. --- diff --git a/serialport.c b/serialport.c index 0a46688..eee453a 100644 --- a/serialport.c +++ b/serialport.c @@ -2478,17 +2478,17 @@ SP_API char *sp_last_error_message(void) TRACE_VOID(); #ifdef _WIN32 - TCHAR *message; + char *message; DWORD error = GetLastError(); - DWORD length = FormatMessage( + DWORD length = FormatMessageA( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPTSTR) &message, + (LPSTR) &message, 0, NULL ); if (length >= 2 && message[length - 2] == '\r')