1
0
mirror of git://sigrok.org/libserialport synced 2023-08-10 21:13:24 +03:00

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.
This commit is contained in:
Martin Ling 2020-01-24 04:00:13 +00:00
parent 2149db9e93
commit e47c7dcbff
2 changed files with 4 additions and 4 deletions

View File

@ -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);

View File

@ -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);