char *CUFormatDlg::GetDeviceName(int device)
{
PSTORAGE_DEVICE_DESCRIPTOR pDevDesc;
STORAGE_PROPERTY_QUERY Query; // 查询输入参数
DWORD dwOutBytes; // IOCTL输出长度
BOOL bResult; // IOCTL 返回值
PUCHAR p;
UCHAR outBuf[512];
char dev_name[128];
char DeviceName[128];
sprintf(dev_name,"\\\\.\\PhysicalDrive%d",device);
HANDLE hDevice = OpenDevice(dev_name);
if (hDevice == INVALID_HANDLE_VALUE)
{
return NULL;
}
// 设置查询参数
Query.PropertyId = StorageDeviceProperty;
Query.QueryType = PropertyStandardQuery;
// 设置查询参数
bResult = ::DeviceIoControl(hDevice, // 设备句柄
IOCTL_STORAGE_QUERY_PROPERTY, // 设备属性信息
&Query, sizeof(STORAGE_PROPERTY_QUERY), // 输出buffer
outBuf, 512, // 输出buffer
& dwOutBytes, // 输出长度
(LPOVERLAPPED)NULL);
pDevDesc=(PSTORAGE_DEVICE_DESCRIPTOR)outBuf;
p = (PUCHAR) outBuf;
int index=0;
//获取VerdorID
if ( pDevDesc->VendorIdOffset && p[pDevDesc->VendorIdOffset] )
{
for(int i=pDevDesc->VendorIdOffset; p[i] != (UCHAR)NULL && i<(int)dwOutBytes; i++ )
{
DeviceName[index]=p[i];
index++;
if(p[i]==' ' && p[i+1]==' ') break;//去掉多余空格
}
}
//获取ProductID
if ( pDevDesc->ProductIdOffset && p[pDevDesc->ProductIdOffset] )
{
for(int i=pDevDesc->ProductIdOffset; p[i] != (UCHAR)NULL && i<(int)dwOutBytes; i++ )
{
DeviceName[index]=p[i];
index++;
if(p[i]==' ' && p[i+1]==' ') break;//去掉多余空格
}
}
DeviceName[index]='\0';
CloseHandle(hDevice);
return DeviceName;
}
{
PSTORAGE_DEVICE_DESCRIPTOR pDevDesc;
STORAGE_PROPERTY_QUERY Query; // 查询输入参数
DWORD dwOutBytes; // IOCTL输出长度
BOOL bResult; // IOCTL 返回值
PUCHAR p;
UCHAR outBuf[512];
char dev_name[128];
char DeviceName[128];
sprintf(dev_name,"\\\\.\\PhysicalDrive%d",device);
HANDLE hDevice = OpenDevice(dev_name);
if (hDevice == INVALID_HANDLE_VALUE)
{
return NULL;
}
// 设置查询参数
Query.PropertyId = StorageDeviceProperty;
Query.QueryType = PropertyStandardQuery;
// 设置查询参数
bResult = ::DeviceIoControl(hDevice, // 设备句柄
IOCTL_STORAGE_QUERY_PROPERTY, // 设备属性信息
&Query, sizeof(STORAGE_PROPERTY_QUERY), // 输出buffer
outBuf, 512, // 输出buffer
& dwOutBytes, // 输出长度
(LPOVERLAPPED)NULL);
pDevDesc=(PSTORAGE_DEVICE_DESCRIPTOR)outBuf;
p = (PUCHAR) outBuf;
int index=0;
//获取VerdorID
if ( pDevDesc->VendorIdOffset && p[pDevDesc->VendorIdOffset] )
{
for(int i=pDevDesc->VendorIdOffset; p[i] != (UCHAR)NULL && i<(int)dwOutBytes; i++ )
{
DeviceName[index]=p[i];
index++;
if(p[i]==' ' && p[i+1]==' ') break;//去掉多余空格
}
}
//获取ProductID
if ( pDevDesc->ProductIdOffset && p[pDevDesc->ProductIdOffset] )
{
for(int i=pDevDesc->ProductIdOffset; p[i] != (UCHAR)NULL && i<(int)dwOutBytes; i++ )
{
DeviceName[index]=p[i];
index++;
if(p[i]==' ' && p[i+1]==' ') break;//去掉多余空格
}
}
DeviceName[index]='\0';
CloseHandle(hDevice);
return DeviceName;
}