How to enumerate all devices in Windows

Source: Internet
Author: User

 

In Linux, we can read/proc/partitions to obtain the names of all the hard disks and partitions in the system. However, in windows, how can we obtain the various devices in the system? Here we use the Windows API ---Findfirstvolume,

Findnextvolume
And
Findvolumeclose.

These APIs can enumerate various devices in windows.

 

The following program is taken from Microsoft's msdn example program to display the path names of all devices and volumes in the system. For each volume, the program in the example searches for each volume in the system, and the program obtains the device name and Path Name of the volume, and displays it.

 

 

# Include <windows. h> <br/> # include <stdio. h> <br/> void displayvolumepaths (<br/> _ in pwchar volumename <br/>) <br/>{< br/> DWORD charcount = max_path + 1; <br/> pwchar names = NULL; <br/> pwchar nameidx = NULL; <br/> bool success = false; <br/> (;;) <br/> {<br/> // allocate a buffer to hold the paths. <br/> names = (pwchar) New byte [charcount * sizeof (wchar)]; <br/> If (! Names) <br/>{< br/> // <br/> // If memory can't be allocated, return. <br/> return; <br/>}< br/> // <br/> // obtain all of the paths <br/> // For this volume. <br/> success = getvolumepathnamesforvolumenamew (<br/> volumename, names, charcount, & charcount <br/>); <br/> If (SUCCESS) <br/>{< br/> break; <br/>}< br/> If (getlasterror ()! = Error_more_data) <br/>{< br/> break; <br/>}< br/> // <br/> // try again with the <br/> // new suggested size. <br/> Delete [] names; <br/> names = NULL; <br/>}< br/> If (SUCCESS) <br/> {<br/> // display the varous paths. <br/> for (nameidx = names; <br/> nameidx [0]! = L'/0'; <br/> nameidx + = wcslen (nameidx) + 1) <br/>{< br/> wprintf (L "% s ", nameidx); <br/>}< br/> wprintf (L "/N"); <br/>}< br/> If (names! = NULL) <br/>{< br/> Delete [] names; <br/> names = NULL; <br/>}< br/> return; <br/>}< br/> void _ cdecl wmain (void) <br/>{< br/> DWORD charcount = 0; <br/> wchar devicename [max_path] = l ""; <br/> DWORD error = error_success; <br/> handle findhandle = invalid_handle_value; <br/> bool found = false; <br/> size_t Index = 0; <br/> bool success = false; <br/> wchar volumename [max_path] = l ""; <br/>/ /<Br/> // enumerate all volumes in the system. <br/> findhandle = findfirstvolumew (volumename, arraysize (volumename); <br/> If (findhandle = invalid_handle_value) <br/>{< br/> error = getlasterror (); <br/> wprintf (L "findfirstvolumew failed with error code % d/N", error ); <br/> return; <br/>}< br/> (;;) <br/>{< br/> // <br/> // skip //? /Prefix and remove the trailing backslash. <br/> Index = wcslen (volumename)-1; <br/> If (volumename [0]! = L' // '| <br/> volumename [1]! = L' // '| <br/> volumename [2]! = L '? '| <Br/> volumename [3]! = L' // '| <br/> volumename [Index]! = L' // ') <br/>{< br/> error = error_bad_pathname; <br/> wprintf (L "findfirstvolumew/findnextvolumew returned a bad path: % s/n ", volumename); <br/> break; <br/>}< br/> // <br/> // querydosdevicew doesn' t allow a trailing backslash, <br/> // so temporarily remove it. <br/> volumename [Index] = l'/0'; <br/> charcount = querydosdevicew (& volumename [4], devicename, arraysize (devicename )); <br/> volumenam E [Index] = l' // '; <br/> If (charcount = 0) <br/>{< br/> error = getlasterror (); <br/> wprintf (L "querydosdevicew failed with error code % d/N", error); <br/> break; <br/>}< br/> wprintf (L "/nfound a device:/n % s", devicename); <br/> wprintf (L "/nvolume Name: % s ", volumename); <br/> wprintf (L"/npaths: "); <br/> displayvolumepaths (volumename ); <br/> // move on to the next volume. <br/> succ ESS = findnextvolumew (findhandle, volumename, arraysize (volumename); <br/> If (! Success) <br/>{< br/> error = getlasterror (); <br/> If (error! = Error_no_more_files) <br/>{< br/> wprintf (L "findnextvolumew failed with error code % d/N", error); <br/> break; <br/>}< br/> // <br/> // finished iterating <br/> // through all the volumes. <br/> error = error_success; <br/> break; <br/>}< br/> findvolumeclose (findhandle ); <br/> findhandle = invalid_handle_value; <br/> return; <br/>}< br/>

 

The following is an example of program output. For each volume, output its volume device path, Volume guid path, and device characters.

Found a device:
/Device/HarddiskVolume2
Volume name: //?/Volume{4c1b02c1-d990-11dc-99ae-806e6f6e6963}/
Paths: C:/

Found a device:
/Device/CdRom0
Volume name: //?/Volume{4c1b02c4-d990-11dc-99ae-806e6f6e6963}/
Paths: D:/
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.