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.
It's possible for the HARDWARE\DEVICEMAP\SERIALCOMM key to not exist in
the registry if there are no serial ports at all and never have been, as
discovered on my rather minimalist gaming machine.
Handle that case gracefully and return an empty list.
RegOpenKeyEx() and RegQueryInfoKey() return system error codes directly,
not by setting the thread-local errno equivalent that is returned by
GetLastError().
When returning SP_ERR_FAIL, our API specifies that sp_last_error_code()
may be called immediately afterwards to get the system error code. In
this case that would not work, as it would call GetLastError() and miss
the directly-returned result.
We therefore need to call SetLastError() with the error code before
returning with SP_ERR_FAIL.
In wc_to_utf8() in windows.c, the zero terminator is written to an invalid
array index, which results in 2 bytes being zeroed in a random place in the
stack. This sometimes causes a crash when running sp_list_ports() (depending
on string length and compiler optimisation settings).
sizeof(wc_str) returns the size in bytes, so cannot be used directly as an
index into that array, it should be divided by sizeof(WCHAR). Otherwise the
zero terminator index is approximately twice what it should be.
This fixes bug #1031.
USB composite devices can contain an ACM serial interface.
On Windows, the correct iSerial descriptor field is assigned to the parent
(composite) device instead of to the actual serial interface. A bogus
value is returned if the serial interface is asked to provide the S/N.
This patch provides a fallback for this kind of device (tested on Android
with adb + cdc gadgets and on Arduino Zero Programming Port)