-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListComPorts.cpp
More file actions
139 lines (125 loc) · 3.78 KB
/
Copy pathListComPorts.cpp
File metadata and controls
139 lines (125 loc) · 3.78 KB
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
// ListComPorts.cpp
// A Simple Example how to List COM Ports in Windows.
// Lists the COM Ports that you can see in Device Manager.
#include <windows.h>
#include <tchar.h>
#include <setupapi.h>
#include <vector>
int _tmain(int argc, _TCHAR* argv[])
{
unsigned int uReturn = ERROR_SUCCESS;
HDEVINFO devInfo;
std::vector<BYTE> memSpc;
std::vector<BYTE> memSpc2;
PSP_INTERFACE_DEVICE_DETAIL_DATA pDevIfcDetailData = NULL;
// This statement returns a device information set that contains all devices
// of a specified class, with the last parameter not set to DIGCF_DEVICEINTERFACE,
// the first parameter specifies a setup class, the last parameter causes this
// function to return only devices that are currently present.
devInfo = SetupDiGetClassDevs((CONST LPGUID) &GUID_DEVINTERFACE_COMPORT,
(LPCTSTR) NULL,
(HWND) NULL,
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if(devInfo == INVALID_HANDLE_VALUE)
{
uReturn = GetLastError();
}
else
{
SP_DEVICE_INTERFACE_DATA devIfcData;
devIfcData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
SP_DEVINFO_DATA devInfoData;
devInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
unsigned int uDevIdx = 0;
for( ; ; uDevIdx++)
{
if(uReturn != ERROR_SUCCESS)
{
break;
}
// The next statement returns a context structure for a device information
// element of a device information set. Each call returns information about
// one device, the function can be called repeatedly to get information
// about several devices.
if(!SetupDiEnumDeviceInterfaces(devInfo,
NULL,
(CONST LPGUID) &GUID_DEVINTERFACE_COMPORT,
uDevIdx,
&devIfcData))
{
uReturn = GetLastError(); // 259 = NO_MORE_ITEMS
_ASSERTE(uReturn == ERROR_NO_MORE_ITEMS);
if(uReturn == ERROR_NO_MORE_ITEMS)
{
uReturn = ERROR_SUCCESS;
}
break;
}
DWORD uReqLength = 0;
DWORD uPreLength = 0;
if(!SetupDiGetDeviceInterfaceDetail(devInfo,
&devIfcData,
NULL,
0,
&uReqLength,
NULL))
{
uReturn = GetLastError();
_ASSERTE(uReturn == ERROR_INSUFFICIENT_BUFFER);
if(uReturn == ERROR_INSUFFICIENT_BUFFER)
{
uReturn = ERROR_SUCCESS;
}
else
{
break;
}
}
uPreLength = uReqLength;
memSpc.resize(uPreLength);
void* pLint = &(memSpc[0]);
pDevIfcDetailData = reinterpret_cast<PSP_INTERFACE_DEVICE_DETAIL_DATA> (pLint);
pDevIfcDetailData->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
if(!SetupDiGetDeviceInterfaceDetail(devInfo,
&devIfcData,
pDevIfcDetailData,
uPreLength,
&uReqLength,
&devInfoData))
{
uReturn = GetLastError();
break;
}
//std::wstring strDeviceInterface = pDevIfcDetailData->DevicePath;
//wprintf(L"%s\n", strDeviceInterface.c_str());
DWORD PropertyRegDataType = 0;
DWORD RequiredSize = 0;
BOOL bRet = SetupDiGetDeviceRegistryProperty(
devInfo,
&devInfoData,
SPDRP_FRIENDLYNAME ,
&PropertyRegDataType,
0,
0,
&RequiredSize
);
memSpc2.resize(RequiredSize);
BYTE* pPropertyBuffer = &memSpc2[0];
bRet = SetupDiGetDeviceRegistryProperty(
devInfo,
&devInfoData,
SPDRP_FRIENDLYNAME ,
&PropertyRegDataType,
pPropertyBuffer,
RequiredSize,
&RequiredSize
);
pPropertyBuffer[RequiredSize-1] = 0;
pPropertyBuffer[RequiredSize-2] = 0;
wprintf(L"%s\n", pPropertyBuffer);
} // end for
// cleanup
SetupDiDestroyDeviceInfoList(devInfo);
}
return 0;
}