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