-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEnumDisks.cpp
More file actions
569 lines (463 loc) · 15.1 KB
/
EnumDisks.cpp
File metadata and controls
569 lines (463 loc) · 15.1 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
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
#include "StdAfx.h"
#include <cfgmgr32.h>
#include <setupapi.h>
#include "EnumDisks.h"
#include "NotifyWindow.h"
#include "IEnumerator.h"
using namespace Disk;
using namespace Disk::Types;
static RemovableDeviceType GetDeviceType(PSP_DEVINFO_DATA pDevInfoData);
static std::wstring GetDevicePath(PSP_DEVINFO_DATA pDevInfoData);
static std::wstring GetDeviceRegistryProperty(HDEVINFO hDevInfo, PSP_DEVINFO_DATA pDevInfoData, DWORD dwProperty);
static BOOL IsDeviceNumberEq(const std::wstring& sDevicePath, DWORD dwDeviceNumber);
static BOOL GetDrivesDevInst(DWORD dwDeviceNumber, HDEVINFO hDevInfo, DWORD dwIndex, PDEVINST devInst);
static DEVINST FindDrivesDevInstByPath(const std::wstring& wsPath);
static DEVINST FindDrivesDevInstByDeviceNumber(DWORD dwDeviceNumber);
static bool BuildPaths(const std::wstring& sPath, std::wstring& sDevice, std::wstring& sVolumeAccess);
static DWORD GetDeviceNumber(const std::wstring & sVolumeAccess);
//static std::vector<unsigned int> GetHarddiskIndexes(const std::wstring& wsPath);
//static DiskGeometry GetDeviceGeometry(const std::wstring& wsDevInterfaceVolume);
static std::wstring GetDevInterfaceVolume(PSP_DEVINFO_DATA pdevInfoData);
std::wstring GetDevicePath(PSP_DEVINFO_DATA pDevInfoData)
{
if(NULL == pDevInfoData)
{
// Wrong PSP_DEVINFO_DATA parameter
throw std::runtime_error("Wrong PSP_DEVINFO_DATA parameter");
}
std::wstring wsPath(L"X:\\");
for(wchar_t wLeter = L'A'; wLeter <= L'Z'; ++wLeter)
{
wsPath[0] = wLeter;
if(pDevInfoData->DevInst == FindDrivesDevInstByPath(wsPath))
{
return wsPath;
}
}
// Can't find device path
throw std::runtime_error("Can't find device path");
}
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 == ::SetupDiGetDeviceRegistryPropertyW(hDevInfo, pDevInfoData, dwProperty, &dwDataType, NULL, 0, &dwSize))
{
if(ERROR_INSUFFICIENT_BUFFER != ::GetLastError())
{
if(SPDRP_FRIENDLYNAME == dwProperty)
{
return L"Removable Disk";
}
// Can't get Device Registry Property
throw std::runtime_error("Can't get Device Registry Property");
}
}
std::vector<wchar_t> sResult(dwSize + sizeof(wchar_t), 0x00);
if(FALSE == ::SetupDiGetDeviceRegistryPropertyW(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");
}
return std::wstring(&sResult.at(0));
}
BOOL IsDeviceNumberEq(const std::wstring& sDevicePath, DWORD dwDeviceNumber)
{
BOOL bResult = FALSE;
HANDLE hDrive = ::CreateFileW(sDevicePath.c_str(), 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL);
if(INVALID_HANDLE_VALUE == hDrive)
{
return bResult;
}
DWORD dwRead = 0;
STORAGE_DEVICE_NUMBER sdn;
ZeroMemory(&sdn, sizeof(sdn));
if(::DeviceIoControl(hDrive, IOCTL_STORAGE_GET_DEVICE_NUMBER, NULL, 0, &sdn, sizeof(sdn), &dwRead, NULL))
{
bResult = (dwDeviceNumber == sdn.DeviceNumber);
}
::CloseHandle(hDrive);
return bResult;
}
BOOL GetDrivesDevInst(DWORD dwDeviceNumber, HDEVINFO hDevInfo, DWORD dwIndex, PDEVINST devInst)
{
if((INVALID_HANDLE_VALUE == hDevInfo) || (0 == devInst))
{
return FALSE;
}
*devInst = 0;
DWORD dwSize = 0;
BYTE bBuffer[0x1000] = {0};
SP_DEVICE_INTERFACE_DATA spDevInterfaceData;
ZeroMemory(&spDevInterfaceData, sizeof(spDevInterfaceData));
spDevInterfaceData.cbSize = sizeof(spDevInterfaceData);
if(FALSE == ::SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &GUID_DEVINTERFACE_DISK, dwIndex, &spDevInterfaceData))
{
return FALSE;
}
::SetupDiGetDeviceInterfaceDetail(hDevInfo, &spDevInterfaceData, NULL, 0, &dwSize, NULL);
if((0 == dwSize) || (sizeof(bBuffer) < dwSize))
{
return FALSE;
}
SP_DEVINFO_DATA spDevInfoData;
ZeroMemory(&spDevInfoData, sizeof(spDevInfoData));
spDevInfoData.cbSize = sizeof(spDevInfoData);
PSP_DEVICE_INTERFACE_DETAIL_DATA pspDevInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA) bBuffer;
pspDevInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
if(FALSE == ::SetupDiGetDeviceInterfaceDetail(hDevInfo, &spDevInterfaceData, pspDevInterfaceDetailData, dwSize, &dwSize, &spDevInfoData))
{
return TRUE;
}
if(IsDeviceNumberEq(pspDevInterfaceDetailData->DevicePath, dwDeviceNumber))
{
*devInst = spDevInfoData.DevInst;
}
return TRUE;
}
DEVINST FindDrivesDevInstByPath(const std::wstring& wsPath)
{
std::wstring sDevice;
std::wstring sVolumeAccess;
if(!BuildPaths(wsPath, sDevice, sVolumeAccess))
{
return 0;
}
DWORD dwDeviceNumber = GetDeviceNumber(sVolumeAccess);
if(-1 == dwDeviceNumber)
{
return 0;
}
wchar_t szDosDeviceName[MAX_PATH] = {0};
if(FALSE == ::QueryDosDeviceW(sDevice.c_str(), szDosDeviceName, MAX_PATH))
{
return 0;
}
const std::wstring patern = L"Harddisk";
const std::wstring dosDeviceName = szDosDeviceName;
if(dosDeviceName.end() == std::search(dosDeviceName.begin(), dosDeviceName.end(), patern.begin(), patern.end()))
{
return 0;
}
return FindDrivesDevInstByDeviceNumber(dwDeviceNumber);
}
DEVINST FindDrivesDevInstByDeviceNumber(DWORD dwDeviceNumber)
{
CONST DEVINST devInstEmpty = 0;
HDEVINFO hDevInfo = ::SetupDiGetClassDevs(&GUID_DEVINTERFACE_DISK, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
DEVINST devInst = 0;
BOOL bResult = TRUE;
for(DWORD dwIndex = 0; ((dwIndex < 0xFFFFFFFF) && (devInstEmpty == devInst) && bResult); ++dwIndex)
{
bResult = GetDrivesDevInst(dwDeviceNumber, hDevInfo, dwIndex, &devInst);
}
::SetupDiDestroyDeviceInfoList(hDevInfo);
return devInst;
}
bool BuildPaths(const std::wstring& sPath, std::wstring& sDevice, std::wstring& sVolumeAccess)
{
if(sPath.empty())
{
return false;
}
wchar_t wcDriveLetter = sPath[0];
wcDriveLetter &= ~0x20;
if((wcDriveLetter < L'A') || (wcDriveLetter > L'Z'))
{
return false;
}
sDevice.assign(L"X:");
sVolumeAccess.assign(L"\\\\.\\X:");
sDevice[0] = wcDriveLetter;
sVolumeAccess[4] = wcDriveLetter;
return true;
}
DWORD GetDeviceNumber(const std::wstring& sVolumeAccess)
{
DWORD dwDeviceNumber = (DWORD)(-1);
HANDLE hVolume = ::CreateFileW(sVolumeAccess.c_str(), 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL);
if(hVolume == INVALID_HANDLE_VALUE)
{
return dwDeviceNumber;
}
DWORD dwBytesReturned = 0;
STORAGE_DEVICE_NUMBER sdn;
if(::DeviceIoControl(hVolume, IOCTL_STORAGE_GET_DEVICE_NUMBER, NULL, 0, &sdn, sizeof(sdn), &dwBytesReturned, NULL))
{
dwDeviceNumber = sdn.DeviceNumber;
}
::CloseHandle(hVolume);
return dwDeviceNumber;
}
std::vector<unsigned int> GetHarddiskIndexes(const std::wstring& wsPath)
{
CAutoHandle handle(::CreateFileW(
wsPath.c_str(),
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS,
NULL));
VOLUME_DISK_EXTENTS vde;
::ZeroMemory(&vde, sizeof(VOLUME_DISK_EXTENTS));
DWORD dwBytes = 0;
BOOL bRes = ::DeviceIoControl(
handle,
IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,
NULL,
0,
&vde,
sizeof(vde),
&dwBytes,
NULL);
DWORD dwError = ::GetLastError();
if(((FALSE == bRes) && (ERROR_MORE_DATA != dwError)) || (0 == vde.NumberOfDiskExtents))
{
return std::vector<unsigned int>();
}
dwBytes = sizeof(VOLUME_DISK_EXTENTS) + (vde.NumberOfDiskExtents - 1) * sizeof(DISK_EXTENT);
std::vector<BYTE> tmp_buf(dwBytes, 0);
PVOLUME_DISK_EXTENTS pExtVde = (PVOLUME_DISK_EXTENTS)&tmp_buf.at(0);
bRes = ::DeviceIoControl(handle,
IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,
NULL,
0,
pExtVde,
dwBytes,
&dwBytes,
NULL);
if(FALSE == bRes)
{
return std::vector<unsigned int>();
}
std::vector<unsigned int> result(pExtVde->NumberOfDiskExtents, 0);
for(DWORD i = 0; i < pExtVde->NumberOfDiskExtents; ++i)
{
result[i] = pExtVde->Extents[i].DiskNumber;
}
return result;
}
DiskGeometry GetDeviceGeometry(const std::wstring& wsDevInterfaceVolume)
{
DiskGeometry result;
CAutoHandle hDisk(::CreateFileW(
wsDevInterfaceVolume.c_str(),
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
NULL,
NULL));
DWORD dwRead = 0;
BOOL bResult = ::DeviceIoControl(
hDisk,
IOCTL_DISK_GET_DRIVE_GEOMETRY,
NULL,
NULL,
&result,
sizeof(result),
&dwRead,
NULL);
if(FALSE == bResult)
{
throw std::runtime_error("Can't get drive geometry");
}
return result;
}
RemovableDeviceType GetDeviceType(PSP_DEVINFO_DATA pDevInfoData)
{
DEVINST childDevInst;
DWORD res = CM_Get_Child_Ex(&childDevInst, pDevInfoData->DevInst, 0, NULL);
bool isDisk = (CR_NO_SUCH_DEVNODE == res);
if(isDisk)
{
return rdtHardDisk;
}
else
{
return rdtFlash;
}
return rdtUnknown;
}
std::wstring GetDevInterfaceVolume(PSP_DEVINFO_DATA pdevInfoData)
{
ULONG ulSize = 0;
DEVINST childDevInst;
bool isDisk = false;
if(NULL == pdevInfoData)
{
// Wrong PSP_DEVINFO_DATA parameter
throw std::runtime_error("Wrong PSP_DEVINFO_DATA parameter");
}
DWORD res = CM_Get_Child_Ex(&childDevInst, pdevInfoData->DevInst, 0, NULL);
isDisk = (CR_NO_SUCH_DEVNODE == res);
if(isDisk)
{
childDevInst = pdevInfoData->DevInst;
}
else if(CR_SUCCESS != res)
{
// Can't get Child Device Instance
throw std::runtime_error("Can't get Child Device Instance");
}
TCHAR bDeviceID[MAX_DEVICE_ID_LEN] = {0};
if(CR_SUCCESS != CM_Get_Device_ID_Ex(childDevInst, bDeviceID, sizeof(bDeviceID)/sizeof(TCHAR), 0, NULL))
{
// Can't get Device ID");
throw std::runtime_error("Can't get Device ID");
}
LPGUID InterfaceClassGuid = isDisk ? (LPGUID)&DiskClassGuid : (LPGUID)&VolumeClassGuid;
if(CR_SUCCESS != CM_Get_Device_Interface_List_Size(&ulSize, InterfaceClassGuid, bDeviceID, 0))
{
// Can't get Device Instance List Size");
throw std::runtime_error("Can't get Device Instance List Size");
}
std::vector<TCHAR> sResult(ulSize, 0x00);
if(sResult.empty())
{
// Device Interface Volume is empty");
throw std::runtime_error("Device Interface Volume is empty");
}
InterfaceClassGuid = isDisk ? (LPGUID)&GUID_DEVINTERFACE_DISK : (LPGUID)&GUID_DEVINTERFACE_VOLUME;
if(CR_SUCCESS != CM_Get_Device_Interface_List(InterfaceClassGuid, bDeviceID, &sResult.front(), ulSize, 0))
{
// Can't get Device Instance List");
throw std::runtime_error("Can't get Device Instance List");
}
sResult.resize(ulSize);
if(sResult.empty())
{
// Device Interface Volume is empty
throw std::runtime_error("Device Interface Volume is empty");
}
return std::wstring(&sResult.front());
}
bool Functions::SearchRemovalDisks(RemovableDeviceInfo_vt &vctDevice)
{
vctDevice.clear();
/*GUID_DEVCLASS_DISKDRIVE*/
CONST CLSID CLSID_DeviceInstance = { 0x4D36E967, 0xE325, 0x11CE, { 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 } };
HDEVINFO hDevInfo = ::SetupDiGetClassDevs(&CLSID_DeviceInstance, NULL, NULL, DIGCF_PRESENT);
if(INVALID_HANDLE_VALUE == hDevInfo)
{
// Can't Get Class Devices
return false;
}
try
{
DWORD dwCount = 0;
do
{
DWORD dwSize = 0;
DWORD dwDataType = 0;
DWORD dwRemovalPolicy = 0;
SP_DEVINFO_DATA devInfoData;
devInfoData.cbSize = sizeof(devInfoData);
if(!::SetupDiEnumDeviceInfo(hDevInfo, dwCount, &devInfoData))
{
break;
}
// [Warning]: only Windows XP and later
if(TRUE == ::SetupDiGetDeviceRegistryProperty(hDevInfo, &devInfoData, SPDRP_REMOVAL_POLICY, &dwDataType, (PBYTE)&dwRemovalPolicy, sizeof(dwRemovalPolicy), &dwSize))
{
if(CM_REMOVAL_POLICY_EXPECT_NO_REMOVAL != dwRemovalPolicy)
{
RemovableDeviceInfo devInfo;
devInfo.Clear();
devInfo.wsDeviceDesc = GetDeviceRegistryProperty(hDevInfo, &devInfoData, SPDRP_DEVICEDESC );
devInfo.wsEnumeratorName = GetDeviceRegistryProperty(hDevInfo, &devInfoData, SPDRP_ENUMERATOR_NAME);
devInfo.wsCompatibleIDs = GetDeviceRegistryProperty(hDevInfo, &devInfoData, SPDRP_COMPATIBLEIDS );
devInfo.wsHardwareID = GetDeviceRegistryProperty(hDevInfo, &devInfoData, SPDRP_HARDWAREID );
devInfo.wsMFG = GetDeviceRegistryProperty(hDevInfo, &devInfoData, SPDRP_MFG );
devInfo.wsFriendlyName = GetDeviceRegistryProperty(hDevInfo, &devInfoData, SPDRP_FRIENDLYNAME );
devInfo.wsDevInterfaceVolume = GetDevInterfaceVolume(&devInfoData);
devInfo.deviceType = GetDeviceType (&devInfoData);
vctDevice.push_back(devInfo);
}
}
dwCount++;
} while (true);
::SetupDiDestroyDeviceInfoList(hDevInfo);
return true;
}
catch(...)
{
::SetupDiDestroyDeviceInfoList(hDevInfo);
throw;
}
return false;
/*
try
{
//std::wstring name;
//RemovableDeviceInfo devInfo;
SP_DEVINFO_DATA devInfoData;
devInfoData.cbSize = sizeof(devInfoData);
for(DWORD dwCount = 0; ::SetupDiEnumDeviceInfo(hDevInfo, dwCount, &devInfoData); ++dwCount)
{
DWORD dwSize = 0;
DWORD dwDataType = 0;
DWORD dwRemovalPolicy = 0;
// [Warning]: only Windows XP and later
if(TRUE == ::SetupDiGetDeviceRegistryProperty(hDevInfo, &devInfoData, SPDRP_REMOVAL_POLICY, &dwDataType, (PBYTE)&dwRemovalPolicy, sizeof(dwRemovalPolicy), &dwSize))
{
if(CM_REMOVAL_POLICY_EXPECT_NO_REMOVAL != dwRemovalPolicy)
{
RemovableDeviceInfo devInfo;
devInfo.wsDeviceDesc = GetDeviceRegistryProperty(hDevInfo, &devInfoData, SPDRP_DEVICEDESC );
devInfo.wsEnumeratorName = GetDeviceRegistryProperty(hDevInfo, &devInfoData, SPDRP_ENUMERATOR_NAME);
devInfo.wsCompatibleIDs = GetDeviceRegistryProperty(hDevInfo, &devInfoData, SPDRP_COMPATIBLEIDS );
devInfo.wsHardwareID = GetDeviceRegistryProperty(hDevInfo, &devInfoData, SPDRP_HARDWAREID );
devInfo.wsMFG = GetDeviceRegistryProperty(hDevInfo, &devInfoData, SPDRP_MFG );
devInfo.wsFriendlyName = GetDeviceRegistryProperty(hDevInfo, &devInfoData, SPDRP_FRIENDLYNAME );
devInfo.wsDevInterfaceVolume = GetDevInterfaceVolume(&devInfoData);
//devInfo.wsPath = GetDevicePath (&devInfoData);///masked by hupf
devInfo.deviceType = GetDeviceType (&devInfoData);
vctDevice.push_back(devInfo);///added by hupf
//std::wstring diskPath(L"\\\\.\\");
//diskPath += devInfo.wsPath[0];
//diskPath += L":";
//std::vector<unsigned int> divIndexes = GetHarddiskIndexes(devInfo.wsDevInterfaceVolume);
//std::vector<unsigned int> pathIndexes = GetHarddiskIndexes(diskPath);
//if(false == pathIndexes.empty())
//{
// devInfo.vHarddiskIndexes = pathIndexes;
//}
//else
//{
// devInfo.vHarddiskIndexes = divIndexes;
//}
//if(!devInfo.vHarddiskIndexes.empty())
//{
// try
// {
// devInfo.diskGeometry = GetDeviceGeometry(devInfo.wsDevInterfaceVolume);
// vctDevice.push_back(devInfo);
// }
// catch(const std::runtime_error& ex)
// {
// ex.what();
// Suppressing GetDeviceGeometry error
// }
//}
}
}
}
::SetupDiDestroyDeviceInfoList(hDevInfo);
return true;
}
catch(...)
{
// Catch exception in SearchRemovableDevicePath
::SetupDiDestroyDeviceInfoList(hDevInfo);
throw;
}
return false;
*/
}