windows驱动中通过volume找partition的函数
时间: 2023-09-09 09:05:44 浏览: 180
Oracle中rank,over partition函数的使用方法
5星 · 资源好评率100%
下面是一个示例函数,它使用 IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS 和 IOCTL_DISK_GET_PARTITION_INFO_EX 控制码来获取指定卷的分区信息:
```c
#include <windows.h>
#include <winioctl.h>
#include <stdio.h>
BOOL GetPartitionInfoByVolume(HANDLE hVolume, PULARGE_INTEGER pStartingOffset, PULARGE_INTEGER pPartitionSize, PWSTR pFileSystemName, DWORD cchFileSystemName)
{
BOOL bRet = FALSE;
DWORD dwBytesReturned;
STORAGE_DEVICE_NUMBER sdn;
VOLUME_DISK_EXTENTS vde;
PARTITION_INFORMATION_EX pie;
// 获取卷的设备号
if (!DeviceIoControl(hVolume, IOCTL_STORAGE_GET_DEVICE_NUMBER, NULL, 0, &sdn, sizeof(sdn), &dwBytesReturned, NULL)) {
printf("DeviceIoControl(IOCTL_STORAGE_GET_DEVICE_NUMBER) failed %d\n", GetLastError());
goto Exit;
}
// 获取卷所在的磁盘编号和偏移量
if (!DeviceIoControl(hVolume, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, NULL, 0, &vde, sizeof(vde), &dwBytesReturned, NULL)) {
printf("DeviceIoControl(IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS) failed %d\n", GetLastError());
goto Exit;
}
// 获取包含指定偏移量的分区信息
HANDLE hDisk = NULL;
WCHAR szDiskName[MAX_PATH];
_snwprintf_s(szDiskName, MAX_PATH, L"\\\\.\\PhysicalDrive%d", sdn.DeviceNumber);
hDisk = CreateFile(szDiskName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (hDisk == INVALID_HANDLE_VALUE) {
printf("CreateFile(%ws) failed %d\n", szDiskName, GetLastError());
goto Exit;
}
LARGE_INTEGER liStartingOffset;
liStartingOffset.QuadPart = pStartingOffset->QuadPart + vde.Extents[0].StartingOffset.QuadPart;
if (!DeviceIoControl(hDisk, IOCTL_DISK_GET_PARTITION_INFO_EX, &liStartingOffset, sizeof(liStartingOffset), &pie, sizeof(pie), &dwBytesReturned, NULL)) {
printf("DeviceIoControl(IOCTL_DISK_GET_PARTITION_INFO_EX) failed %d\n", GetLastError());
goto Exit;
}
// 返回分区信息
pPartitionSize->QuadPart = pie.PartitionLength.QuadPart;
wcsncpy_s(pFileSystemName, cchFileSystemName, pie.FileSystemTypeName, cchFileSystemName - 1);
bRet = TRUE;
Exit:
if (hDisk != INVALID_HANDLE_VALUE) {
CloseHandle(hDisk);
}
return bRet;
}
```
该函数需要传入一个已经打开的卷句柄和指向 ULARGE_INTEGER 类型的指针,以获取卷的起始偏移量和分区大小。函数还需要传入一个指向 WCHAR 类型的缓冲区和一个 DWORD 类型的值,以获取文件系统类型的字符串和字符串长度。如果函数调用成功,则返回 TRUE;否则返回 FALSE。
阅读全文