X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=windows.c;h=bc07e234c341ebf8cdec4d34aa1344ae2fe4429a;hb=e47c7dcbffa04583d1e94d9c359784fc14a59d88;hp=a9d09279fdffa57228dae907955b6d7cf4be83e1;hpb=c79e0ac8ef8e978de0a5bc15af0b555ba59d7da0;p=libserialport.git
diff --git a/windows.c b/windows.c
index a9d0927..bc07e23 100644
--- a/windows.c
+++ b/windows.c
@@ -18,8 +18,6 @@
* along with this program. If not, see .
*/
-#include
-#include "libserialport.h"
#include "libserialport_internal.h"
/* USB path is a string of at most 8 decimal numbers < 128 separated by dots. */
@@ -28,32 +26,42 @@
static void enumerate_hub(struct sp_port *port, const char *hub_name,
const char *parent_path, DEVINST dev_inst);
-static char *wc_to_utf8(PWCHAR wc_buffer, ULONG size)
+static char *wc_to_utf8(PWCHAR wc_buffer, ULONG wc_bytes)
{
- ULONG wc_length = size / sizeof(WCHAR);
- WCHAR wc_str[wc_length + 1];
- char *utf8_str;
+ ULONG wc_length = wc_bytes / sizeof(WCHAR);
+ ULONG utf8_bytes;
+ WCHAR *wc_str = NULL;
+ char *utf8_str = NULL;
+
+ /* Allocate aligned wide char buffer */
+ if (!(wc_str = malloc((wc_length + 1) * sizeof(WCHAR))))
+ goto wc_to_utf8_end;
/* Zero-terminate the wide char string. */
- memcpy(wc_str, wc_buffer, size);
+ memcpy(wc_str, wc_buffer, wc_bytes);
wc_str[wc_length] = 0;
/* Compute the size of the UTF-8 converted string. */
- if (!(size = WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, wc_str, -1,
+ if (!(utf8_bytes = WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, wc_str, -1,
NULL, 0, NULL, NULL)))
- return NULL;
+ goto wc_to_utf8_end;
/* Allocate UTF-8 output buffer. */
- if (!(utf8_str = malloc(size)))
- return NULL;
+ if (!(utf8_str = malloc(utf8_bytes)))
+ goto wc_to_utf8_end;
/* Actually converted to UTF-8. */
if (!WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, wc_str, -1,
- utf8_str, size, NULL, NULL)) {
+ utf8_str, utf8_bytes, NULL, NULL)) {
free(utf8_str);
- return NULL;
+ utf8_str = NULL;
+ goto wc_to_utf8_end;
}
+wc_to_utf8_end:
+ if (wc_str)
+ free(wc_str);
+
return utf8_str;
}
@@ -459,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);
@@ -491,8 +499,12 @@ SP_PRIV enum sp_return list_ports(struct sp_port ***list)
DEBUG("Opening registry key");
if ((result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"),
0, KEY_QUERY_VALUE, &key)) != ERROR_SUCCESS) {
- SetLastError(result);
- SET_FAIL(ret, "RegOpenKeyEx() failed");
+ /* It's possible for this key to not exist if there are no serial ports
+ * at all. In that case we're done. Return a failure for any other error. */
+ if (result != ERROR_FILE_NOT_FOUND) {
+ SetLastError(result);
+ SET_FAIL(ret, "RegOpenKeyEx() failed");
+ }
goto out_done;
}
DEBUG("Querying registry key value and data sizes");