]> sigrok.org Git - libserialport.git/commitdiff
windows: Handle the case where there are no serial ports at all.
authorMartin Ling <redacted>
Mon, 20 Jan 2020 01:42:00 +0000 (01:42 +0000)
committerMartin Ling <redacted>
Thu, 23 Jan 2020 03:35:47 +0000 (03:35 +0000)
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.

windows.c

index a9d09279fdffa57228dae907955b6d7cf4be83e1..53c1b02333c5550a19d593d18fe3e1c4f296bafe 100644 (file)
--- a/windows.c
+++ b/windows.c
@@ -491,8 +491,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) {
        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");
                goto out_done;
        }
        DEBUG("Querying registry key value and data sizes");