如何获取卷 GUID 路径所属的物理设备?
How to get the physical device that a volume GUID path belongs to?
我需要使用 winapi
.
将卷 GUID 路径转换为物理驱动器路径
我使用 FindFirstVolume
/FindNextVolume
列出了我 PC 上的卷,我得到了以下输出:
First volume found: \?\Volume{42f73c69-4b40-11e9-a0b2-806e6f6e6963}\
Found next volume: \?\Volume{8aef5fee-0000-0000-0000-100000000000}\
Found next volume: \?\Volume{8aef5fee-0000-0000-0000-90c30b000000}\
Found next volume: \?\Volume{8aef5fee-0000-0000-0000-501f1e000000}\
Found next volume: \?\Volume{ec716472-e587-11e8-a031-806e6f6e6963}\
Search finished: [18] There are no more files.
我需要知道每个卷所属的物理设备,比如\.\PHYSICALDRIVE1
有我的代码:
/* This program attempts to list the volumes and get the corresponding physical device for each one */
#include <stdio.h>
#include <windows.h>
HANDLE openDevice(const char *deviceID);
BOOL getAllVolumes();
/* Main function
* Access the device
* Get all volume GUID paths
* Close handler
*/
int main()
{
char *deviceID = "\\.\PHYSICALDRIVE1";
HANDLE hDevice = openDevice(deviceID);
if (hDevice != INVALID_HANDLE_VALUE) { /* success */
fprintf(stderr, "Device %s opened.\n", deviceID);
/* find all volumes in the device and get the corresponding device to each one */
BOOL result = getAllVolumes();
CloseHandle(hDevice); /* close the handler to the device */
}
return 0;
}
HANDLE openDevice(const char *deviceID)
{
/* Access the device */
HANDLE hDevice = CreateFileA(
deviceID, /* device id (get it with `$ wmic DISKDRIVE`) */
FILE_READ_DATA | FILE_WRITE_DATA, /* read/write access */
FILE_SHARE_READ | FILE_SHARE_WRITE, /* shared */
NULL, /* default security attributes */
OPEN_EXISTING, /* only open if the device exists */
0, /* file attributes */
NULL); /* do not copy file attributes */
if (hDevice == INVALID_HANDLE_VALUE) { /* cannot open the physical drive */
DWORD errorCode = GetLastError();
fprintf(stderr, "Cannot open the device %s: [%lu]\n", deviceID, errorCode); /* print the error code and message */
}
return hDevice;
}
BOOL getAllVolumes()
{
char volumePath[512] = {0};
fprintf(stderr, "Attempt to find first volume...\n");
HANDLE searchHandlerDevice = FindFirstVolume(volumePath, sizeof(volumePath));
fprintf(stderr, "Done. Retrieving results...\n");
if (searchHandlerDevice == INVALID_HANDLE_VALUE) {
DWORD errorCode = GetLastError();
fprintf(stderr, "Cannot find a volume: [%lu]\n", errorCode); /* print the error code and message */
return FALSE;
} else {
fprintf(stdout, "First volume found: %s\n", volumePath);
}
/* find the other volumes */
while (FindNextVolume(searchHandlerDevice, volumePath, sizeof(volumePath))) {
fprintf(stdout, "Found volume: %s\n", volumePath);
}
/* searching failed */
DWORD errorCode = GetLastError();
fprintf(stderr, "Search failed: [%lu]\n", errorCode); /* print the error code and message */
/* close the search */
BOOL result = FindVolumeClose(searchHandlerDevice);
if (!result) {
fprintf(stderr, "Search failed: [%lu]\n", errorCode); /* print the error code */
}
return result;
}
嗯,我在DiskDriveclass中找到了答案。它表示计算机 运行 Windows 操作系统所见的物理磁盘驱动器。
我需要使用 winapi
.
我使用 FindFirstVolume
/FindNextVolume
列出了我 PC 上的卷,我得到了以下输出:
First volume found: \?\Volume{42f73c69-4b40-11e9-a0b2-806e6f6e6963}\
Found next volume: \?\Volume{8aef5fee-0000-0000-0000-100000000000}\
Found next volume: \?\Volume{8aef5fee-0000-0000-0000-90c30b000000}\
Found next volume: \?\Volume{8aef5fee-0000-0000-0000-501f1e000000}\
Found next volume: \?\Volume{ec716472-e587-11e8-a031-806e6f6e6963}\
Search finished: [18] There are no more files.
我需要知道每个卷所属的物理设备,比如\.\PHYSICALDRIVE1
有我的代码:
/* This program attempts to list the volumes and get the corresponding physical device for each one */
#include <stdio.h>
#include <windows.h>
HANDLE openDevice(const char *deviceID);
BOOL getAllVolumes();
/* Main function
* Access the device
* Get all volume GUID paths
* Close handler
*/
int main()
{
char *deviceID = "\\.\PHYSICALDRIVE1";
HANDLE hDevice = openDevice(deviceID);
if (hDevice != INVALID_HANDLE_VALUE) { /* success */
fprintf(stderr, "Device %s opened.\n", deviceID);
/* find all volumes in the device and get the corresponding device to each one */
BOOL result = getAllVolumes();
CloseHandle(hDevice); /* close the handler to the device */
}
return 0;
}
HANDLE openDevice(const char *deviceID)
{
/* Access the device */
HANDLE hDevice = CreateFileA(
deviceID, /* device id (get it with `$ wmic DISKDRIVE`) */
FILE_READ_DATA | FILE_WRITE_DATA, /* read/write access */
FILE_SHARE_READ | FILE_SHARE_WRITE, /* shared */
NULL, /* default security attributes */
OPEN_EXISTING, /* only open if the device exists */
0, /* file attributes */
NULL); /* do not copy file attributes */
if (hDevice == INVALID_HANDLE_VALUE) { /* cannot open the physical drive */
DWORD errorCode = GetLastError();
fprintf(stderr, "Cannot open the device %s: [%lu]\n", deviceID, errorCode); /* print the error code and message */
}
return hDevice;
}
BOOL getAllVolumes()
{
char volumePath[512] = {0};
fprintf(stderr, "Attempt to find first volume...\n");
HANDLE searchHandlerDevice = FindFirstVolume(volumePath, sizeof(volumePath));
fprintf(stderr, "Done. Retrieving results...\n");
if (searchHandlerDevice == INVALID_HANDLE_VALUE) {
DWORD errorCode = GetLastError();
fprintf(stderr, "Cannot find a volume: [%lu]\n", errorCode); /* print the error code and message */
return FALSE;
} else {
fprintf(stdout, "First volume found: %s\n", volumePath);
}
/* find the other volumes */
while (FindNextVolume(searchHandlerDevice, volumePath, sizeof(volumePath))) {
fprintf(stdout, "Found volume: %s\n", volumePath);
}
/* searching failed */
DWORD errorCode = GetLastError();
fprintf(stderr, "Search failed: [%lu]\n", errorCode); /* print the error code and message */
/* close the search */
BOOL result = FindVolumeClose(searchHandlerDevice);
if (!result) {
fprintf(stderr, "Search failed: [%lu]\n", errorCode); /* print the error code */
}
return result;
}
嗯,我在DiskDriveclass中找到了答案。它表示计算机 运行 Windows 操作系统所见的物理磁盘驱动器。