参考:DeviceIoControl 函数详细解析
BOOL WINAPI DeviceIoControl(
_In_ HANDLE hDevice,
_In_ DWORD dwIoControlCode,
_In_opt_ LPVOID lpInBuffer,
_In_ DWORD nInBufferSize,
_Out_opt_ LPVOID lpOutBuffer,
_In_ DWORD nOutBufferSize,
_Out_opt_ LPDWORD lpBytesReturned,
_Inout_opt_ LPOVERLAPPED lpOverlapped
);
- @param
hDevice
:设备句柄,通过CreateFile获得 - @param
dwIoControlCode
:控制码,每个控制码都提供了lpInBuffer,nInBufferSize,lpOutBuffer和nOutBufferSize参数的使用细节 - @param
lpInBuffer
,nInBufferSize
,lpOutBuffer
,nOutBufferSize
根据控制码不同 - lpBytesReturned 返回实际数据空间的大小,如果
lpOutBuffer
缓冲区大于实际缓冲区则lpBytesReturned 为实际缓冲区大小,如果lpOutBuffer
缓冲区小于实际缓冲区则lpBytesReturned为lpOutBuffer
缓冲区大小 - @param
lpOverlapped
基本不使用,直接NULL
通过CreateFile
打开设备驱动程序的句柄时,应指定FILE_SHARE_READ
和FILE_SHARE_WRITE
访问标志。但是,当您打开通信资源(如串行端口)时,必须指定独占访问权限。FileName通常以如下形式:
文件名称 | 含义 |
---|---|
\\.\PhysicalDriveX | 获取物理驱动器X的信息,X以下标0开始计算 |
\\.\X: | 获取逻辑分区X的信息,例如C盘:\\.\C: |
bool getDiskDesc(int index)
{
TCHAR szDrvName[MAX_PATH] = _T("");
_stprintf_s<MAX_PATH>(szDrvName, _T("\\\\.\\PhysicalDrive%d"), index);
HANDLE hDevice = NULL;
try
{
hDevice = CreateFile(szDrvName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL);
if (hDevice == INVALID_HANDLE_VALUE)
{
return false;
}
DWORD dwOutBytes = 0;
STORAGE_PROPERTY_QUERY Query;
Query.PropertyId = StorageDeviceProperty;
Query.QueryType = PropertyStandardQuery;
std::string buf(1024, 0);
BOOL bResult = ::DeviceIoControl(hDevice, IOCTL_STORAGE_QUERY_PROPERTY, &Query, sizeof(Query), (char *)buf.data(), buf.capacity() - 1, &dwOutBytes, NULL);
if (bResult)
{
PSTORAGE_DEVICE_DESCRIPTOR pDevDesc = (PSTORAGE_DEVICE_DESCRIPTOR)buf.data();
if (pDevDesc->Size >= buf.capacity())
{
buf.resize(pDevDesc->Size + 1);
bResult = ::DeviceIoControl(hDevice, IOCTL_STORAGE_QUERY_PROPERTY, &Query, sizeof(Query), (char *)buf.data(), buf.capacity() - 1, &dwOutBytes, NULL);
if (!bResult)
{
CloseHandle(hDevice);
return false;
}
pDevDesc = (PSTORAGE_DEVICE_DESCRIPTOR)buf.data();
}
STORAGE_BUS_TYPE bsutype = pDevDesc->BusType;
//char *SerialNumber = &buf[pDevDesc->SerialNumberOffset];
//char *Vendor = &buf[pDevDesc->VendorIdOffset];
if (pDevDesc->ProductIdOffset > 0)
{
char *ProductId = &buf[pDevDesc->ProductIdOffset];
printf("DISK pId:%s,type:%d\r\n", ProductId, bsutype);
}
CloseHandle(hDevice);
return true;
}
}
catch (...)
{
}
CloseHandle(hDevice);
return false;
}
枚举所有物理驱动器信息:
int nindex = 0;
while (getDiskDesc(nindex++)){
};
type 7:表示usb设备
今天的文章获取硬盘信息失败怎么办_pe系统检测不到硬盘分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/58904.html