How to list physical disks in Windows?
In order to obtain a list of "\\\\.\PhysicalDrive0"
available.
15 Answers
WMIC
wmic
is a very complete tool
wmic diskdrive list
provide a (too much) detailed list, for instance
for less info
wmic diskdrive list brief
#C Sebastian Godelet mentions in the comments:
In C:
system("wmic diskdrive list");
As commented, you can also call the WinAPI, but... as shown in "How to obtain data from WMI using a C Application?", this is quite complex (and generally done with C++, not C).
PowerShell
Or with PowerShell:
Get-WmiObject Win32_DiskDrive
Update Feb. 2022, Microsoft announces in "Windows 10 features we're no longer developing"
The WMIC tool is deprecated in Windows 10, version 21H1 and the 21H1 General Availability Channel release of Windows Server.
This tool is superseded by Windows PowerShell for WMI.
Note: This deprecation only applies to the command-line management tool. WMI itself is not affected.
-
17-1 Does not answer the question, which is asking for how to do it in C. Commented Feb 16, 2012 at 13:26
-
13+1 Does not answer the question, but it is a very useful piece of information :-) Commented Jul 27, 2012 at 7:53
-
8
-
You can also use WMI via WinApi, not just calling wmic app.– Alex P.Commented Jan 22, 2019 at 21:30
-
Win32_DiskDrive does not list a physical disk when software raid or StorageSpaces enabled. Original physical disks got filtered out. Easy to compare with PowerShell Get-PhysicalDisk Commented Mar 1, 2020 at 2:07
One way to do it:
Enumerate logical drives using
GetLogicalDrives
For 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 thedwIoControlCode
parameter set toIOCTL_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].DiskNumber
-
17What if there is an empty disk without any (mounted) volumes?– j_kubikCommented Jul 7, 2014 at 2:15
-
2Note that his suggested implementation of
DeviceIoControl(IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS)
call will fail if a volume spans multiple disks. In other words, you first need to askDeviceIoControl
for the size ofVOLUME_DISK_EXTENTS
struct, then allocate that much memory, and only then call it again with the allocated buffer. It works the way shown above because most volumes reside on just one disk.– ahmd0Commented Oct 24, 2015 at 21:32 -
sorry, I can`t open the "\\.\C:“ successfully by using CreateFile((_T("\\.\C:"), GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,///*FILE_FLAG_WRITE_THROUGH |*/FILE_FLAG_NO_BUFFERING, NULL); Could you figure out my problem? Commented Nov 12, 2015 at 7:25
-
@ahmd0
VOLUME_DISK_EXTENTS
holds enough memory for one extent, so you can call it like Grodriguez suggests and then check forsuccess || ERROR_MORE_DATA == GetLastError()
since we only care about the first extent anyway. Commented Mar 24, 2017 at 17:17 -
1Use 0 instead of GENERIC_READ, this will allow open disk even w/o admin privileges, but still you will be able to read meta information like disk extents.– ivan.ukrCommented Jul 12, 2018 at 14:46
This might be 5 years too late :). But as I see no answer for this yet, adding this.
We can use Setup APIs to get the list of disks ie., devices in the system implementing GUID_DEVINTERFACE_DISK
.
Once we have their device paths, we can issue IOCTL_STORAGE_GET_DEVICE_NUMBER
to construct "\\.\PHYSICALDRIVE%d"
with STORAGE_DEVICE_NUMBER.DeviceNumber
See also SetupDiGetClassDevs
function
#include <Windows.h>
#include <Setupapi.h>
#include <Ntddstor.h>
#pragma comment( lib, "setupapi.lib" )
#include <iostream>
#include <string>
using namespace std;
#define START_ERROR_CHK() \
DWORD error = ERROR_SUCCESS; \
DWORD failedLine; \
string failedApi;
#define CHK( expr, api ) \
if ( !( expr ) ) { \
error = GetLastError( ); \
failedLine = __LINE__; \
failedApi = ( api ); \
goto Error_Exit; \
}
#define END_ERROR_CHK() \
error = ERROR_SUCCESS; \
Error_Exit: \
if ( ERROR_SUCCESS != error ) { \
cout << failedApi << " failed at " << failedLine << " : Error Code - " << error << endl; \
}
int main( int argc, char **argv ) {
HDEVINFO diskClassDevices;
GUID diskClassDeviceInterfaceGuid = GUID_DEVINTERFACE_DISK;
SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
PSP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailData;
DWORD requiredSize;
DWORD deviceIndex;
HANDLE disk = INVALID_HANDLE_VALUE;
STORAGE_DEVICE_NUMBER diskNumber;
DWORD bytesReturned;
START_ERROR_CHK();
//
// Get the handle to the device information set for installed
// disk class devices. Returns only devices that are currently
// present in the system and have an enabled disk device
// interface.
//
diskClassDevices = SetupDiGetClassDevs( &diskClassDeviceInterfaceGuid,
NULL,
NULL,
DIGCF_PRESENT |
DIGCF_DEVICEINTERFACE );
CHK( INVALID_HANDLE_VALUE != diskClassDevices,
"SetupDiGetClassDevs" );
ZeroMemory( &deviceInterfaceData, sizeof( SP_DEVICE_INTERFACE_DATA ) );
deviceInterfaceData.cbSize = sizeof( SP_DEVICE_INTERFACE_DATA );
deviceIndex = 0;
while ( SetupDiEnumDeviceInterfaces( diskClassDevices,
NULL,
&diskClassDeviceInterfaceGuid,
deviceIndex,
&deviceInterfaceData ) ) {
++deviceIndex;
SetupDiGetDeviceInterfaceDetail( diskClassDevices,
&deviceInterfaceData,
NULL,
0,
&requiredSize,
NULL );
CHK( ERROR_INSUFFICIENT_BUFFER == GetLastError( ),
"SetupDiGetDeviceInterfaceDetail - 1" );
deviceInterfaceDetailData = ( PSP_DEVICE_INTERFACE_DETAIL_DATA ) malloc( requiredSize );
CHK( NULL != deviceInterfaceDetailData,
"malloc" );
ZeroMemory( deviceInterfaceDetailData, requiredSize );
deviceInterfaceDetailData->cbSize = sizeof( SP_DEVICE_INTERFACE_DETAIL_DATA );
CHK( SetupDiGetDeviceInterfaceDetail( diskClassDevices,
&deviceInterfaceData,
deviceInterfaceDetailData,
requiredSize,
NULL,
NULL ),
"SetupDiGetDeviceInterfaceDetail - 2" );
disk = CreateFile( deviceInterfaceDetailData->DevicePath,
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL );
CHK( INVALID_HANDLE_VALUE != disk,
"CreateFile" );
CHK( DeviceIoControl( disk,
IOCTL_STORAGE_GET_DEVICE_NUMBER,
NULL,
0,
&diskNumber,
sizeof( STORAGE_DEVICE_NUMBER ),
&bytesReturned,
NULL ),
"IOCTL_STORAGE_GET_DEVICE_NUMBER" );
CloseHandle( disk );
disk = INVALID_HANDLE_VALUE;
cout << deviceInterfaceDetailData->DevicePath << endl;
cout << "\\\\?\\PhysicalDrive" << diskNumber.DeviceNumber << endl;
cout << endl;
}
CHK( ERROR_NO_MORE_ITEMS == GetLastError( ),
"SetupDiEnumDeviceInterfaces" );
END_ERROR_CHK();
Exit:
if ( INVALID_HANDLE_VALUE != diskClassDevices ) {
SetupDiDestroyDeviceInfoList( diskClassDevices );
}
if ( INVALID_HANDLE_VALUE != disk ) {
CloseHandle( disk );
}
return error;
}
-
Adding another link (I didn't have enough rep to post in the answer) Setup API Functions– arunCommented Aug 12, 2013 at 9:19
-
1
-
note that these SetupAPI functions will not only list all physical drives but virtual ones, too - in fact every registered disk drive interface will be listed, methinks this may be solution to the question but it will also produce a lot of "noise data", using SetupAPI is much more complex than what is suggested in this answer Commented Apr 20, 2015 at 0:06
-
I wrote a small library called libwindevblk based on the answer above that list drives, retrieves volume names when possible and provide an api allowing to read/write simply on partitions Commented Aug 21, 2020 at 10:57
-
1@HarryMallon Actually you can use
0
instead ofGENERIC_READ
access rights. It seems to be allow the device number query and doesn't require admin. Commented May 9, 2024 at 14:44
The answer is far simpler than all the above answers. The physical drive list is actually stored in a Registry key which also gives the device mapping.
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\disk\Enum
Count is the number of PhysicalDrive# and each numbered Registry Value is the corresponding physical drive.
For example, Registry Value "0" is PhysicalDrive0. The value is the actual device PhysicalDrive0 is mapped to. The value contained here can be passed into CM_Locate_DevNode within parameter pDeviceID to use the plug and play services. This will allow you to gather a wealth of information on the device. Such as the properties from Device Manager like "Friendly Display Name" if you need a name for the drive, serial numbers and more.
There is no need for WMI services which may not be running on the system or other hackery and this functionality has been present in Windows since at least 2000 and continues to be the case in Windows 10.
-
Interesting alternative, probably more relevant than my 7+ years old answer. +1– VonCCommented May 20, 2016 at 14:43
-
Best option in my opinion, since is simple, reliable and the use of the registry is probably what developers of windows wanted when desining windows. Commented Jul 24, 2016 at 7:36
-
Far better option than my answer, +1. The last question is why does it have to contain actual information. Is it documented? At which moment Windows writes data there? Does management console use it? Commented May 17, 2017 at 12:34
-
2Excellent method, but it has one small drawback: it can't list each physical drive size, since these are not stored in the registry (WMI services do provide them though). For things like getting the manufacturer and model of each drive, it's still much better and less resource intensive, so +1 from me. I need to get the size of each drive and I'm not using this in C, so I'll have to go the WMI way. It's more or less the same story as with the physical memory, whose detailed data isn't stored in the registry either... Commented Jan 1, 2019 at 21:09
-
1Another issue with this approach - the disks numbers can be incorrect. If you have a USB sticks you can plug USB1 to make it show as Disk 1 and USB2 as Disk 2. Then unplug USB1. USB2 will remain as Disk 2, however the registry incorrectly reports it as 1. To verify, use diskpart.– ymerejCommented May 18, 2021 at 14:57
The only sure shot way to do this is to call CreateFile()
on all \\.\Physicaldiskx
where 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.
-
7
-
1Why limit to
15
? Keep enumerating till you fail. I am not sure if some device number would be skipped by the OS.– AjayCommented Dec 15, 2017 at 10:57 -
3@Ajay my best guess is in case you plug device A, plug device B, then unplug device A Commented Aug 25, 2018 at 15:16
-
diskpart jumped from
Disk 1
toDisk 3
(USB flash drive after replug) so I'm un-upvoting this answer but I can't.\\.\Physicaldisk2
exists but it isn't a drive(I don't know what it is). inHKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\disk\Enum
,2
starts withUSBSTOR\
instead ofSCSI\
. Setup API finds 0,1,3 so that works (@arun's answer)– Mr. DogeCommented Jun 21, 2022 at 15:43 -
USBSTOR\
is actually the correct drive(becauseCreateFile
below, then will list the 4 partitions), it's swapped with a SCSI that I don't know of : registry: 2 is USBSTOR, 3 is SCSI, vs Setup API (and diskpart too): 3 is USBSTOR, 2 is SCSI, this specific SCSI isn't a diskSCSI\Disk&Ven_MSFT&Prod_XVDD
– Mr. DogeCommented Jun 21, 2022 at 16:07
The only correct answer is the one by @Grodriguez, and here's a complete implementation:
#include <windows.h>
#include <iostream>
#include <bitset>
#include <vector>
using namespace std;
typedef struct _DISK_EXTENT {
DWORD DiskNumber;
LARGE_INTEGER StartingOffset;
LARGE_INTEGER ExtentLength;
} DISK_EXTENT, *PDISK_EXTENT;
typedef struct _VOLUME_DISK_EXTENTS {
DWORD NumberOfDiskExtents;
DISK_EXTENT Extents[ANYSIZE_ARRAY];
} VOLUME_DISK_EXTENTS, *PVOLUME_DISK_EXTENTS;
#define CTL_CODE(DeviceType, Function, Method, Access) \
(((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method))
#define IOCTL_VOLUME_BASE ((DWORD)'V')
#define METHOD_BUFFERED 0
#define FILE_ANY_ACCESS 0x00000000
#define IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS CTL_CODE(IOCTL_VOLUME_BASE, 0, METHOD_BUFFERED, FILE_ANY_ACCESS)
int main() {
bitset<32> drives(GetLogicalDrives());
vector<char> goodDrives;
for (char c = 'A'; c <= 'Z'; ++c) {
if (drives[c - 'A']) {
if (GetDriveType((c + string(":\\")).c_str()) == DRIVE_FIXED) {
goodDrives.push_back(c);
}
}
}
for (auto & drive : goodDrives) {
string s = string("\\\\.\\") + drive + ":";
HANDLE h = CreateFileA(
s.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, FILE_FLAG_NO_BUFFERING | FILE_FLAG_RANDOM_ACCESS, NULL
);
if (h == INVALID_HANDLE_VALUE) {
cerr << "Drive " << drive << ":\\ cannot be opened";
continue;
}
DWORD bytesReturned;
VOLUME_DISK_EXTENTS vde;
if (!DeviceIoControl(
h, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,
NULL, 0, &vde, sizeof(vde), &bytesReturned, NULL
)) {
cerr << "Drive " << drive << ":\\ cannot be mapped into physical drive";
continue;
}
cout << "Drive " << drive << ":\\ is on the following physical drives: ";
for (int i = 0; i < vde.NumberOfDiskExtents; ++i) {
cout << vde.Extents[i].DiskNumber << ' ';
}
cout << endl;
}
}
I think that installation of Windows Driver Development Kit is quite a lengthy process, so I've included the declarations one needs to use DeviceIoControl
for this task.
-
2also : redefining windows macros may just be the worst idea ever - such an application will break and stop working pretty soon. Commented Apr 19, 2015 at 23:58
-
2Like I showed in my comment to this answer you're calling
DeviceIoControl
incorrectly. You can't assume that there's only one extent. You need to askDeviceIoControl
for the size of the neededVOLUME_DISK_EXTENTS
buffer.– ahmd0Commented Oct 24, 2015 at 21:35 -
@ahmd0 I'd be glad to fix it. Could you point to the msdn page that describes such behaviour, please? (Though the way to create a disk that is situated on two extents would be OK too, as I I've just found no way to test it.) Commented Oct 25, 2015 at 18:25
-
8Not every physical drive will be associated to a logical drive and even then not every logical drive will be assigned a drive letter.– anniCommented Jul 13, 2016 at 21:23
-
3Like @anni said, filesystem devices can be attached without a drive letter. I'm programmatically mounting VHDs for a product that opens a lot of them at once, and if I didn't pass the no-drive-letter parameter, I'd run out of letters. Commented Mar 25, 2020 at 15:24
GetLogicalDrives() 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.
-
3QueryDosDevice retuens partition, not the disk itself. Single disk is split to C: and D:, Win7 x64. So: c => "\Device\HarddiskVolume2"; d => "\Device\HarddiskVolume3'" Commented Jul 25, 2012 at 12:47
Thic WMIC command combination works fine:
wmic volume list brief
-
2volumes != physical disks. This command will not list physical disks that contain zero volumes, such as uninitialized disks. (Also, although it is not a fatal problem like the previous one, the output of this command will require further processing to deduplicate ids of physical disks that contain multiple volumes.) Commented Mar 25, 2020 at 15:28
If you only need to look at the existing disks, this one will suffice:
powershell "get-physicaldisk"
-
Always write your answers with details, like how it works and if it is not working for the OP then give the reason for that, check this link for more details. stackoverflow.com/help/how-to-answer Commented Dec 5, 2020 at 7:18
Here is a new solution of doing it with doing WMI calls.
Then all you need to do is just to call :
queryAndPrintResult(L"SELECT * FROM Win32_DiskDrive", L"Name");
In Windows Powershell
Get-WmiObject -Class Win32_DiskDrive -ComputerName <COMPUTERNAME> | select Name,SerialNumber,SCSITargetID,Size
Might want to include the old A: and B: drives as you never know who might be using them! I got tired of USB drives bumping my two SDHC drives that are just for Readyboost. I had been assigning them to High letters Z: Y: with a utility that will assign drive letters to devices as you wish. I wondered.... Can I make a Readyboost drive letter A: ? YES! Can I put my second SDHC drive letter as B: ? YES!
I've used Floppy Drives back in the day, never thought that A: or B: would come in handy for Readyboost.
My point is, don't assume A: & B: will not be used by anyone for anything You might even find the old SUBST command being used!
The following code works for me on Windows 11, admin rights are not required:
char szPhysicalDrive[32] = { 0 };
for (int i = 0; i < 16; i++)
{
sprintf_s(szPhysicalDrive, "\\\\.\\PhysicalDrive%d", i);
HANDLE hPhysicalDrive = CreateFile(szPhysicalDrive, 0,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);
if (INVALID_HANDLE_VALUE == hPhysicalDrive)
{
continue;
}
printf("Physical drive '%s' found\r\n", szPhysicalDrive);
CloseHandle(hPhysicalDrive);
}
Output:
Physical drive '\\.\PhysicalDrive0' found
Physical drive '\\.\PhysicalDrive1' found
First one is an internal hard drive, second one is a USB flash drive.
I 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\MountedDevices
One 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;
-
2i 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...– CiNNCommented Dec 12, 2008 at 8:45
-
1'Fraid this isn't what we're looking for, and is similar to @Alnitak's answer. Commented Oct 28, 2010 at 10:30
-
1You'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 PCommented Dec 16, 2011 at 20:07
Make 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.