我通过使用setup api系列函数,枚举得到几个磁盘名字,比如:Hitachi HTS542516K9SA00,Kingston DateTraverl2.0 USB Device,....然后我想使用CreateFile以"\\PhysicalDriveX"打开某个磁盘,如何确定他们之间的关系啊,比如\\PhysicalDrive0就是Hitachi HTS542516K9SA00
解决方案 »
- Radon变换源代码疑惑
- 几个对话框之间怎么传递数据
- 在WINDOWS 局域网内如何访问没有共享的打印机以及控制打印机?
- VC++本身的问题
- 如何用ADO连接带有密码的ACCESS数据库
- 第一次执行SetEvent(handle)后,WaitForSingleObject的返回值是WAIT_OBJECT_0,而第二次却不是,为什么?
- 数据库需要并发控制吗?
- 大家快看这段代码,是否VC的Bug!!!!!!!可以试一试,很简单的!
- wordXP的符号栏被禁用了
- 如何在程序里实现发送短消息的功能?电信有没有提供这样的服务?
- 为什么用bitmap.GetBitmap(&bmp)就会报错 Debug assertion failed
- 为什么总是Ex APIs are not present
再调用
BOOL DeviceIoControl(
(HANDLE) hDevice, // handle to device
IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, // dwIoControlCode
NULL, // lpInBuffer
0, // nInBufferSize
(LPVOID) lpOutBuffer, // output buffer
(DWORD) nOutBufferSize, // size of output buffer
(LPDWORD) lpBytesReturned, // number of bytes returned
(LPOVERLAPPED) lpOverlapped // OVERLAPPED structure
);反回的结构
typedef struct _VOLUME_DISK_EXTENTS {
ULONG NumberOfDiskExtents;
DISK_EXTENT Extents[1];
} VOLUME_DISK_EXTENTS, *PVOLUME_DISK_EXTENTS;typedef struct _DISK_EXTENT { DWORD DiskNumber; LARGE_INTEGER StartingOffset; LARGE_INTEGER ExtentLength;
} DISK_EXTENT, DiskNumber 就是这个盘符,所在的磁盘了
IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS这个是打开盘符吧,我想要物理设备\\PhysicalDrive跟物理设备名字的对应关系!!
DeviceID 得到 "\\PhysicalDrive0"
Caption 得到名字如我的是 "ST3160815AS"如果需要更详细的信息, 可以获取每个实例关联类如 Win32_PnPEntity....等关联类WQL语句sprintf(szWql, "ASSOCIATORS OF {%s} WHERE AssocClass = %s", (CHAR *)bStrPath, pAssocClassName);
用SetupDiGetDeviceRegistryProperty 函数 使用 SPDRP_LOCATION_INFORMATION 参数就能查询磁盘所在位置,组合成\\PhysicalDriveX即可比如,如下代码
#include <stdio.h>
#include <windows.h>
#include <setupapi.h>
#include <devguid.h>
#include <regstr.h>
#include <TCHAR.h>#pragma comment (lib,"setupapi")const TCHAR szDevClass[] = "DiskDrive";int main( int argc, char *argv[ ], char *envp[ ] )
{
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData;
DWORD i;
DWORD PropertyID = SPDRP_CLASS;
bool isdevflag = false;
// Create a HDEVINFO with all present devices.
hDevInfo = SetupDiGetClassDevs(NULL,
0, // Enumerator
0,
DIGCF_PRESENT | DIGCF_ALLCLASSES );
if (hDevInfo == INVALID_HANDLE_VALUE)
{
// Insert error handling here.
return 1;
}
// Enumerate through all devices in Set.
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i,
&DeviceInfoData);i++)
{
DWORD DataT;
LPTSTR buffer = NULL;
DWORD buffersize = 0;
DWORD dwErrCode;
//
// Call function with null to begin with,
// then use the returned buffer size
// to Alloc the buffer. Keep calling until
// success or an unknown failure.
//
Nxt:
while (!SetupDiGetDeviceRegistryProperty(
hDevInfo,
&DeviceInfoData,
PropertyID,
&DataT,
(PBYTE)buffer,
buffersize,
&buffersize))
{
if ((dwErrCode = GetLastError() ) ==
ERROR_INSUFFICIENT_BUFFER)
{
// Change the buffer size.
if (buffer) LocalFree(buffer);
buffer = (TCHAR *)LocalAlloc(LPTR,buffersize);
}
else if ( ERROR_INVALID_DATA == dwErrCode )
{
printf("SetupDiGetDeviceRegistryProperty UnSupported\n");
goto Exit;
}
else
{
// Insert error handling here.
printf("SetupDiGetDeviceRegistryProperty Error : [%ld]\n",dwErrCode);
goto Exit;
}
}
//是磁盘驱动器?
if ( _tcscmp( szDevClass, buffer ) == 0 )
{
PropertyID = SPDRP_LOCATION_INFORMATION ;
if (buffer)
{
LocalFree(buffer);
buffer = NULL;
}
buffersize = 0;
isdevflag = true;
goto Nxt;
}
else if ( isdevflag )
{
printf("DevName:\\\\.\\PhysicalDrive%s\n",buffer);
if (buffer)
{
LocalFree(buffer);
buffer = NULL;
}
buffersize = 0;
isdevflag = false;
} PropertyID = SPDRP_CLASS;
}
if ( GetLastError()!=NO_ERROR &&
GetLastError()!=ERROR_NO_MORE_ITEMS )
{
// Insert error handling here.
return 1;
}Exit:
// Cleanup
SetupDiDestroyDeviceInfoList(hDevInfo);
return 0;
}