-
Notifications
You must be signed in to change notification settings - Fork 42
/
listComPorts.c
148 lines (113 loc) · 3.17 KB
/
listComPorts.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
/*
* listComPorts.c -- list COM ports
*
* http://github.com/todbot/usbSearch/
*
* 2012, Tod E. Kurt, http://todbot.com/blog/
*
*
* Uses DispHealper : http://disphelper.sourceforge.net/
*
* Notable VIDs & PIDs combos:
* VID 0403 - FTDI
*
* VID 0403 / PID 6001 - Arduino Diecimila
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <windows.h>
#include <setupapi.h>
#include <ddk/hidsdi.h>
#include <ddk/hidclass.h>
#include "disphelper.h"
//
void usage(void)
{
fprintf(stderr, "Usage: listComPorts [-h] [-v] [-vid <vid>] [-pid <pid>]\n");
fprintf(stderr, "\t-vid <vid> : Search by Vendor ID\n");
fprintf(stderr, "\t-pid <pid> : Search by Product ID\n");
fprintf(stderr, "\t-v : Verbose output (more for more output)\n");
fprintf(stderr, "\t-h : This help message\n");
fprintf(stderr, "\nFor more info, https://github.com/todbot/usbSearch\n");
exit(1);
}
// command-line options
int verbose = 0;
char* VIDstr;
char* PIDstr;
void do_silly_test(void);
//
void parse_options(int argc, char **argv)
{
int i;
const char *arg;
for (i=1; i<argc; i++) {
arg = argv[i];
//printf("arg: %s\n", arg);
if (*arg == '-') { // starts with a dash
if (strcmp(arg, "-h") == 0) {
usage();
} else if (strcmp(arg, "-v") == 0) {
verbose++;
} else if (strcmp(arg, "-vid") == 0) {
VIDstr = argv[++i];
} else if (strcmp(arg, "-pid") == 0) {
PIDstr = argv[++i];
}
}
}
}
//
int listComPorts(void)
{
if(verbose)
printf("Searching for COM ports...\n");
DISPATCH_OBJ(wmiSvc);
DISPATCH_OBJ(colDevices);
dhInitialize(TRUE);
dhToggleExceptions(TRUE);
dhGetObject(L"winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\cimv2",
//dhGetObject(L"winmgmts:\\\\.\\root\\cimv2",
NULL, &wmiSvc);
dhGetValue(L"%o", &colDevices, wmiSvc,
L".ExecQuery(%S)",
L"Select * from Win32_PnPEntity");
int port_count = 0;
FOR_EACH(objDevice, colDevices, NULL) {
char* name = NULL;
char* pnpid = NULL;
char* manu = NULL;
char* match;
dhGetValue(L"%s", &name, objDevice, L".Name");
dhGetValue(L"%s", &pnpid, objDevice, L".PnPDeviceID");
if(verbose>1) printf("'%s'.\n", name);
if( name != NULL && ((match = strstr( name, "(COM" )) != NULL) ) { // look for "(COM23)"
// 'Manufacturuer' can be null, so only get it if we need it
dhGetValue(L"%s", &manu, objDevice, L".Manufacturer");
port_count++;
char* comname = strtok( match, "()");
printf("%s - %s - %s\n",comname, manu, pnpid);
dhFreeString(manu);
}
dhFreeString(name);
dhFreeString(pnpid);
} NEXT(objDevice);
SAFE_RELEASE(colDevices);
SAFE_RELEASE(wmiSvc);
dhUninitialize(TRUE);
if(verbose)
printf("Found %d port%s\n",port_count,(port_count==1)?"":"s");
return 0;
}
//
int main(int argc, char **argv)
{
parse_options(argc, argv);
listComPorts();
return 0;
}