-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEnumDevices.cpp
More file actions
232 lines (197 loc) · 6.42 KB
/
Copy pathEnumDevices.cpp
File metadata and controls
232 lines (197 loc) · 6.42 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
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
#include "StdAfx.h"
#include "EnumDevices.h"
#include <setupapi.h>
using namespace DeviceDetectLibrary;
static DeviceType GetDeviceType(const wchar_t* sPath);
static std::wstring GetDeviceRegistryProperty(HDEVINFO & hDevInfo, PSP_DEVINFO_DATA pdevInfoData, DWORD dwProperty);
DeviceType GetDeviceType(const wchar_t* sPath)
{
COMMPROP comProp;
DeviceType type = TypeUnknown;
HANDLE hFile = ::CreateFile(
sPath,
FILE_GENERIC_READ | FILE_GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(INVALID_HANDLE_VALUE != hFile && TRUE == ::GetCommProperties(hFile, &comProp))
{
switch(comProp.dwProvSubType)
{
case PST_MODEM:
{
type = TypeUsb;
}
break;
case PST_RS232:
{
type = TypeSerial;
}
break;
default:
{
type= TypeUnknown;
}
break;
}
}
::CloseHandle(hFile);
return type;
}
std::wstring GetDeviceRegistryProperty(HDEVINFO & hDevInfo, PSP_DEVINFO_DATA pdevInfoData, DWORD dwProperty)
{
DWORD dwSize = 0;
DWORD dwDataType = 0;
if(NULL == pdevInfoData)
{
// Wrong PSP_DEVINFO_DATA parameter");
throw std::runtime_error("Wrong PSP_DEVINFO_DATA parameter");
}
if(FALSE == ::SetupDiGetDeviceRegistryProperty(hDevInfo, pdevInfoData, dwProperty, &dwDataType, NULL, 0, &dwSize))
{
DWORD error = ::GetLastError();
if(ERROR_INVALID_DATA == error)
{
return std::wstring();
}
if(ERROR_INSUFFICIENT_BUFFER != error)
{
// Can't get Device Registry Property");
throw std::runtime_error("Can't get Device Registry Property");
}
}
std::vector<TCHAR> sResult(dwSize, 0x00);
if(sResult.empty())
{
// Can't get Device Registry Property is empty");
throw std::runtime_error("Can't get Device Registry Property is empty");
}
if(FALSE == ::SetupDiGetDeviceRegistryProperty(hDevInfo, pdevInfoData, dwProperty, &dwDataType, (PBYTE) &sResult.front(), (DWORD)sResult.size(), &dwSize))
{
// Can't get Device Registry Property");
throw std::runtime_error("Can't get Device Registry Property");
}
sResult.resize(dwSize);
if(sResult.empty())
{
// Device Registry Property is empty");
throw std::runtime_error("Device Registry Property is empty");
}
return std::wstring(&sResult.front());
}
/*
template <class TYPECHECKER>
ConnectionInfo_vt GetDevicesByGuidCheck(GUID* guid, const std::wstring & sPattern, TYPECHECKER checker)
{
ConnectionInfo_vt result;
HDEVINFO hDevInfo = ::SetupDiGetClassDevs(guid, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);
if(INVALID_HANDLE_VALUE == hDevInfo)
{
// Can't Find Class Devises");
return result;
}
SP_DEVICE_INTERFACE_DATA devInterfaceData;
ZeroMemory(&devInterfaceData, sizeof(SP_DEVICE_INTERFACE_DATA));
devInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
for(DWORD dwCount = 0; ::SetupDiEnumDeviceInterfaces(hDevInfo, NULL, guid, dwCount, &devInterfaceData); ++dwCount)
{
try
{
ConnectionInfo info = checker(hDevInfo, &devInterfaceData, sPattern);
result.push_back(info);
}
catch(const std::exception & ex)
{
ex.what();
}
}
::SetupDiDestroyDeviceInfoList(hDevInfo);
return result;
}
*/
//ConnectionInfo PureChecker(HDEVINFO hDevInfo, SP_DEVICE_INTERFACE_DATA* devInterfaceData, const std::wstring & sPattern)
bool PureChecker(HDEVINFO hDevInfo, SP_DEVICE_INTERFACE_DATA* devInterfaceData, const std::wstring & sPattern, CConnectionInfo::Pointer &pConnectionInfo)
{
pConnectionInfo->Clear();
TCHAR tcBuffer[0x400] = {0x00};
PSP_DEVICE_INTERFACE_DETAIL_DATA pDevInfoDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA) tcBuffer;
pDevInfoDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
SP_DEVINFO_DATA devInfoData;
ZeroMemory(&devInfoData, sizeof(SP_DEVINFO_DATA));
devInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
DWORD dwReqSize = 0;
if(TRUE == ::SetupDiGetDeviceInterfaceDetail(hDevInfo, devInterfaceData, pDevInfoDetail, sizeof(tcBuffer)/sizeof(TCHAR), &dwReqSize, &devInfoData))
{
pConnectionInfo->Type = TypeUsb;
pConnectionInfo->FriendlyName = GetDeviceRegistryProperty(hDevInfo, &devInfoData, SPDRP_FRIENDLYNAME);
pConnectionInfo->DeviceDescription = GetDeviceRegistryProperty(hDevInfo, &devInfoData, SPDRP_DEVICEDESC);
pConnectionInfo->HardwareID = GetDeviceRegistryProperty(hDevInfo, &devInfoData, SPDRP_HARDWAREID);
pConnectionInfo->ServiceName = GetDeviceRegistryProperty(hDevInfo, &devInfoData, SPDRP_SERVICE);
pConnectionInfo->DevicePath = pDevInfoDetail->DevicePath;
return true;
}
throw std::runtime_error("Can't SetupDiGetInterfaceDeviceDetail");
}
//ConnectionInfo_vt DeviceDetectLibrary::GetDevicesByGuid(GUID guid)
bool DeviceDetectLibrary::GetDevicesByGuid(GUID guid, ConnectionInfo_vt &vctResult)
{
//return GetDevicesByGuidCheck(&guid, L"", &PureChecker);
vctResult.clear();
//ConnectionInfo_vt result;
HDEVINFO hDevInfo = ::SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);
if(INVALID_HANDLE_VALUE == hDevInfo)
{
// Can't Find Class Devises");
//return result;
return false;
}
SP_DEVICE_INTERFACE_DATA devInterfaceData;
ZeroMemory(&devInterfaceData, sizeof(SP_DEVICE_INTERFACE_DATA));
devInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
for(DWORD dwCount = 0; ::SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &guid, dwCount, &devInterfaceData); ++dwCount)
{
try
{
//ConnectionInfo info = PureChecker(hDevInfo, &devInterfaceData, L"");
CConnectionInfo::Pointer pConnectionInfo = CConnectionInfo::Create();
if(PureChecker(hDevInfo, &devInterfaceData, L"", pConnectionInfo))
{
vctResult.push_back(pConnectionInfo);
}
//result.push_back(info);
}
catch(const std::exception & ex)
{
ex.what();
}
}
::SetupDiDestroyDeviceInfoList(hDevInfo);
return true;
}
HDEVNOTIFY DeviceDetectLibrary::RegisterUSBNotify(const GUID& guid, HANDLE hWnd)
{
DEV_BROADCAST_DEVICEINTERFACE notificationFilter;
ZeroMemory(¬ificationFilter, sizeof(notificationFilter));
notificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
notificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
notificationFilter.dbcc_classguid = guid;
HDEVNOTIFY hDevNotify = ::RegisterDeviceNotification(hWnd,
¬ificationFilter,
DEVICE_NOTIFY_WINDOW_HANDLE
);
if(hDevNotify == NULL)
{
int error = GetLastError();
throw std::runtime_error("Can not register device notify");
}
return hDevNotify;
}
void DeviceDetectLibrary::UnRegisterUSBNotify(HDEVNOTIFY hNotyfy)
{
if(FALSE == ::UnregisterDeviceNotification(hNotyfy))
{
throw std::runtime_error("Can not unregister device notify");
}
}