Listing All Physical Drives (Windows)
Asked Answered
M

2

7

How can I get all the physical drive paths (\\.\PhysicalDriveX) on a Windows computer, with C/C++?

The answers in this question suggest getting the logical drive letter, and then getting the physical drive corresponding to that mounted drive. The problem is, I want to get all physical drives connected to the computer, including drives that are not mounted.

Other answers suggest incrementing a value from 0-15 and checking if a drive exists there (\\.\PhysicalDrive0, \\.\PhysicalDrive1, ...) or calling WMIC to list all the drives.[

As these seem like they would work, they seem like they are not the best approach to be taking. Is there not a simple function such as GetPhysicalDrives that simply returns a vector of std::string's containing the paths of all the physical drives?

Myrtia answered 11/3, 2013 at 23:35 Comment(3)
Have you looked if boost::filesystem can help you?Metsky
I have not yet looked into boost, no. I will take a look at that if no answers not requiring another library do not come up soon.Myrtia
Drive letters exist for logical drives whether mounted or not... you still have a valid concern about physical disks with no logical drives (recognized partitions) at all.Hunnish
P
5

You can use QueryDosDevice. Based on the description, you'd expect this to list things like C: and D:, but it will also lists things like PhysicalDrive0, PhysicalDrive1 and so on.

The major shortcoming is that it will also list a lot of other device names you probably don't care about, so (for example) on my machine, I get a list of almost 600 device names, of which only a fairly small percentage is related to what you care about.

Just in case you care, some (old) sample code:

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#include <iostream>

int main(int argc, char **argv) {

    char physical[65536];
    char logical[65536];

    if ( argc > 1) {
        for (int i=1; i<argc; i++) {
            QueryDosDevice(argv[i],logical, sizeof(logical));
            std::cout << argv[i] << " : \t" << logical << std::endl << std::endl;
        }
        return 0;
    }

    QueryDosDevice(NULL, physical, sizeof(physical));

    std::cout << "devices: " << std::endl;

    for (char *pos = physical; *pos; pos+=strlen(pos)+1) {
        QueryDosDevice(pos, logical, sizeof(logical));
        std::cout << pos << " : \t" << logical << std::endl << std::endl;
    }    

    return 0;
}    

However, if I run this like `devlist | grep "^Physical", it lists the physical drives.

Pregnant answered 12/3, 2013 at 0:3 Comment(1)
wanted this for a completely different task but it's the best example i found on how to use QueryDosDevice; +1 for you sir.Individuate
F
-2

Yes, you can just type NET USE. Here is an example output...

NET USE
New connections will be remembered.


Status       Local     Remote                    Network

-------------------------------------------------------------------------------
             H:        \\romaxtechnology.com\HomeDrive\Users\Henry.Tanner
                                                Microsoft Windows Network
OK           N:        \\ukfs01.romaxtechnology.com\romaxfs
                                                Microsoft Windows Network
OK           X:        \\ukfs03.romaxtechnology.com\exchange
                                                Microsoft Windows Network
OK           Z:        \\ukfs07\Engineering      Microsoft Windows Network
                       \\romaxtechnology.com\HomeDrive
                                                Microsoft Windows Network
OK                     \\ukfs07\IPC$             Microsoft Windows Network
The command completed successfully.

Frightful answered 2/7, 2019 at 7:48 Comment(1)
Network/UNC paths have nothing to do with paths for locally-attached, physical storage devices.Northerner

© 2022 - 2024 — McMap. All rights reserved.