-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial_enum.c
303 lines (234 loc) · 7.14 KB
/
serial_enum.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
//---------------------------------------------------------------------------
/*
This enumerator is 99.9% based on this material:
http://www.codeproject.com/KB/system/serial_portsenum_fifo.aspx
By Vladimir Afanasyev
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <setupapi.h>
#define MAX_NAME_PORTS 7
#define RegDisposition_OpenExisting (0x00000001) // open key only if exists
#define CM_REGISTRY_HARDWARE (0x00000000)
static int main_result = 0;
static int scan_flag = 0;
static int web_flag = 0;
static int detect_flag = 0;
static char file_name[MAX_PATH];
static FILE *output;
HANDLE serial_handle;
typedef DWORD WINAPI
(* CM_Open_DevNode_Key)(DWORD, DWORD, DWORD, DWORD, PHKEY, DWORD);
HKEY GetDeviceKey(LPTSTR portName)
{
static HINSTANCE CfgMan;
static CM_Open_DevNode_Key OpenDevNodeKey=NULL;
int numDev=0;
int res1;
BOOL guidTest=FALSE;
DWORD RequiredSize=0;
char* buf;
HDEVINFO DeviceInfoSet;
SP_DEVINFO_DATA DeviceInfoData= {0};
unsigned char DevName[MAX_NAME_PORTS]= {0};
guidTest=SetupDiClassGuidsFromNameA(
"Ports",0,0,&RequiredSize);
if(RequiredSize < 1)
return (HKEY)-1;
buf=(char *)malloc(RequiredSize*sizeof(GUID));
guidTest=SetupDiClassGuidsFromNameA(
"Ports",(GUID *)buf,RequiredSize*sizeof(GUID),&RequiredSize);
if(!guidTest)return (HKEY)-1;
DeviceInfoSet=SetupDiGetClassDevs(
(GUID *)buf,NULL,NULL,DIGCF_PRESENT);
if(DeviceInfoSet == INVALID_HANDLE_VALUE)
return (HKEY)-1;
free(buf);
if(!OpenDevNodeKey)
{
CfgMan=LoadLibrary("cfgmgr32");
if(!CfgMan)return (HKEY)-1;
OpenDevNodeKey=(CM_Open_DevNode_Key)GetProcAddress(CfgMan,
"CM_Open_DevNode_Key");
if(!OpenDevNodeKey)
{
FreeLibrary(CfgMan);
return (HKEY)-1;
}
}
DeviceInfoData.cbSize=sizeof(SP_DEVINFO_DATA);
while(TRUE)
{
HKEY KeyDevice;
DWORD len,len2;
res1=SetupDiEnumDeviceInfo(
DeviceInfoSet,numDev,&DeviceInfoData);
if(!res1)
{
SetupDiDestroyDeviceInfoList(DeviceInfoSet);
FreeLibrary(CfgMan);
OpenDevNodeKey=NULL;
return (HKEY)-1;
}
res1=OpenDevNodeKey(DeviceInfoData.DevInst,KEY_QUERY_VALUE | KEY_WRITE,0,
RegDisposition_OpenExisting,&KeyDevice,CM_REGISTRY_HARDWARE);
if(res1 != ERROR_SUCCESS)
{
SetupDiDestroyDeviceInfoList(DeviceInfoSet);
FreeLibrary(CfgMan);
OpenDevNodeKey=NULL;
return (HKEY)-1;
}
len=MAX_NAME_PORTS;
res1=RegQueryValueEx(
KeyDevice, // handle of key to query
"portname", // address of name of value to query
NULL, // reserved
NULL, // address of buffer for value type
DevName, // address of data buffer
&len // address of data buffer size
);
if(res1 != ERROR_SUCCESS)
{
FreeLibrary(CfgMan);
OpenDevNodeKey=NULL;
return (HKEY)-1;
}
if(!strcasecmp((char*)DevName,(char*)portName))
{
FreeLibrary(CfgMan);
OpenDevNodeKey=NULL;
return KeyDevice;
}
RegCloseKey(KeyDevice);
numDev++;
}
}
HANDLE BeginEnumeratePorts(VOID)
{
BOOL guidTest=FALSE;
DWORD RequiredSize=0;
int j;
HDEVINFO DeviceInfoSet;
char* buf;
guidTest=SetupDiClassGuidsFromNameA(
"Ports",0,0,&RequiredSize);
if(RequiredSize < 1)return (HANDLE)(-1);
buf=(char *)malloc(RequiredSize*sizeof(GUID));
guidTest=SetupDiClassGuidsFromNameA(
"Ports",(GUID *)buf,RequiredSize*sizeof(GUID),&RequiredSize);
if(!guidTest)return (HANDLE)(-1);
DeviceInfoSet=SetupDiGetClassDevs(
(GUID *)buf,NULL,NULL,DIGCF_PRESENT);
if(DeviceInfoSet == INVALID_HANDLE_VALUE)return (HANDLE)-1;
free(buf);
return DeviceInfoSet;
}
char FriendlyName[MAX_PATH]= {0};
BOOL EnumeratePortsNext(HANDLE DeviceInfoSet, LPTSTR lpBuffer)
{
static CM_Open_DevNode_Key OpenDevNodeKey=NULL;
static HINSTANCE CfgMan;
int res1,res2;
unsigned char DevName[MAX_NAME_PORTS]= {0};
static int numDev=0;
int numport;
if(DeviceInfoSet == NULL && lpBuffer == NULL)
{
numDev = 0;
return FALSE;
}
SP_DEVINFO_DATA DeviceInfoData= {0};
DeviceInfoData.cbSize=sizeof(SP_DEVINFO_DATA);
if(!DeviceInfoSet || !lpBuffer)return -1;
if(!OpenDevNodeKey)
{
CfgMan=LoadLibrary("cfgmgr32");
if(!CfgMan)return FALSE;
OpenDevNodeKey=(CM_Open_DevNode_Key)GetProcAddress(CfgMan,"CM_Open_DevNode_Key");
if(!OpenDevNodeKey)
{
FreeLibrary(CfgMan);
return FALSE;
}
}
while(TRUE)
{
HKEY KeyDevice;
DWORD len,len2;
res1=SetupDiEnumDeviceInfo(
DeviceInfoSet,numDev,&DeviceInfoData);
if(!res1)
{
SetupDiDestroyDeviceInfoList(DeviceInfoSet);
FreeLibrary(CfgMan);
OpenDevNodeKey=NULL;
return FALSE;
}
res1=OpenDevNodeKey(DeviceInfoData.DevInst,KEY_QUERY_VALUE,0,
RegDisposition_OpenExisting,&KeyDevice,CM_REGISTRY_HARDWARE);
if(res1 != ERROR_SUCCESS)
return NULL;
len=MAX_NAME_PORTS;
res1=RegQueryValueEx(
KeyDevice, // handle of key to query
"portname", // address of name of value to query
NULL, // reserved
NULL, // address of buffer for value type
DevName, // address of data buffer
&len // address of data buffer size
);
RegCloseKey(KeyDevice);
if(res1 != ERROR_SUCCESS)return NULL;
numDev++;
if(memicmp(DevName, "com", 3))continue;
numport=atoi(DevName+3);
if(numport > 0)
{
strcpy(lpBuffer,DevName);
return TRUE;
}
FreeLibrary(CfgMan);
OpenDevNodeKey=NULL;
return FALSE;
}
}
//---------------------------------------------------------------------------
BOOL EndEnumeratePorts(HANDLE DeviceInfoSet)
{
if(SetupDiDestroyDeviceInfoList(DeviceInfoSet))
return TRUE;
printf("Error:%d\n",GetLastError());
return FALSE;
}
char *filename_extract(char *full_name)
{
int len = strlen(full_name);
int i;
for(i=len-1; i>=0; i--)
{
if(full_name[i] == '/' || full_name[i] == '\\')
return &full_name[i+1];
}
return full_name;
}
int get_port_list(char *ports_pull)
{
char port[20] = "",*p = ports_pull;
HANDLE res;
int cnt = 0;
EnumeratePortsNext(NULL, NULL);
res=BeginEnumeratePorts();
while(TRUE)
{
if(!EnumeratePortsNext(res,port))
break;
cnt++;
p += sprintf(p,"%s\n",port);
}
// EndEnumeratePorts(res);
return cnt;
}