//遍历系统盘,找到移动设备
void GETUsbList()
{
HANDLE hDevice; // handle to the drive to be examined
BOOL result; // results flag
DWORD readed; // discard results
STORAGE_DESCRIPTOR_HEADER *pDevDescHeader;
STORAGE_DEVICE_DESCRIPTOR *pDevDesc;
DWORD devDescLength;
STORAGE_PROPERTY_QUERY query;
std::wstring wsSrcPath;
for (wchar_t d = 'A'; d <= 'Z'; d++)
{
TCHAR szTemp[2] = { d ,':'};
wsSrcPath = TChar2ws(szTemp);
if(wsSrcPath.empty())
{
return;
}
wsSrcPath = wsSrcPath.substr(0,wsSrcPath.find(L":"));
std::map<std::wstring, std::wstring>::iterator iter = mUsbNameList.find(wsSrcPath);
if(iter != mUsbNameList.end())
{
return;
}
wsSrcPath = wsSrcPath.substr(0,wsSrcPath.find(L":\\"));
std::wstring szPath = wsformat(L"\\\\.\\%s:",wsSrcPath.c_str());
hDevice = CreateFile(
szPath.c_str(), // drive to open
GENERIC_READ | GENERIC_WRITE, // access to the drive
FILE_SHARE_READ | FILE_SHARE_WRITE, //share mode
NULL, // default security attributes
OPEN_EXISTING, // disposition
0, // file attributes
NULL // do not copy file attribute
);
if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive
{
fprintf(stderr, "CreateFile() Error: %ld\n", GetLastError());
continue;
}
query.PropertyId = StorageDeviceProperty;
query.QueryType = PropertyStandardQuery;
pDevDescHeader = (STORAGE_DESCRIPTOR_HEADER *)malloc(sizeof(STORAGE_DESCRIPTOR_HEADER));
if (NULL == pDevDescHeader)
{
continue;
}
result = DeviceIoControl(
hDevice, // device to be queried
IOCTL_STORAGE_QUERY_PROPERTY, // operation to perform
&query,
sizeof query, // no input buffer
pDevDescHeader,
sizeof(STORAGE_DESCRIPTOR_HEADER), // output buffer
&readed, // # bytes returned
NULL); // synchronous I/O
if (!result) //fail
{
fprintf(stderr, "IOCTL_STORAGE_QUERY_PROPERTY Error: %ld\n", GetLastError());
free(pDevDescHeader);
(void)CloseHandle(hDevice);
continue ;
}
devDescLength = pDevDescHeader->Size;
pDevDesc = (STORAGE_DEVICE_DESCRIPTOR *)malloc(devDescLength);
if (NULL == pDevDesc)
{
free(pDevDescHeader);
continue;
}
result = DeviceIoControl(
hDevice, // device to be queried
IOCTL_STORAGE_QUERY_PROPERTY, // operation to perform
&query,
sizeof query, // no input buffer
pDevDesc,
devDescLength, // output buffer
&readed, // # bytes returned
NULL); // synchronous I/O
if (!result) //fail
{
fprintf(stderr, "IOCTL_STORAGE_QUERY_PROPERTY Error: %ld\n", GetLastError());
free(pDevDescHeader);
free(pDevDesc);
(void)CloseHandle(hDevice);
continue ;
}
if(BusTypeUsb == (WORD)pDevDesc->BusType)
{
//类型收移动设备的处理
}
else
{
continue;
}
}
return ;
}
//获取移动设备硬件信息
void GetUsbInfo(std::wstring wsSrcPath)
{
if(wsSrcPath.empty())
{
return;
}
wsSrcPath = wsSrcPath.substr(0,wsSrcPath.find(L":\\"));
std::wstring szPath = wsformat(L"\\\\.\\%s:",wsSrcPath.c_str());
HANDLE hDevice = CreateFile(szPath.c_str(), GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL);
if (hDevice != INVALID_HANDLE_VALUE)
{
PSTORAGE_DEVICE_DESCRIPTOR pDevDesc = (PSTORAGE_DEVICE_DESCRIPTOR)new BYTE[sizeof(STORAGE_DEVICE_DESCRIPTOR) + 512 - 1];
pDevDesc->Size = sizeof(STORAGE_DEVICE_DESCRIPTOR) + 512 - 1;
if(GetDisksProperty(hDevice, pDevDesc))
{
char *p= (char*)pDevDesc;
VendorId = (pDevDesc->VendorIdOffset ? &p[pDevDesc->VendorIdOffset]:"(NULL)");
ProductId=(pDevDesc->ProductIdOffset ? &p[pDevDesc->ProductIdOffset]:"(NULL)");
ProductRevision = (pDevDesc->ProductRevisionOffset ? &p[pDevDesc->ProductRevisionOffset] : "(NULL)");
SerialNumber = (pDevDesc->SerialNumberOffset ? &p[pDevDesc->SerialNumberOffset] : "(NULL)");
}
delete pDevDesc;
CloseHandle(hDevice);
}
}
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交