diff options
author | Danilo Cesar Lemes de Paula <danilo.eu@gmail.com> | 2013-09-16 18:04:42 -0300 |
---|---|---|
committer | Danilo Cesar Lemes de Paula <danilo.eu@gmail.com> | 2013-09-16 22:40:47 -0300 |
commit | a8d33f80b0e8da4acb84028be0da8f33b81de0f9 (patch) | |
tree | d3813d817fd8407e79f58d68cffb393d8741db3d /windows.c | |
parent | f5b33dede359dccc31fafa72e0e2550868d8edd2 (diff) | |
download | subsurface-a8d33f80b0e8da4acb84028be0da8f33b81de0f9.tar.gz |
implement device probe in C
It's an attempt to build auto-completion for the dive-computers
based on unpublished code inside libdivecomputer[1]
[1] -
http://git.libdivecomputer.org/?p=libdivecomputer.git;a=commitdiff;h=d44053a99435fb9fc1f408fb3f1629a54c938afc
Signed-off-by: Danilo Cesar Lemes de Paula <danilo.eu@gmail.com>
Diffstat (limited to 'windows.c')
-rw-r--r-- | windows.c | 53 |
1 files changed, 53 insertions, 0 deletions
@@ -333,3 +333,56 @@ gboolean subsurface_os_feature_available(os_feature_t f) return TRUE; } } + +int enumerate_devices (device_callback_t callback, void *userdata) +{ + // Open the registry key. + HKEY hKey; + int index = -1; + LONG rc = RegOpenKeyEx (HKEY_LOCAL_MACHINE, "HARDWARE\\DEVICEMAP\\SERIALCOMM", 0, KEY_QUERY_VALUE, &hKey); + if (rc != ERROR_SUCCESS) { + return -1; + } + + // Get the number of values. + DWORD count = 0; + rc = RegQueryInfoKey (hKey, NULL, NULL, NULL, NULL, NULL, NULL, &count, NULL, NULL, NULL, NULL); + if (rc != ERROR_SUCCESS) { + RegCloseKey(hKey); + return -1; + } + + for (DWORD i = 0; i < count; ++i) { + // Get the value name, data and type. + char name[512], data[512]; + DWORD name_len = sizeof (name); + DWORD data_len = sizeof (data); + DWORD type = 0; + rc = RegEnumValue (hKey, i, name, &name_len, NULL, &type, (LPBYTE) data, &data_len); + if (rc != ERROR_SUCCESS) { + RegCloseKey(hKey); + return -1; + } + + // Ignore non-string values. + if (type != REG_SZ) + continue; + + // Prevent a possible buffer overflow. + if (data_len >= sizeof (data)) { + RegCloseKey(hKey); + return -1; + } + + // Null terminate the string. + data[data_len] = 0; + + callback (data, userdata); + index++; + if (is_default_dive_computer_device(filename)) + index = i; + } + + RegCloseKey(hKey); + return index; +} |