winapi - Get total size of a hard disk in c++ windows -
i trying total size of physical drive (unallocated + primary partitions + extended partitions size).
i have disk name \\.\physicaldrivex
i tried using getdiskfreespaceex doesn't give correct result when partition extended partition, in case returns total size of partition.
bool ret = false; ularge_integer ulfreespace; ularge_integer ultotalspace; ularge_integer ultotalfreespace; __int64 ultotalusedspace = 0; getdiskfreespaceex(szbuffer, &ulfreespace, &ultotalspace, &ultotalfreespace); *disksize = ultotalspace.quadpart; i can partition info using deviceiocontrol using ioctl_disk_get_drive_layout_ex getting confused extended partition size.
is there way can accurately total size of hard disk in c++ on windows ??
since you're talking physical disk , not partitions, take @ deviceiocontrol.
example there, includes calculation of total disk size in wmain:
#include <windows.h> #include <winioctl.h> #include <stdio.h> bool getdrivegeometry(lpwstr wszpath, disk_geometry *pdg) { handle hdevice = invalid_handle_value; // handle drive examined bool bresult = false; // results flag dword junk = 0; // discard results hdevice = createfilew(wszpath, // drive open 0, // no access drive file_share_read | // share mode file_share_write, null, // default security attributes open_existing, // disposition 0, // file attributes null); // not copy file attributes if (hdevice == invalid_handle_value) // cannot open drive { return (false); } bresult = deviceiocontrol(hdevice, // device queried ioctl_disk_get_drive_geometry, // operation perform null, 0, // no input buffer pdg, sizeof(*pdg), // output buffer &junk, // # bytes returned (lpoverlapped) null); // synchronous i/o closehandle(hdevice); return (bresult); } int wmain(int argc, wchar_t *argv[]) { disk_geometry pdg = { 0 }; // disk drive geometry structure bool bresult = false; // generic results flag ulonglong disksize = 0; // size of drive, in bytes bresult = getdrivegeometry (wszdrive, &pdg); if (bresult) { wprintf(l"drive path = %ws\n", wszdrive); wprintf(l"cylinders = %i64d\n", pdg.cylinders); wprintf(l"tracks/cylinder = %ld\n", (ulong) pdg.trackspercylinder); wprintf(l"sectors/track = %ld\n", (ulong) pdg.sectorspertrack); wprintf(l"bytes/sector = %ld\n", (ulong) pdg.bytespersector); disksize = pdg.cylinders.quadpart * (ulong)pdg.trackspercylinder * (ulong)pdg.sectorspertrack * (ulong)pdg.bytespersector; wprintf(l"disk size = %i64d (bytes)\n" l" = %.2f (gb)\n", disksize, (double) disksize / (1024 * 1024 * 1024)); } else { wprintf (l"getdrivegeometry failed. error %ld.\n", getlasterror ()); } return ((int)bresult); }
Comments
Post a Comment