gpt4 book ai didi

c - GetDiskFreeSpace 函数返回 ERROR_INVALID_FUNCTION

转载 作者:太空宇宙 更新时间:2023-11-04 08:24:07 29 4
gpt4 key购买 nike

我有以下代码,它应该获取外部驱动器的句柄并获取扇区大小。

HANDLE hRawDisk = CreateFile(L"\\\\.\\F:",
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_FLAG_NO_BUFFERING,
NULL);

if (hRawDisk == INVALID_HANDLE_VALUE)
{
printf("CreateFile failed\r\n");
PrintLastError();
return -1;
}

DWORD sectorsPerCluster;
DWORD bytesPerSector;
DWORD numberOfFreeClusters;
DWORD totalNumberOfClusters;

if (!GetDiskFreeSpace(L"\\\\.\\F:", &sectorsPerCluster, &bytesPerSector,
&numberOfFreeClusters, &totalNumberOfClusters))
{
printf("GetDiskFreeSpace failed\r\n");
PrintLastError();
CloseHandle(hRawDisk);
return -2;
}

我不知道为什么函数 GetDiskFreeSpace 返回 ERROR_INVALID_FUNCTION。它可能与访问权限有关吗?

使用函数 GetDiskFreeSpaceEx 给出完全相同的结果。

最佳答案

引用函数 GetDiskFreeSpace 的 MSDN 文档,关于第一个参数(https://msdn.microsoft.com/en-us/library/windows/desktop/aa364935(v=vs.85).aspx):

The root directory of the disk for which information is to be returned. If this parameter is NULL, the function uses the root of the current disk. If this parameter is a UNC name, it must include a trailing backslash (for example, "\MyServer\MyShare\"). Furthermore, a drive specification must have a trailing backslash (for example, "C:\"). The calling application must have FILE_LIST_DIRECTORY access rights for this directory.

因此,例如,您可以按以下方式调用该函数:

GetDiskFreeSpace(L"F:\\", &sectorsPerCluster, &bytesPerSector,
&numberOfFreeClusters, &totalNumberOfClusters)

关于c - GetDiskFreeSpace 函数返回 ERROR_INVALID_FUNCTION,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31683764/

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