1:首先找到系统所在分区 ,使用函数为
UINT GetWindowsDirectory(
LPTSTR lpBuffer, // address of buffer for Windows directory
UINT uSize // size of directory buffer
);
获取哪一个分区是系统分区。
2:根据分区获取该分区对应的物理磁盘
HANDLE hDevice;
DWORD bytesReturned;
char rawDiskName[] = ".//C:";//假定是c分区
hDevice = CreateFile(rawDiskName, GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if (hDevice == INVALID_HANDLE_VALUE) return FALSE;
STORAGE_DEVICE_NUMBER deviceInfo;
BOOL retcode=DeviceIoControl(hDevice,
IOCTL_STORAGE_GET_DEVICE_NUMBER,
NULL,
0,
&deviceInfo,
sizeof(deviceInfo),
&bytesReturned,
NULL);
if(retcode==FALSE)
{
CloseHandle(hDevice);
return FALSE;
}
*diskNumberPtr = deviceInfo.DeviceNumber;
第二种方法:
例如: F:是光驱,如果获取该光驱是连在IDE0上还是IDE1上?是该IDE的主盘还是从盘?
---------------------------------------------------------------
typedef DWORD DEVICE_TYPE ;
typedef struct _STORAGE_DEVICE_NUMBER {
DEVICE_TYPE DeviceType;
ULONG DeviceNumber;
ULONG PartitionNumber;
} STORAGE_DEVICE_NUMBER, *PSTORAGE_DEVICE_NUMBER;
BOOL GetDiskAndPartitionNumbers (char driveLetter, DWORD *diskNumberPtr,
DWORD *partitionNumberPtr)
{//I found this in a message from "Jacob Lorch", dated September 10, 1999:
HANDLE fileHandle;
STORAGE_DEVICE_NUMBER deviceInfo;
DWORD bytesReturned;
char rawDiskName[] = ".//C:";
// Open the raw disk
rawDiskName[4] = driveLetter;
fileHandle = CreateFile(rawDiskName, GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if (fileHandle == INVALID_HANDLE_VALUE) return FALSE;
// Get disk and partition number information using a device control
// request
if (!DeviceIoControl(fileHandle, IOCTL_STORAGE_GET_DEVICE_NUMBER, NULL,
0, &deviceInfo, sizeof(deviceInfo), &bytesReturned,
NULL))
return FALSE;
if (bytesReturned != sizeof(deviceInfo))
return FALSE;
// Put values into the passed pointers
*diskNumberPtr = deviceInfo.DeviceNumber;
*partitionNumberPtr = deviceInfo.PartitionNumber;
char msgbuf[128];
sprintf(msgbuf,"%d %d/n", deviceInfo.DeviceNumber, deviceInfo.PartitionNumber);
OutputDebugString(msgbuf);
// Close the handle to the raw disk and return success
CloseHandle(fileHandle);
return TRUE;
}