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