Displaying Volume Paths

本文提供了一个C++示例程序,用于展示系统中每个卷及其设备的所有路径。通过调用Windows API函数,该程序能够获取卷名对应的设备名称及所有路径,并将这些路径打印出来。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Displaying Volume Paths
 
 

The following C++ example shows how to display all paths for each volume and device. For each volume in the system, the example locates the volume, obtains the device name, obtains all paths for that volume, and displays the paths.

 

  1. #include <windows.h>
  2. #include <stdio.h>
  3. void DisplayVolumePaths(
  4.         __in PWCHAR VolumeName
  5.         )
  6. {
  7.     DWORD  CharCount = MAX_PATH + 1;
  8.     PWCHAR Names     = NULL;
  9.     PWCHAR NameIdx   = NULL;
  10.     BOOL   Success   = FALSE;
  11.     for (;;) 
  12.     {
  13.         //
  14.         //  Allocate a buffer to hold the paths.
  15.         Names = (PWCHARnew BYTE [CharCount * sizeof(WCHAR)];
  16.         if ( !Names ) 
  17.         {
  18.             //
  19.             //  If memory can't be allocated, return.
  20.             return;
  21.         }
  22.         //
  23.         //  Obtain all of the paths
  24.         //  for this volume.
  25.         Success = GetVolumePathNamesForVolumeNameW(
  26.             VolumeName, Names, CharCount, 
  27.             );
  28.         if ( Success ) 
  29.         {
  30.             break;
  31.         }
  32.         if ( GetLastError() != ERROR_MORE_DATA ) 
  33.         {
  34.             break;
  35.         }
  36.         //
  37.         //  Try again with the
  38.         //  new suggested size.
  39.         delete [] Names;
  40.         Names = NULL;
  41.     }
  42.     if ( Success )
  43.     {
  44.         //
  45.         //  Display the various paths.
  46.         for ( NameIdx = Names; 
  47.               NameIdx[0] != L'/0'
  48.               NameIdx += wcslen(NameIdx) + 1 ) 
  49.         {
  50.             wprintf(L"  %s", NameIdx);
  51.         }
  52.         wprintf(L"/n");
  53.     }
  54.     if ( Names != NULL ) 
  55.     {
  56.         delete [] Names;
  57.         Names = NULL;
  58.     }
  59.     return;
  60. }
  61. void __cdecl wmain(void)
  62. {
  63.     DWORD  CharCount            = 0;
  64.     WCHAR  DeviceName[MAX_PATH] = L"";
  65.     DWORD  Error                = ERROR_SUCCESS;
  66.     HANDLE FindHandle           = INVALID_HANDLE_VALUE;
  67.     BOOL   Found                = FALSE;
  68.     size_t Index                = 0;
  69.     BOOL   Success              = FALSE;
  70.     WCHAR  VolumeName[MAX_PATH] = L"";
  71.     //
  72.     //  Enumerate all volumes in the system.
  73.     FindHandle = FindFirstVolumeW(VolumeName, ARRAYSIZE(VolumeName));
  74.     if (FindHandle == INVALID_HANDLE_VALUE)
  75.     {
  76.         Error = GetLastError();
  77.         wprintf(L"FindFirstVolumeW failed with error code %d/n", Error);
  78.         return;
  79.     }
  80.     for (;;)
  81.     {
  82.         //
  83.         //  Skip the //?/ prefix and remove the trailing backslash.
  84.         Index = wcslen(VolumeName) - 1;
  85.         if (VolumeName[0]     != L'//' ||
  86.             VolumeName[1]     != L'//' ||
  87.             VolumeName[2]     != L'?'  ||
  88.             VolumeName[3]     != L'//' ||
  89.             VolumeName[Index] != L'//'
  90.         {
  91.             Error = ERROR_BAD_PATHNAME;
  92.             wprintf(L"FindFirstVolumeW/FindNextVolumeW returned a bad path: %s/n", VolumeName);
  93.             break;
  94.         }
  95.         //
  96.         //  QueryDosDeviceW doesn't allow a trailing backslash,
  97.         //  so temporarily remove it.
  98.         VolumeName[Index] = L'/0';
  99.         CharCount = QueryDosDeviceW(&VolumeName[4], DeviceName, ARRAYSIZE(DeviceName)); 
  100.         VolumeName[Index] = L'//';
  101.         if ( CharCount == 0 ) 
  102.         {
  103.             Error = GetLastError();
  104.             wprintf(L"QueryDosDeviceW failed with error code %d/n", Error);
  105.             break;
  106.         }
  107.         wprintf(L"/nFound a device:/n %s", DeviceName);
  108.         wprintf(L"/nVolume name: %s", VolumeName);
  109.         wprintf(L"/nPaths:");
  110.         DisplayVolumePaths(VolumeName);
  111.         //
  112.         //  Move on to the next volume.
  113.         Success = FindNextVolumeW(FindHandle, VolumeName, ARRAYSIZE(VolumeName));
  114.         if ( !Success ) 
  115.         {
  116.             Error = GetLastError();
  117.             if (Error != ERROR_NO_MORE_FILES) 
  118.             {
  119.                 wprintf(L"FindNextVolumeW failed with error code %d/n", Error);
  120.                 break;
  121.             }
  122.             //
  123.             //  Finished iterating
  124.             //  through all the volumes.
  125.             Error = ERROR_SUCCESS;
  126.             break;
  127.         }
  128.     }
  129.     FindVolumeClose(FindHandle);
  130.     FindHandle = INVALID_HANDLE_VALUE;
  131.     return;
  132. }

The following is example output from running the application. For each volume, the output includes a volume device path, a volume GUID path, and a drive letter.

 

 

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:/
See also http://msdn.microsoft.com/en-us/library/aa365730(VS.85).aspx
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值