转载  Displaying Volume Paths 收藏

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

发表于 @ 2009年01月04日 14:46:00 | 评论( loading... ) | 编辑| 举报| 收藏

旧一篇:DLL(Dynamic Link Libraries)专题  | 新一篇:【PDA】第一个项目

  • 发表评论
  • 评论内容:
  •  
Copyright © lfchen
Powered by CSDN Blog