]> sigrok.org Git - libserialport.git/blame - windows.c
change type of result variables to ssize_t
[libserialport.git] / windows.c
CommitLineData
e33dcf90
ML
1/*
2 * This file is part of the libserialport project.
3 *
48a4076f 4 * Copyright (C) 2013-2014 Martin Ling <martin-libserialport@earth.li>
e33dcf90
ML
5 * Copyright (C) 2014 Aurelien Jacobs <aurel@gnuage.org>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
e33dcf90
ML
21#include "libserialport_internal.h"
22
dc422c04
UH
23/* USB path is a string of at most 8 decimal numbers < 128 separated by dots. */
24#define MAX_USB_PATH ((8 * 3) + (7 * 1) + 1)
e33dcf90 25
eb50b1ac 26static void enumerate_hub(struct sp_port *port, const char *hub_name,
02c8a142 27 const char *parent_path, DEVINST dev_inst);
e33dcf90 28
6aaf8448 29static char *wc_to_utf8(PWCHAR wc_buffer, ULONG wc_bytes)
e33dcf90 30{
6aaf8448
ML
31 ULONG wc_length = wc_bytes / sizeof(WCHAR);
32 ULONG utf8_bytes;
a9900f8b
ML
33 WCHAR *wc_str = NULL;
34 char *utf8_str = NULL;
35
36 /* Allocate aligned wide char buffer */
6aaf8448 37 if (!(wc_str = malloc((wc_length + 1) * sizeof(WCHAR))))
a9900f8b 38 goto wc_to_utf8_end;
e33dcf90 39
dc422c04 40 /* Zero-terminate the wide char string. */
6aaf8448 41 memcpy(wc_str, wc_buffer, wc_bytes);
38b71192 42 wc_str[wc_length] = 0;
e33dcf90 43
dc422c04 44 /* Compute the size of the UTF-8 converted string. */
6aaf8448 45 if (!(utf8_bytes = WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, wc_str, -1,
e33dcf90 46 NULL, 0, NULL, NULL)))
a9900f8b 47 goto wc_to_utf8_end;
e33dcf90 48
dc422c04 49 /* Allocate UTF-8 output buffer. */
6aaf8448 50 if (!(utf8_str = malloc(utf8_bytes)))
a9900f8b 51 goto wc_to_utf8_end;
e33dcf90 52
dc422c04 53 /* Actually converted to UTF-8. */
e33dcf90 54 if (!WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, wc_str, -1,
6aaf8448 55 utf8_str, utf8_bytes, NULL, NULL)) {
e33dcf90 56 free(utf8_str);
a9900f8b
ML
57 utf8_str = NULL;
58 goto wc_to_utf8_end;
e33dcf90
ML
59 }
60
a9900f8b
ML
61wc_to_utf8_end:
62 if (wc_str)
63 free(wc_str);
64
e33dcf90
ML
65 return utf8_str;
66}
67
68static char *get_root_hub_name(HANDLE host_controller)
69{
dc422c04 70 USB_ROOT_HUB_NAME root_hub_name;
e33dcf90
ML
71 PUSB_ROOT_HUB_NAME root_hub_name_wc;
72 char *root_hub_name_utf8;
73 ULONG size = 0;
74
dc422c04 75 /* Compute the size of the root hub name string. */
e33dcf90
ML
76 if (!DeviceIoControl(host_controller, IOCTL_USB_GET_ROOT_HUB_NAME, 0, 0,
77 &root_hub_name, sizeof(root_hub_name), &size, NULL))
78 return NULL;
79
dc422c04 80 /* Allocate wide char root hub name string. */
e33dcf90
ML
81 size = root_hub_name.ActualLength;
82 if (!(root_hub_name_wc = malloc(size)))
83 return NULL;
84
dc422c04 85 /* Actually get the root hub name string. */
e33dcf90
ML
86 if (!DeviceIoControl(host_controller, IOCTL_USB_GET_ROOT_HUB_NAME,
87 NULL, 0, root_hub_name_wc, size, &size, NULL)) {
88 free(root_hub_name_wc);
89 return NULL;
90 }
91
dc422c04 92 /* Convert the root hub name string to UTF-8. */
2e0437c2 93 root_hub_name_utf8 = wc_to_utf8(root_hub_name_wc->RootHubName, size - offsetof(USB_ROOT_HUB_NAME, RootHubName));
e33dcf90
ML
94 free(root_hub_name_wc);
95 return root_hub_name_utf8;
96}
97
98static char *get_external_hub_name(HANDLE hub, ULONG connection_index)
99{
dc422c04 100 USB_NODE_CONNECTION_NAME ext_hub_name;
e33dcf90
ML
101 PUSB_NODE_CONNECTION_NAME ext_hub_name_wc;
102 char *ext_hub_name_utf8;
103 ULONG size;
104
dc422c04 105 /* Compute the size of the external hub name string. */
e33dcf90
ML
106 ext_hub_name.ConnectionIndex = connection_index;
107 if (!DeviceIoControl(hub, IOCTL_USB_GET_NODE_CONNECTION_NAME,
108 &ext_hub_name, sizeof(ext_hub_name),
109 &ext_hub_name, sizeof(ext_hub_name), &size, NULL))
110 return NULL;
111
dc422c04 112 /* Allocate wide char external hub name string. */
e33dcf90
ML
113 size = ext_hub_name.ActualLength;
114 if (size <= sizeof(ext_hub_name)
115 || !(ext_hub_name_wc = malloc(size)))
116 return NULL;
117
dc422c04 118 /* Get the name of the external hub attached to the specified port. */
e33dcf90
ML
119 ext_hub_name_wc->ConnectionIndex = connection_index;
120 if (!DeviceIoControl(hub, IOCTL_USB_GET_NODE_CONNECTION_NAME,
121 ext_hub_name_wc, size,
122 ext_hub_name_wc, size, &size, NULL)) {
123 free(ext_hub_name_wc);
124 return NULL;
125 }
126
dc422c04 127 /* Convert the external hub name string to UTF-8. */
2e0437c2 128 ext_hub_name_utf8 = wc_to_utf8(ext_hub_name_wc->NodeName, size - offsetof(USB_NODE_CONNECTION_NAME, NodeName));
e33dcf90
ML
129 free(ext_hub_name_wc);
130 return ext_hub_name_utf8;
131}
132
133static char *get_string_descriptor(HANDLE hub_device, ULONG connection_index,
134 UCHAR descriptor_index)
135{
136 char desc_req_buf[sizeof(USB_DESCRIPTOR_REQUEST) +
137 MAXIMUM_USB_STRING_LENGTH] = { 0 };
dc422c04
UH
138 PUSB_DESCRIPTOR_REQUEST desc_req = (void *)desc_req_buf;
139 PUSB_STRING_DESCRIPTOR desc = (void *)(desc_req + 1);
e33dcf90
ML
140 ULONG size = sizeof(desc_req_buf);
141
dc422c04
UH
142 desc_req->ConnectionIndex = connection_index;
143 desc_req->SetupPacket.wValue = (USB_STRING_DESCRIPTOR_TYPE << 8)
144 | descriptor_index;
145 desc_req->SetupPacket.wIndex = 0;
60fc49ce 146 desc_req->SetupPacket.wLength = (USHORT) (size - sizeof(*desc_req));
e33dcf90
ML
147
148 if (!DeviceIoControl(hub_device,
149 IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION,
150 desc_req, size, desc_req, size, &size, NULL)
151 || size < 2
152 || desc->bDescriptorType != USB_STRING_DESCRIPTOR_TYPE
153 || desc->bLength != size - sizeof(*desc_req)
154 || desc->bLength % 2)
155 return NULL;
156
2e0437c2 157 return wc_to_utf8(desc->bString, desc->bLength - offsetof(USB_STRING_DESCRIPTOR, bString));
e33dcf90
ML
158}
159
160static void enumerate_hub_ports(struct sp_port *port, HANDLE hub_device,
02c8a142 161 ULONG nb_ports, const char *parent_path, DEVINST dev_inst)
e33dcf90
ML
162{
163 char path[MAX_USB_PATH];
164 ULONG index = 0;
165
166 for (index = 1; index <= nb_ports; index++) {
167 PUSB_NODE_CONNECTION_INFORMATION_EX connection_info_ex;
dc422c04 168 ULONG size = sizeof(*connection_info_ex) + (30 * sizeof(USB_PIPE_INFO));
e33dcf90
ML
169
170 if (!(connection_info_ex = malloc(size)))
171 break;
172
173 connection_info_ex->ConnectionIndex = index;
174 if (!DeviceIoControl(hub_device,
175 IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX,
176 connection_info_ex, size,
177 connection_info_ex, size, &size, NULL)) {
dc422c04
UH
178 /*
179 * Try to get CONNECTION_INFORMATION if
180 * CONNECTION_INFORMATION_EX did not work.
181 */
e33dcf90
ML
182 PUSB_NODE_CONNECTION_INFORMATION connection_info;
183
dc422c04 184 size = sizeof(*connection_info) + (30 * sizeof(USB_PIPE_INFO));
e33dcf90
ML
185 if (!(connection_info = malloc(size))) {
186 free(connection_info_ex);
187 continue;
188 }
189 connection_info->ConnectionIndex = index;
190 if (!DeviceIoControl(hub_device,
191 IOCTL_USB_GET_NODE_CONNECTION_INFORMATION,
192 connection_info, size,
193 connection_info, size, &size, NULL)) {
194 free(connection_info);
195 free(connection_info_ex);
196 continue;
197 }
198
199 connection_info_ex->ConnectionIndex = connection_info->ConnectionIndex;
200 connection_info_ex->DeviceDescriptor = connection_info->DeviceDescriptor;
201 connection_info_ex->DeviceIsHub = connection_info->DeviceIsHub;
202 connection_info_ex->DeviceAddress = connection_info->DeviceAddress;
203 free(connection_info);
204 }
205
206 if (connection_info_ex->DeviceIsHub) {
dc422c04 207 /* Recursively enumerate external hub. */
e33dcf90
ML
208 PCHAR ext_hub_name;
209 if ((ext_hub_name = get_external_hub_name(hub_device, index))) {
210 snprintf(path, sizeof(path), "%s%ld.",
211 parent_path, connection_info_ex->ConnectionIndex);
02c8a142 212 enumerate_hub(port, ext_hub_name, path, dev_inst);
e33dcf90
ML
213 }
214 free(connection_info_ex);
215 } else {
216 snprintf(path, sizeof(path), "%s%ld",
217 parent_path, connection_info_ex->ConnectionIndex);
218
dc422c04 219 /* Check if this device is the one we search for. */
e33dcf90
ML
220 if (strcmp(path, port->usb_path)) {
221 free(connection_info_ex);
222 continue;
223 }
224
dc422c04 225 /* Finally grab detailed information regarding the device. */
e33dcf90
ML
226 port->usb_address = connection_info_ex->DeviceAddress + 1;
227 port->usb_vid = connection_info_ex->DeviceDescriptor.idVendor;
228 port->usb_pid = connection_info_ex->DeviceDescriptor.idProduct;
229
230 if (connection_info_ex->DeviceDescriptor.iManufacturer)
81243567 231 port->usb_manufacturer = get_string_descriptor(hub_device, index,
e33dcf90
ML
232 connection_info_ex->DeviceDescriptor.iManufacturer);
233 if (connection_info_ex->DeviceDescriptor.iProduct)
234 port->usb_product = get_string_descriptor(hub_device, index,
235 connection_info_ex->DeviceDescriptor.iProduct);
02c8a142 236 if (connection_info_ex->DeviceDescriptor.iSerialNumber) {
e33dcf90
ML
237 port->usb_serial = get_string_descriptor(hub_device, index,
238 connection_info_ex->DeviceDescriptor.iSerialNumber);
02c8a142
MF
239 if (port->usb_serial == NULL) {
240 //composite device, get the parent's serial number
241 char device_id[MAX_DEVICE_ID_LEN];
242 if (CM_Get_Parent(&dev_inst, dev_inst, 0) == CR_SUCCESS) {
243 if (CM_Get_Device_IDA(dev_inst, device_id, sizeof(device_id), 0) == CR_SUCCESS)
244 port->usb_serial = strdup(strrchr(device_id, '\\')+1);
245 }
246 }
247 }
e33dcf90
ML
248
249 free(connection_info_ex);
250 break;
251 }
252 }
253}
254
eb50b1ac 255static void enumerate_hub(struct sp_port *port, const char *hub_name,
02c8a142 256 const char *parent_path, DEVINST dev_inst)
e33dcf90
ML
257{
258 USB_NODE_INFORMATION hub_info;
259 HANDLE hub_device;
260 ULONG size = sizeof(hub_info);
261 char *device_name;
262
dc422c04 263 /* Open the hub with its full name. */
e33dcf90
ML
264 if (!(device_name = malloc(strlen("\\\\.\\") + strlen(hub_name) + 1)))
265 return;
266 strcpy(device_name, "\\\\.\\");
267 strcat(device_name, hub_name);
d6412d28
ML
268 hub_device = CreateFileA(device_name, GENERIC_WRITE, FILE_SHARE_WRITE,
269 NULL, OPEN_EXISTING, 0, NULL);
e33dcf90
ML
270 free(device_name);
271 if (hub_device == INVALID_HANDLE_VALUE)
272 return;
273
dc422c04 274 /* Get the number of ports of the hub. */
e33dcf90
ML
275 if (DeviceIoControl(hub_device, IOCTL_USB_GET_NODE_INFORMATION,
276 &hub_info, size, &hub_info, size, &size, NULL))
dc422c04 277 /* Enumerate the ports of the hub. */
e33dcf90 278 enumerate_hub_ports(port, hub_device,
02c8a142 279 hub_info.u.HubInformation.HubDescriptor.bNumberOfPorts, parent_path, dev_inst);
e33dcf90
ML
280
281 CloseHandle(hub_device);
282}
283
284static void enumerate_host_controller(struct sp_port *port,
02c8a142
MF
285 HANDLE host_controller_device,
286 DEVINST dev_inst)
e33dcf90
ML
287{
288 char *root_hub_name;
289
290 if ((root_hub_name = get_root_hub_name(host_controller_device))) {
02c8a142 291 enumerate_hub(port, root_hub_name, "", dev_inst);
e33dcf90
ML
292 free(root_hub_name);
293 }
294}
295
296static void get_usb_details(struct sp_port *port, DEVINST dev_inst_match)
297{
298 HDEVINFO device_info;
299 SP_DEVINFO_DATA device_info_data;
300 ULONG i, size = 0;
301
dc422c04 302 device_info = SetupDiGetClassDevs(&GUID_CLASS_USB_HOST_CONTROLLER, NULL, NULL,
e33dcf90
ML
303 DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
304 device_info_data.cbSize = sizeof(device_info_data);
305
dc422c04 306 for (i = 0; SetupDiEnumDeviceInfo(device_info, i, &device_info_data); i++) {
e33dcf90
ML
307 SP_DEVICE_INTERFACE_DATA device_interface_data;
308 PSP_DEVICE_INTERFACE_DETAIL_DATA device_detail_data;
309 DEVINST dev_inst = dev_inst_match;
310 HANDLE host_controller_device;
311
312 device_interface_data.cbSize = sizeof(device_interface_data);
313 if (!SetupDiEnumDeviceInterfaces(device_info, 0,
314 &GUID_CLASS_USB_HOST_CONTROLLER,
315 i, &device_interface_data))
316 continue;
317
318 if (!SetupDiGetDeviceInterfaceDetail(device_info,&device_interface_data,
319 NULL, 0, &size, NULL)
320 && GetLastError() != ERROR_INSUFFICIENT_BUFFER)
321 continue;
322
323 if (!(device_detail_data = malloc(size)))
324 continue;
325 device_detail_data->cbSize = sizeof(*device_detail_data);
326 if (!SetupDiGetDeviceInterfaceDetail(device_info,&device_interface_data,
327 device_detail_data, size, &size,
328 NULL)) {
329 free(device_detail_data);
330 continue;
331 }
332
333 while (CM_Get_Parent(&dev_inst, dev_inst, 0) == CR_SUCCESS
334 && dev_inst != device_info_data.DevInst) { }
335 if (dev_inst != device_info_data.DevInst) {
336 free(device_detail_data);
337 continue;
338 }
339
340 port->usb_bus = i + 1;
341
342 host_controller_device = CreateFile(device_detail_data->DevicePath,
343 GENERIC_WRITE, FILE_SHARE_WRITE,
344 NULL, OPEN_EXISTING, 0, NULL);
345 if (host_controller_device != INVALID_HANDLE_VALUE) {
02c8a142 346 enumerate_host_controller(port, host_controller_device, dev_inst_match);
e33dcf90
ML
347 CloseHandle(host_controller_device);
348 }
349 free(device_detail_data);
350 }
351
352 SetupDiDestroyDeviceInfoList(device_info);
353 return;
354}
355
970f279a 356SP_PRIV enum sp_return get_port_details(struct sp_port *port)
e33dcf90 357{
dc422c04
UH
358 /*
359 * Description limited to 127 char, anything longer
360 * would not be user friendly anyway.
361 */
e33dcf90
ML
362 char description[128];
363 SP_DEVINFO_DATA device_info_data = { .cbSize = sizeof(device_info_data) };
364 HDEVINFO device_info;
365 int i;
366
367 device_info = SetupDiGetClassDevs(NULL, 0, 0,
368 DIGCF_PRESENT | DIGCF_ALLCLASSES);
369 if (device_info == INVALID_HANDLE_VALUE)
370 RETURN_FAIL("SetupDiGetClassDevs() failed");
371
dc422c04 372 for (i = 0; SetupDiEnumDeviceInfo(device_info, i, &device_info_data); i++) {
e33dcf90
ML
373 HKEY device_key;
374 DEVINST dev_inst;
375 char value[8], class[16];
376 DWORD size, type;
377 CONFIGRET cr;
378
dc422c04 379 /* Check if this is the device we are looking for. */
b328a48b
AJ
380 device_key = SetupDiOpenDevRegKey(device_info, &device_info_data,
381 DICS_FLAG_GLOBAL, 0,
382 DIREG_DEV, KEY_QUERY_VALUE);
383 if (device_key == INVALID_HANDLE_VALUE)
e33dcf90
ML
384 continue;
385 size = sizeof(value);
386 if (RegQueryValueExA(device_key, "PortName", NULL, &type, (LPBYTE)value,
7aeb6736
AJ
387 &size) != ERROR_SUCCESS || type != REG_SZ) {
388 RegCloseKey(device_key);
e33dcf90 389 continue;
7aeb6736 390 }
e33dcf90 391 RegCloseKey(device_key);
81243567 392 value[sizeof(value) - 1] = 0;
e33dcf90
ML
393 if (strcmp(value, port->name))
394 continue;
395
dc422c04 396 /* Check port transport type. */
e33dcf90
ML
397 dev_inst = device_info_data.DevInst;
398 size = sizeof(class);
399 cr = CR_FAILURE;
400 while (CM_Get_Parent(&dev_inst, dev_inst, 0) == CR_SUCCESS &&
401 (cr = CM_Get_DevNode_Registry_PropertyA(dev_inst,
402 CM_DRP_CLASS, 0, class, &size, 0)) != CR_SUCCESS) { }
403 if (cr == CR_SUCCESS) {
404 if (!strcmp(class, "USB"))
405 port->transport = SP_TRANSPORT_USB;
406 }
407
dc422c04 408 /* Get port description (friendly name). */
e33dcf90
ML
409 dev_inst = device_info_data.DevInst;
410 size = sizeof(description);
411 while ((cr = CM_Get_DevNode_Registry_PropertyA(dev_inst,
412 CM_DRP_FRIENDLYNAME, 0, description, &size, 0)) != CR_SUCCESS
413 && CM_Get_Parent(&dev_inst, dev_inst, 0) == CR_SUCCESS) { }
414 if (cr == CR_SUCCESS)
415 port->description = strdup(description);
416
dc422c04 417 /* Get more informations for USB connected ports. */
e33dcf90
ML
418 if (port->transport == SP_TRANSPORT_USB) {
419 char usb_path[MAX_USB_PATH] = "", tmp[MAX_USB_PATH];
420 char device_id[MAX_DEVICE_ID_LEN];
421
dc422c04 422 /* Recurse over parents to build the USB device path. */
e33dcf90
ML
423 dev_inst = device_info_data.DevInst;
424 do {
dc422c04 425 /* Verify that this layer of the tree is USB related. */
e33dcf90
ML
426 if (CM_Get_Device_IDA(dev_inst, device_id,
427 sizeof(device_id), 0) != CR_SUCCESS
428 || strncmp(device_id, "USB\\", 4))
429 continue;
430
dc422c04 431 /* Discard one layer for composite devices. */
e33dcf90
ML
432 char compat_ids[512], *p = compat_ids;
433 size = sizeof(compat_ids);
434 if (CM_Get_DevNode_Registry_PropertyA(dev_inst,
435 CM_DRP_COMPATIBLEIDS, 0,
436 &compat_ids,
437 &size, 0) == CR_SUCCESS) {
438 while (*p) {
439 if (!strncmp(p, "USB\\COMPOSITE", 13))
440 break;
441 p += strlen(p) + 1;
442 }
443 if (*p)
444 continue;
445 }
446
dc422c04 447 /* Stop the recursion when reaching the USB root. */
e33dcf90
ML
448 if (!strncmp(device_id, "USB\\ROOT", 8))
449 break;
450
dc422c04 451 /* Prepend the address of current USB layer to the USB path. */
e33dcf90
ML
452 DWORD address;
453 size = sizeof(address);
454 if (CM_Get_DevNode_Registry_PropertyA(dev_inst, CM_DRP_ADDRESS,
455 0, &address, &size, 0) == CR_SUCCESS) {
456 strcpy(tmp, usb_path);
457 snprintf(usb_path, sizeof(usb_path), "%d%s%s",
458 (int)address, *tmp ? "." : "", tmp);
459 }
460 } while (CM_Get_Parent(&dev_inst, dev_inst, 0) == CR_SUCCESS);
461
462 port->usb_path = strdup(usb_path);
463
dc422c04 464 /* Wake up the USB device to be able to read string descriptor. */
e33dcf90
ML
465 char *escaped_port_name;
466 HANDLE handle;
467 if (!(escaped_port_name = malloc(strlen(port->name) + 5)))
468 RETURN_ERROR(SP_ERR_MEM, "Escaped port name malloc failed");
469 sprintf(escaped_port_name, "\\\\.\\%s", port->name);
e47c7dcb
ML
470 handle = CreateFileA(escaped_port_name, GENERIC_READ, 0, 0,
471 OPEN_EXISTING,
472 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED, 0);
e33dcf90
ML
473 free(escaped_port_name);
474 CloseHandle(handle);
475
dc422c04 476 /* Retrieve USB device details from the device descriptor. */
e33dcf90
ML
477 get_usb_details(port, device_info_data.DevInst);
478 }
479 break;
480 }
481
7aeb6736
AJ
482 SetupDiDestroyDeviceInfoList(device_info);
483
e33dcf90
ML
484 RETURN_OK();
485}
486
970f279a 487SP_PRIV enum sp_return list_ports(struct sp_port ***list)
48a4076f
AJ
488{
489 HKEY key;
490 TCHAR *value, *data;
491 DWORD max_value_len, max_data_size, max_data_len;
492 DWORD value_len, data_size, data_len;
493 DWORD type, index = 0;
c79e0ac8 494 LSTATUS result;
48a4076f
AJ
495 char *name;
496 int name_len;
497 int ret = SP_OK;
498
499 DEBUG("Opening registry key");
c79e0ac8
ML
500 if ((result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"),
501 0, KEY_QUERY_VALUE, &key)) != ERROR_SUCCESS) {
84888681
ML
502 /* It's possible for this key to not exist if there are no serial ports
503 * at all. In that case we're done. Return a failure for any other error. */
504 if (result != ERROR_FILE_NOT_FOUND) {
505 SetLastError(result);
506 SET_FAIL(ret, "RegOpenKeyEx() failed");
507 }
48a4076f
AJ
508 goto out_done;
509 }
510 DEBUG("Querying registry key value and data sizes");
c79e0ac8
ML
511 if ((result = RegQueryInfoKey(key, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
512 &max_value_len, &max_data_size, NULL, NULL)) != ERROR_SUCCESS) {
513 SetLastError(result);
48a4076f
AJ
514 SET_FAIL(ret, "RegQueryInfoKey() failed");
515 goto out_close;
516 }
517 max_data_len = max_data_size / sizeof(TCHAR);
518 if (!(value = malloc((max_value_len + 1) * sizeof(TCHAR)))) {
dc422c04 519 SET_ERROR(ret, SP_ERR_MEM, "Registry value malloc failed");
48a4076f
AJ
520 goto out_close;
521 }
522 if (!(data = malloc((max_data_len + 1) * sizeof(TCHAR)))) {
dc422c04 523 SET_ERROR(ret, SP_ERR_MEM, "Registry data malloc failed");
48a4076f
AJ
524 goto out_free_value;
525 }
526 DEBUG("Iterating over values");
527 while (
528 value_len = max_value_len + 1,
529 data_size = max_data_size,
530 RegEnumValue(key, index, value, &value_len,
531 NULL, &type, (LPBYTE)data, &data_size) == ERROR_SUCCESS)
532 {
7aeb6736 533 if (type == REG_SZ) {
6c444ade
AJ
534 data_len = data_size / sizeof(TCHAR);
535 data[data_len] = '\0';
48a4076f 536#ifdef UNICODE
6c444ade 537 name_len = WideCharToMultiByte(CP_ACP, 0, data, -1, NULL, 0, NULL, NULL);
48a4076f 538#else
6c444ade 539 name_len = data_len + 1;
48a4076f 540#endif
6c444ade 541 if (!(name = malloc(name_len))) {
dc422c04 542 SET_ERROR(ret, SP_ERR_MEM, "Registry port name malloc failed");
6c444ade
AJ
543 goto out;
544 }
48a4076f 545#ifdef UNICODE
6c444ade 546 WideCharToMultiByte(CP_ACP, 0, data, -1, name, name_len, NULL, NULL);
48a4076f 547#else
6c444ade 548 strcpy(name, data);
48a4076f 549#endif
d38c7d2c 550 DEBUG_FMT("Found port %s", name);
48a4076f 551 if (!(*list = list_append(*list, name))) {
dc422c04 552 SET_ERROR(ret, SP_ERR_MEM, "List append failed");
7aeb6736 553 free(name);
48a4076f
AJ
554 goto out;
555 }
7aeb6736 556 free(name);
48a4076f
AJ
557 }
558 index++;
559 }
560out:
561 free(data);
562out_free_value:
563 free(value);
564out_close:
565 RegCloseKey(key);
566out_done:
567
568 return ret;
569}