Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1683102
  • 博文数量: 584
  • 博客积分: 13857
  • 博客等级: 上将
  • 技术积分: 11883
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-16 09:34

分类: WINDOWS

2011-04-04 16:54:55

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 = (PWCHAR) new 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, &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 does not 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:\
阅读(655) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~