Reputation: 97
I have the following code, which should get a handle to an external drive and get a sector size.
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:", §orsPerCluster, &bytesPerSector,
&numberOfFreeClusters, &totalNumberOfClusters))
{
printf("GetDiskFreeSpace failed\r\n");
PrintLastError();
CloseHandle(hRawDisk);
return -2;
}
I don't know why the function GetDiskFreeSpace returns ERROR_INVALID_FUNCTION. Is it possible that it has something to do with access rights?
Using of function GetDiskFreeSpaceEx gives exactly the same results.
Upvotes: 0
Views: 3240
Reputation: 11666
sample:
auto path = _T("C:\\");
ULARGE_INTEGER FreeBytesAvailable = { 0 };
ULARGE_INTEGER TotalNumberOfBytes={ 0 };
ULARGE_INTEGER TotalNumberOfFreeBytes={ 0 };
BOOL ok = GetDiskFreeSpaceEx(
path,
&FreeBytesAvailable,
&TotalNumberOfBytes,
&TotalNumberOfFreeBytes
);
if (ok)
{
unsigned long long freeAvail = FreeBytesAvailable.QuadPart;
unsigned long long total = TotalNumberOfBytes.QuadPart;
unsigned long long totalfree = TotalNumberOfFreeBytes.QuadPart;
TRACE("\nfree for user: MB: %lld; Total for user: MB: %lld, Free total MB:%lld \n", freeAvail /1024/1024, total / 1024 / 1024, totalfree / 1024 / 1024);
/// if eventually using MFC:
CString s;
s.Format(_T("free for user: MB: %lld; Total for user: MB: %lld, Free total MB:%lld \n"), freeAvail / 1024 / 1024, total / 1024 / 1024, totalfree / 1024 / 1024);
Upvotes: 0
Reputation: 2938
Quoting the MSDN documentation for the function GetDiskFreeSpace
, about the first parameter (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.
So, for example, you'd call the function in the following way:
GetDiskFreeSpace(L"F:\\", §orsPerCluster, &bytesPerSector,
&numberOfFreeClusters, &totalNumberOfClusters)
Upvotes: 2