-
Notifications
You must be signed in to change notification settings - Fork 10
/
listports.c
401 lines (357 loc) · 12.9 KB
/
listports.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/* (C) 2001-2005 Joaquín Mª López Muñoz (joaquin@tid.es). All rights reserved.
* (C) 2005 Tony Kmoch, Telematix a.s. [Windows CE version]
*
* Permission is granted to use, distribute and modify this code provided that:
* · this copyright notice remain unchanged,
* · you submit all changes to the copyright holder and properly mark the
* changes so they can be told from the original code,
* · credits are given to the copyright holder in the documentation of any
* software using this code with the following line:
* "Portions copyright 2001 Joaquín Mª López Muñoz (joaquin@tid.es)"
*
* The author welcomes any suggestions on the code or reportings of actual
* use of the code. Please send your comments to joaquin@tid.es.
*
* The author makes NO WARRANTY or representation, either express or implied,
* with respect to this code, its quality, accuracy, merchantability, or
* fitness for a particular purpose. This software is provided "AS IS", and
* you, its user, assume the entire risk as to its quality and accuracy.
*
* Last modified: August 23rd, 2005
*/
#include "listports.h"
#include <tchar.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static BOOL Win9xListPorts(LISTPORTS_CALLBACK lpCbk,LPVOID lpCbkValue);
static BOOL WinNT40ListPorts(LISTPORTS_CALLBACK lpCbk,LPVOID lpCbkValue);
static BOOL ScanEnumTree(LPCTSTR lpEnumPath,LISTPORTS_CALLBACK lpCbk,LPVOID lpCbkValue);
static LONG OpenSubKeyByIndex(
HKEY hKey,DWORD dwIndex,REGSAM samDesired,PHKEY phkResult,LPTSTR* lppSubKeyName);
static LONG QueryStringValue(HKEY hKey,LPCTSTR lpValueName, LPTSTR* lppStringValue);
BOOL ListPorts(LISTPORTS_CALLBACK lpCbk,LPVOID lpCbkValue)
{
OSVERSIONINFO osvinfo;
/* check parameters */
if(!lpCbk){
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
/* determine which platform we're running on and forward
* to the appropriate routine
*/
ZeroMemory(&osvinfo,sizeof(osvinfo));
osvinfo.dwOSVersionInfoSize=sizeof(osvinfo);
GetVersionEx(&osvinfo);
switch(osvinfo.dwPlatformId){
case VER_PLATFORM_WIN32_WINDOWS:
return Win9xListPorts(lpCbk,lpCbkValue);
break;
case VER_PLATFORM_WIN32_NT:
return WinNT40ListPorts(lpCbk,lpCbkValue);
break;
default:
SetLastError(ERROR_OLD_WIN_VERSION);
return FALSE;
break;
}
}
BOOL Win9xListPorts(LISTPORTS_CALLBACK lpCbk,LPVOID lpCbkValue)
/* Information on communications ports is stored in various places in the registry under
* the HKEY_LOCAL_MACHINE\ENUM key. The structure of the HKLM\ENUM key, where information
* on all hw devices present on the system is stored, is as follows
*
* HKLM\ENUM
* |-LEVEL1
* |-LEVEL2
* |-LEVEL3
* · CLASS
*
* LEVEL1 keys indicate the type of technology (vg., "PCI", "BIOS", "SCSI"). LEVEL2
* keys refer to the type of device. So, for instance, "*PNP0501" is for 16550A UARTs.
* LEVEL3 keys enumerate actual devices installed on the system. The value "CLASS",
* present in all LEVEL3 keys, informs about the type of driver. For our purposes, we must
* pay attention to the Class "Ports", that includes serial as well as parallel ports.
* In these cases, another two values, "PORTNAME" and "FRIENDLYNAME" are expected.
* An example follows. Typically, COM1 is recognized as a PnP device by the system BIOS
* and as such placed on the registry like this:
*
* HKLM\ENUM
* |-BIOS
* |-*PNP0501
* |-0D (or any other value, this is not important for us)
* · CLASS= "Ports"
* · PORTNAME= "COM1"
* · FRIENDLYNAME= "Communications Port (COM1)"
*
* Win9xListPorts() scans all LEVEL3 keys under HKLM\ENUM searching for the Class
* for ports, and when this is found determines, by means of "PORTNAME", whether the
* device is a serial port or not.
*/
{
return ScanEnumTree(TEXT("ENUM"),lpCbk,lpCbkValue);
}
static BOOL WinNT40ListPorts(LISTPORTS_CALLBACK lpCbk,LPVOID lpCbkValue)
/* Unlike Win9x, information on serial ports registry storing in NT 4.0 is
* scarce. HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM is reliably
* documented to hold the names of all installed serial ports, but I haven't
* confirmed this for infrared ports and other nonstandard drivers.
* Descriptions stored under SERIALCOMM lack the "FRIENDLYNAME" found
* in Win9x, there's only the bare names of the ports.
*/
{
DWORD dwError=0;
HKEY hKey=NULL;
DWORD dwIndex;
LPTSTR lpValueName=NULL;
LPTSTR lpPortName=NULL;
if((dwError=RegOpenKeyEx(HKEY_LOCAL_MACHINE,TEXT("HARDWARE\\DEVICEMAP\\SERIALCOMM"),
0,KEY_READ,&hKey)) != 0){
/* it is really strange that this key does not exist, but could happen in theory */
if(dwError==ERROR_FILE_NOT_FOUND)dwError=0;
goto end;
}
for(dwIndex=0;;++dwIndex){
DWORD cbValueName=32*sizeof(TCHAR);
DWORD cbPortName=32*sizeof(TCHAR);
LISTPORTS_PORTINFO portinfo;
/* loop asking for the value data til we allocated enough memory */
for(;;){
free(lpValueName);
if(!(lpValueName=(LPTSTR)malloc(cbValueName))){
dwError=ERROR_NOT_ENOUGH_MEMORY;
goto end;
}
free(lpPortName);
if(!(lpPortName=(LPTSTR)malloc(cbPortName))){
dwError=ERROR_NOT_ENOUGH_MEMORY;
goto end;
}
if(!(dwError=RegEnumValue(hKey,dwIndex,lpValueName,&cbValueName,
NULL,NULL,(LPBYTE)lpPortName,&cbPortName))){
break; /* we did it */
}
else if(dwError==ERROR_MORE_DATA){ /* not enough space */
dwError=0;
/* no indication of space required, we try doubling */
cbValueName=(cbValueName+sizeof(TCHAR))*2;
}
else goto end;
}
portinfo.lpPortName=lpPortName;
portinfo.lpFriendlyName=lpPortName; /* no friendly name in NT 4.0 */
portinfo.lpTechnology=TEXT(""); /* this information is not available */
if(!lpCbk(lpCbkValue,&portinfo)){
goto end; /* listing aborted by callback */
}
}
end:
free(lpValueName);
free(lpPortName);
if(hKey!=NULL)RegCloseKey(hKey);
if(dwError!=0){
SetLastError(dwError);
return FALSE;
}
else return TRUE;
}
BOOL ScanEnumTree(LPCTSTR lpEnumPath,LISTPORTS_CALLBACK lpCbk,LPVOID lpCbkValue)
{
static const TCHAR lpstrPortsClass[]= TEXT("PORTS");
static const TCHAR lpstrPortsClassGUID[]=TEXT("{4D36E978-E325-11CE-BFC1-08002BE10318}");
DWORD dwError=0;
HKEY hkEnum=NULL;
DWORD dwIndex1;
HKEY hkLevel1=NULL;
DWORD dwIndex2;
HKEY hkLevel2=NULL;
DWORD dwIndex3;
HKEY hkLevel3=NULL;
HKEY hkDeviceParameters=NULL;
TCHAR lpClass[sizeof(lpstrPortsClass)/sizeof(lpstrPortsClass[0])];
DWORD cbClass;
TCHAR lpClassGUID[sizeof(lpstrPortsClassGUID)/sizeof(lpstrPortsClassGUID[0])];
DWORD cbClassGUID;
LPTSTR lpPortName=NULL;
LPTSTR lpFriendlyName=NULL;
LPTSTR lpTechnology=NULL;
if((dwError=RegOpenKeyEx(HKEY_LOCAL_MACHINE,lpEnumPath,0,KEY_ENUMERATE_SUB_KEYS,&hkEnum)) != 0){
goto end;
}
for(dwIndex1=0;;++dwIndex1){
if(hkLevel1!=NULL){
RegCloseKey(hkLevel1);
hkLevel1=NULL;
}
if((dwError=OpenSubKeyByIndex(
hkEnum,dwIndex1,KEY_ENUMERATE_SUB_KEYS,&hkLevel1,&lpTechnology)) != 0){
if(dwError==ERROR_NO_MORE_ITEMS){
dwError=0;
break;
}
else goto end;
}
for(dwIndex2=0;;++dwIndex2){
if(hkLevel2!=NULL){
RegCloseKey(hkLevel2);
hkLevel2=NULL;
}
if((dwError=OpenSubKeyByIndex(
hkLevel1,dwIndex2,KEY_ENUMERATE_SUB_KEYS,&hkLevel2,NULL)) != 0){
if(dwError==ERROR_NO_MORE_ITEMS){
dwError=0;
break;
}
else goto end;
}
for(dwIndex3=0;;++dwIndex3){
BOOL bFriendlyNameNotFound=FALSE;
LISTPORTS_PORTINFO portinfo;
if(hkLevel3!=NULL){
RegCloseKey(hkLevel3);
hkLevel3=NULL;
}
if((dwError=OpenSubKeyByIndex(hkLevel2,dwIndex3,KEY_READ,&hkLevel3,NULL)) != 0){
if(dwError==ERROR_NO_MORE_ITEMS){
dwError=0;
break;
}
else goto end;
}
/* Look if the driver class is the one we're looking for.
* We accept either "CLASS" or "CLASSGUID" as identifiers.
* No need to dynamically arrange for space to retrieve the values,
* they must have the same length as the strings they're compared to
* if the comparison is to be succesful.
*/
cbClass=sizeof(lpClass);
if(RegQueryValueEx(hkLevel3,TEXT("CLASS"),NULL,NULL,
(LPBYTE)lpClass,&cbClass)==ERROR_SUCCESS&&
_tcsicmp(lpClass,lpstrPortsClass)==0){
/* ok */
}
else{
cbClassGUID=sizeof(lpClassGUID);
if(RegQueryValueEx(hkLevel3,TEXT("CLASSGUID"),NULL,NULL,
(LPBYTE)lpClassGUID,&cbClassGUID)==ERROR_SUCCESS&&
_tcsicmp(lpClassGUID,lpstrPortsClassGUID)==0){
/* ok */
}
else continue;
}
/* get "PORTNAME" */
dwError=QueryStringValue(hkLevel3,TEXT("PORTNAME"),&lpPortName);
if(dwError==ERROR_FILE_NOT_FOUND){
/* In Win200, "PORTNAME" is located under the subkey "DEVICE PARAMETERS".
* Try and look there.
*/
if(hkDeviceParameters!=NULL){
RegCloseKey(hkDeviceParameters);
hkDeviceParameters=NULL;
}
if(RegOpenKeyEx(hkLevel3,TEXT("DEVICE PARAMETERS"),0,KEY_READ,
&hkDeviceParameters)==ERROR_SUCCESS){
dwError=QueryStringValue(hkDeviceParameters,TEXT("PORTNAME"),&lpPortName);
}
}
if(dwError){
if(dwError==ERROR_FILE_NOT_FOUND){
/* boy that was strange, we better skip this device */
dwError=0;
continue;
}
else goto end;
}
/* check if it is a serial port (instead of, say, a parallel port) */
if(strnicmp(lpPortName,TEXT("COM"),3)!=0)continue;
/* now go for "FRIENDLYNAME" */
if((dwError=QueryStringValue(hkLevel3,TEXT("FRIENDLYNAME"),&lpFriendlyName)) != 0){
if(dwError==ERROR_FILE_NOT_FOUND){
bFriendlyNameNotFound=TRUE;
dwError=0;
}
else goto end;
}
/* Assemble the information and pass it on to the callback.
* In the unlikely case there's no friendly name available,
* use port name instead.
*/
portinfo.lpPortName=lpPortName;
portinfo.lpFriendlyName=bFriendlyNameNotFound?lpPortName:lpFriendlyName;
portinfo.lpTechnology=lpTechnology;
if(!lpCbk(lpCbkValue,&portinfo)){
goto end; /* listing aborted by callback */
}
}
}
}
end:
free(lpTechnology);
free(lpFriendlyName);
free(lpPortName);
if(hkDeviceParameters!=NULL)RegCloseKey(hkDeviceParameters);
if(hkLevel3!=NULL) RegCloseKey(hkLevel3);
if(hkLevel2!=NULL) RegCloseKey(hkLevel2);
if(hkLevel1!=NULL) RegCloseKey(hkLevel1);
if(hkEnum!=NULL) RegCloseKey(hkEnum);
if(dwError!=0){
SetLastError(dwError);
return FALSE;
}
else return TRUE;
}
LONG OpenSubKeyByIndex(
HKEY hKey,DWORD dwIndex,REGSAM samDesired,PHKEY phkResult,LPTSTR* lppSubKeyName)
{
DWORD dwError=0;
BOOL bLocalSubkeyName=FALSE;
LPTSTR lpSubkeyName=NULL;
DWORD cbSubkeyName=128*sizeof(TCHAR); /* an initial guess */
FILETIME filetime;
if(lppSubKeyName==NULL){
bLocalSubkeyName=TRUE;
lppSubKeyName=&lpSubkeyName;
}
/* loop asking for the subkey name til we allocated enough memory */
for(;;){
free(*lppSubKeyName);
if(!(*lppSubKeyName=(LPTSTR)malloc(cbSubkeyName))){
dwError=ERROR_NOT_ENOUGH_MEMORY;
goto end;
}
if(!(dwError=RegEnumKeyEx(hKey,dwIndex,*lppSubKeyName,&cbSubkeyName,
0,NULL,NULL,&filetime))){
break; /* we did it */
}
else if(dwError==ERROR_MORE_DATA){ /* not enough space */
dwError=0;
/* no indication of space required, we try doubling */
cbSubkeyName=(cbSubkeyName+sizeof(TCHAR))*2;
}
else goto end;
}
dwError=RegOpenKeyEx(hKey,*lppSubKeyName,0,samDesired,phkResult);
end:
if(bLocalSubkeyName)free(*lppSubKeyName);
return dwError;
}
LONG QueryStringValue(HKEY hKey,LPCTSTR lpValueName,LPTSTR* lppStringValue)
{
DWORD cbStringValue=128*sizeof(TCHAR); /* an initial guess */
for(;;){
DWORD dwError;
free(*lppStringValue);
if(!(*lppStringValue=(LPTSTR)malloc(cbStringValue))){
return ERROR_NOT_ENOUGH_MEMORY;
}
if(!(dwError=RegQueryValueEx(hKey,lpValueName,NULL,NULL,
(LPBYTE)*lppStringValue,&cbStringValue))){
return ERROR_SUCCESS;
}
else if(dwError==ERROR_MORE_DATA){
/* not enough space, keep looping */
}
else return dwError;
}
}