Wednesday, January 30, 2013

HARD DRIVE

ow to list physical disks?

c windows winapi hard-driveHow to list physical disks in windows? In order to obtain a list of "\.\PhysicalDrive0" available.improve this questionedited Nov 29 '08 at 17:56asked Nov 29 '08 at 17:18CiNN2,551●2●19●349 Answersorder by                      active                     oldest                     votes                 vote up18vote downwmic is a very complete toolwmic diskdrive listprovide a (too much) detailed list, for instancefor less infowmic diskdrive list brief improve this answeredited Jan 17 '12 at 16:07Community♦ answered Nov 29 '08 at 17:24VonC274k●51●512●657-1 Does not answer the question, which is asking for how to do it in C. – unixman83 Feb 16 '12 at 13:26+1 Does not answer the question, but it is a very useful piece of information :-) – Grodriguez Jul 27 '12 at 7:531 you could do a system("wmic diskdrive list"); in C – Sebastian Godelet Nov 21 '12 at 13:28vote up7vote downI've modified an open-source program called "dskwipe" in order to pull this disk information out of it. Dskwipe is written in C, and you can pull this function out of it. The binary and source are available here: dskwipe 0.3 has been releasedThe returned information will look something like this:Device Name Size Type Partition Type ------------------------------ --------- --------- -------------------- \\.\PhysicalDrive0 40.0 GB Fixed \\.\PhysicalDrive1 80.0 GB Fixed \Device\Harddisk0\Partition0 40.0 GB Fixed \Device\Harddisk0\Partition1 40.0 GB Fixed NTFS \Device\Harddisk1\Partition0 80.0 GB Fixed \Device\Harddisk1\Partition1 80.0 GB Fixed NTFS \\.\C: 80.0 GB Fixed NTFS \\.\D: 2.1 GB Fixed FAT32 \\.\E: 40.0 GB Fixed NTFSimprove this answeranswered Dec 8 '08 at 17:55Mick6,389●1●24●631 i thought it was it, but it force brute search for the drives..isn't there an api that will just report back the devices ? – CiNN Dec 9 '08 at 23:031 Yes. SetupApi in Win32, function names start with SetupDi – Warren P Dec 16 '11 at 20:06vote up5vote downFrom your own code, use GetLogicalDrives() first to get all of the drives mapped in the system, and then GetDriveType() to find out which sort of drive each one is.improve this answeredited Nov 29 '08 at 17:31answered Nov 29 '08 at 17:26Alnitak89.1k●10●119●1901 It is not correct because logical drives don't correspond to physical ones. – Sergius Jan 18 '12 at 11:48vote up5vote downGetLogicalDrives() enumerates all mounted disk partitions, not physical drives.You can enumerate the drive letters with (or without) GetLogicalDrives, then call QueryDosDevice() to find out which physical drive the letter is mapped to.Alternatively, you can decode the information in the registry at HKEY_LOCAL_MACHINE\SYSTEM\MountedDevices. The binary data encodings there are not obvious, however. If you have a copy of Russinovich and Solomon's book Microsoft Windows Internals, this registry hive is discussed in Chapter 10.improve this answeranswered Nov 29 '08 at 18:11Die in Sente3,315●10●24can you provide example code? – Chibueze Opata Mar 15 '12 at 14:35QueryDosDevice retuens partition, not the disk itself. Single disk is split to C: and D:, Win7 x64. So: c => "\Device\HarddiskVolume2"; d => "\Device\HarddiskVolume3'" – Arioch 'The Jul 25 '12 at 12:47vote up2vote downThe only sure shot way to do this is to call CreateFile() on all \\.\Physicaldiskxwhere x is from 0 to 15 (16 is maximum number of disks allowed). Check the returned handle value. If invalid check GetLastError() for ERROR_FILE_NOT_FOUND. If it returns anything else then the disk exists but you cannot access it for some reason.improve this answeredited Apr 19 '12 at 16:23Charles Menguy answered Apr 19 '12 at 12:26anni60●5vote up2vote downOne way to do it:Enumerate logical drives using GetLogicalDrivesFor each logical drive, open a file named "\\.\X:" (without the quotes) where X is the logical drive letter.Call DeviceIoControl passing the handle to the file opened in the previous step, and the dwIoControlCode parameter set to IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS:HANDLE hHandle; VOLUME_DISK_EXTENTS diskExtents; DWORD dwSize; [...] iRes = DeviceIoControl( hHandle, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, NULL, 0, (LPVOID) &diskExtents, (DWORD) sizeof(diskExtents), (LPDWORD) &dwSize, NULL);This returns information of the physical location of a logical volume, as a VOLUME_DISK_EXTENTS structure.In the simple case where the volume resides on a single physical drive, the physical drive number is available in diskExtents.Extents[0].DiskNumberimprove this answeranswered Jul 27 '12 at 8:09Grodriguez6,474●16●341 +1 Certainly a more accurate answer than mine ;) – VonC Jul 27 '12 at 8:14vote up1vote downI just ran across this in my RSS Reader today. I've got a cleaner solution for you. This example is in Delphi, but can very easily be converted to C/C++ (It's all Win32).Query all value names from the following registry location:HKLM\SYSTEM\MountedDevicesOne by one, pass them into the following function and you will be returned the device name. Pretty clean and simple! I found this code on a blog here.function VolumeNameToDeviceName(const VolName: String): String; var s: String; TargetPath: Array[0..MAX_PATH] of WideChar; bSucceeded: Boolean; begin Result := "; // VolumeName has a format like this: \\?\Volume{c4ee0265-bada-11dd-9cd5-806e6f6e6963}\ // We need to strip this to Volume{c4ee0265-bada-11dd-9cd5-806e6f6e6963} s := Copy(VolName, 5, Length(VolName) - 5); bSucceeded := QueryDosDeviceW(PWideChar(WideString(s)), TargetPath, MAX_PATH) <> 0; if bSucceeded then begin Result := TargetPath; end else begin // raise exception end; end;improve this answeranswered Dec 10 '08 at 16:31Mick6,389●1●24●631 i want to have the physical name so that i could play with unallocated space, so my guess it that this unallocated space wouldn't have a mounted volume guid... – CiNN Dec 12 '08 at 8:451 'Fraid this isn't what we're looking for, and is similar to @Alnitak's answer. – Matt Joiner Oct 28 '10 at 10:301 You're supposed to use SetupApi in windows xp and later, and no longer use the registry, which was the way to do it in Win98, but not any more. – Warren P Dec 16 '11 at 20:07vote up1vote downI think this is a very good sample for your question, a little late but... its validimprove this answeranswered Feb 6 '10 at 18:10Eugenio Miró741●8●151 As far as I can tell (and I've tested the code you linked too), volumes don't relate to partitions in a 1 to many fashion, and don't give the access the OP is asking for. – Matt Joiner Oct 28 '10 at 10:34This answer doesn't deserve -1. It is no worse than other answers on this page and it's in C. – unixman83 Feb 16 '12 at 13:03vote up0vote downMake a list of all letters in the US English Alphabet, skipping a & b. "CDEFGHIJKLMNOPQRSTUVWXYZ". Open each of those drives with CreateFile e.g. CreateFile("\\.\C:"). If it does not return INVALID_HANDLE_VALUE then you got a 'good' drive. Next take that handle and run it through DeviceIoControl to get the Disk #.See my related answer for more details.improve this answer

No comments:

Post a Comment