gpt4 book ai didi

c - 如何获取卷GUID路径所属的物理设备?

转载 作者:行者123 更新时间:2023-11-30 14:40:05 27 4
gpt4 key购买 nike

我需要使用 winapi 将卷 GUID 路径转换为物理驱动器路径。

我使用 FindFirstVolume/FindNextVolume 列出了我的电脑上的卷,并得到以下输出:

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;
}

最佳答案

嗯,我在 DiskDrive 中找到了答案类(class)。它代表运行 Windows 操作系统的计算机所看到的物理磁盘驱动器。

关于c - 如何获取卷GUID路径所属的物理设备?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55710326/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com