#include "StdAfx.h"
#include "EnumDisks.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)
{
AutoHandle handle( ::CreateFileW(
wsPath.c_str(),
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS,
NULL) );
VOL
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Sources.zip (61个子文件)
Sources
DeviceDetectConsole.sln 2KB
DeviceDetectLibrary
ADeviceWatcher.h 1KB
Win32Types.h 604B
stdafx.cpp 304B
Win_AutoHandle.cpp 887B
ADeviceWatcher.cpp 1KB
NotifyWindow.h 906B
Win_WaitObject.cpp 607B
C_UsbEnumerator.cpp 999B
stdafx.h 956B
EnumDevices.h 418B
ConnectionInfo.h 779B
C_UsbEnumerator.h 473B
Win_Thread.h 594B
DeviceInfo.cpp 541B
Win_Thread.cpp 1KB
NotifyWindow.cpp 5KB
Win_ManualResetEvent.h 338B
C_AUsbEnumerator.h 1KB
Win_WaitObject.h 403B
C_AUsbEnumerator.cpp 2KB
C_BlackBerryUsbEnumerator.cpp 583B
Win_AutoHandle.h 788B
EnumDevices.cpp 6KB
ResultEnumerator.h 577B
C_MSChecker.cpp 2KB
ConnectionInfo.cpp 509B
AutoCriticalSection.h 523B
C_ModemEnumeraror.cpp 1KB
EnumDisks.h 2KB
Win_ManualResetEvent.cpp 320B
DeviceDetectLibrary.vcproj 8KB
Win_CriticalSection.cpp 460B
IEnumerator.h 543B
Utilities.cpp 689B
C_MSEnumerator.h 964B
AutoCriticalSection.cpp 292B
C_ModemdeviceChecker.h 791B
DeviceWatcher.cpp 1KB
C_ModemEnumeraror.h 702B
DeviceInfo.h 2KB
C_BlackBerryUsbEnumerator.h 462B
ResultEnumerator.cpp 621B
C_MSChecker.h 851B
Win_CriticalSection.h 476B
ICollector.h 383B
C_ModemdeviceChecker.cpp 1KB
C_MSEnumerator.cpp 2KB
DeviceWatcher.h 995B
Utilities.h 318B
IDeviceChanged.h 546B
EnumDisks.cpp 16KB
IDeviceWatcherObserver.h 445B
DeviceDetectConsole
stdafx.cpp 304B
DeviceDetectConsole.cpp 492B
TestDeviceWatcherObserver.h 606B
stdafx.h 659B
DeviceDetectConsole.vcproj 5KB
TestDeviceWatcherObserver.cpp 1KB
DeviceDetectLog.h 127B
DeviceDetectLog.cpp 348B
共 61 条
- 1
windcastle
- 粉丝: 2
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
前往页