Reputation: 101
I would like to get file size of my external hard disk.
handle = CreateFile(L"\\\\.\\PhysicalDrive5", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL , NULL);
if (handle == INVALID_HANDLE_VALUE) {
std::cout<<"ERROR!"<<std::endl;
return -1;
}
LARGE_INTEGER size;
if(!GetFileSizeEx(handle, &size))
{
auto lastError = GetLastError();
std::cout<<"Last Error: "<<lastError<<std::endl;
CloseHandle(handle);
return -1;
}
When I execute my application with admin rights ( it is necessary for CreateFile ), I get Last Error: 1
.
OS: Windows 10, Compiler: MinGW 7.3.0
Upvotes: 1
Views: 456
Reputation: 15375
AFAIK, GetFileSizeEx()
is the wrong function to use for a "physical disk" object.
Use DeviceIoControl()
with IOCTL_DISK_GET_DRIVE_GEOMETRY
instead.
Upvotes: 3