From: Martin Ling Date: Fri, 24 Jan 2020 04:00:13 +0000 (+0000) Subject: windows: Use correct variant of CreateFile. X-Git-Url: https://sigrok.org/gitweb/?p=libserialport.git;a=commitdiff_plain;h=e47c7dcbffa04583d1e94d9c359784fc14a59d88 windows: Use correct variant of CreateFile. When built with MSVC and unicode enabled, using CreateFile gave: warning C4133: 'function': incompatible types - from 'char *' to 'LPCWSTR' CreateFile is a macro expanding to either CreateFileW if unicode mode is enabled, or CreateFileA if not. For CreateFileW, the filename is a UTF-16 string. For CreateFileA it is an 'ANSI' string, meaning 8-bit chars in the current Windows code page. We do need to stick to 8-bit strings for port names, since sp_get_port_by_name() and sp_get_port_name() are defined with char * types, and that is what we store in struct sp_port. So CreateFileA is the correct version to use. Since Windows serial port names are always just 'COM' and a digit, with a '\\.\' prefix for higher numbers, encoding is fortunately not an issue - ASCII, UTF-8 and all the Windows code pages seem to be equivalent for these characters. We should however explicitly document what the encoding of strings accepted and returned by libserialport is. --- diff --git a/serialport.c b/serialport.c index b2e4a79..0a46688 100644 --- a/serialport.c +++ b/serialport.c @@ -488,7 +488,7 @@ SP_API enum sp_return sp_open(struct sp_port *port, enum sp_mode flags) if (flags & SP_MODE_WRITE) desired_access |= GENERIC_WRITE; - port->hdl = CreateFile(escaped_port_name, desired_access, 0, 0, + port->hdl = CreateFileA(escaped_port_name, desired_access, 0, 0, OPEN_EXISTING, flags_and_attributes, 0); free(escaped_port_name); diff --git a/windows.c b/windows.c index 0521716..bc07e23 100644 --- a/windows.c +++ b/windows.c @@ -467,9 +467,9 @@ SP_PRIV enum sp_return get_port_details(struct sp_port *port) if (!(escaped_port_name = malloc(strlen(port->name) + 5))) RETURN_ERROR(SP_ERR_MEM, "Escaped port name malloc failed"); sprintf(escaped_port_name, "\\\\.\\%s", port->name); - handle = CreateFile(escaped_port_name, GENERIC_READ, 0, 0, - OPEN_EXISTING, - FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED, 0); + handle = CreateFileA(escaped_port_name, GENERIC_READ, 0, 0, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED, 0); free(escaped_port_name); CloseHandle(handle);