List of all physical drives (Windows)

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

The answer in this question involves obtaining the letter of the logical drive, and then obtaining the physical disk corresponding to this installed disk. The problem is that I want to get all the physical disks connected to the computer, including disks that are not installed.

Other answers suggest increasing the value from 0 to 15 and checking for the presence of a disk (\\. \ PhysicalDrive0, \\. \ PhysicalDrive1, ...) or calling WMIC to display all the disks. [

It seems that they will work, it seems that they are not the best approach to adoption. Is there a simple function, such as GetPhysicalDrives, that simply returns a vector std::stringcontaining the paths of all physical disks?

+6
source share
2 answers

You can use QueryDosDevice. Based on the description, you expect it to list things like C:and D:, but also things like PhysicalDrive0, PhysicalDrive1etc.

The main disadvantage is that it will also list many other device names that you probably do not need, so (for example) on my machine, I get a list of almost 600 device names, of which only a relatively small percentage is related to what you care about.

, , () :

#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;
}    

, `devlist | grep "^ Physical", .

+4

, NET USE. ...

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.

0

All Articles