2017-02-19 4 views
0

HELOコマンドにGetLogicalDrivesから結果を渡すC++の窓は、GetVolumeInformation

イムGetVolumeInformationを使用して、すべてのドライブを取得し、私がドライブの種類を検出するために、この更を使用すると、特定のドライブの状態を確認するためにGetLogicalDrivesを使用して。 GetVolumeInformationとGetDriveTypesでGetLogicalDrives(DWORD)の結果を使用することはできませんが、LPCWSTRを理解しています。どうすればGetLogicalDrivesから結果を変換し、GetVolumeInformationとGetDriveTypesに渡すことができますか?

 TCHAR myDrives[] = L" A"; 
     DWORD myDrivesBitMask = GetLogicalDrives(); 
     WCHAR szTest[10]; 



     if (myDrivesBitMask == 0) 
wprintf(L"GetLogicalDrives() failed with error code: %d\n", GetLastError()); 
     else { 
      wprintf(L"This machine has the following logical drives:\n"); 
      while (myDrivesBitMask) { 
       // Use the bitwise AND with 1 to identify 
       // whether there is a drive present or not. 
       if (myDrivesBitMask & 1) { 
        // Printing out the available drives     
        wprintf(L"drive %s\n", myDrives); 
       } 
       // increment counter for the next available drive. 
       myDrives[1]++; 
       // shift the bitmask binary right  
       myDrivesBitMask >>= 1; 
      } 
      wprintf(L"\n"); 
     } 
UINT test; 
for (i = 0; i<12; i++) 
    { 
     test = GetDriveType(myDrives[i]); 
     switch (test) 
     { 
     case 0: printf("Drive %S is type %d - Cannot be determined.\n", myDrives[i], test); 
      break; 
     case 1: printf("Drive %S is type %d - Invalid root path/Not available.\n", myDrives[i], test); 
      break; 
     case 2: printf("Drive %S is type %d - Removable.\n", myDrives[i], test); 
      break; 
     case 3: printf("Drive %S is type %d - Fixed.\n", myDrives[i], test); 
      break; 
     case 4: printf("Drive %S is type %d - Network.\n", myDrives[i], test); 
      break; 
     case 5: printf("Drive %S is type %d - CD-ROM.\n", myDrives[i], test); 

      break; 
     case 6: printf("Drive %S is type %d - RAMDISK.\n", myDrives[i], test); 
      break; 
     default: "Unknown value!\n"; 
     } 
    } 



    (GetVolumeInformation(myDrives, volumeName, ARRAYSIZE(volumeName), &serialNumber, &maxComponentLen, &fileSystemFlags, fileSystemName, ARRAYSIZE(fileSystemName))) 
      { 
       _tprintf(_T("There is a CD/DVD in the CD/DVD rom")); 
       _tprintf(_T("Volume Name: %s\n"), volumeName); 
       _tprintf(_T("Serial Number: %lu\n"), serialNumber); 
       _tprintf(_T("File System Name: %s\n"), fileSystemName); 
       _tprintf(_T("Max Component Length: %lu\n"), maxComponentLen); 

      } 
      else 
       _tprintf(_T("There is NO CD/DVD in the CD/DVD rom")); 
+1

代わりGetLogicalDrives' 'の使用[' GetLogicalDriveStrings'(https://msdn.microsoft.com/en-us/library/windows/desktop/aa364975(V = VS.85)の.aspx)。 –

+3

または単にドライブ番号「0」を「A:\」にマッピングするなど(アルファベット順)。 –

+1

最新のWindowsでは、 'TCHAR'のようなWindows 9x互換のマクロの代わりに、Unicode(' wchar_t'型)を使うだけでかなり簡単にできます。結局のところ、あなたのツールチェーンは、おそらくWindows 9x実行可能ファイルを生成することさえできません。そのことは、複雑なことを除いて、何のためのものでもありません。 –

答えて

2

ので、 GetDriveType()GetVolumeInformation()に電話するにはドライブレターが必要です。GetLogicalDrives()の代わりにGetLogicalDriveStrings()を使用する方が簡単です。

WCHAR myDrives[105]; 
WCHAR volumeName[MAX_PATH]; 
WCHAR fileSystemName[MAX_PATH]; 
DWORD serialNumber, maxComponentLen, fileSystemFlags; 
UINT driveType; 

if (!GetLogicalDriveStringsW(ARRAYSIZE(myDrives)-1, myDrives)) 
{ 
    wprintf(L"GetLogicalDrives() failed with error code: %lu\n", GetLastError()); 
} 
else 
{ 
    wprintf(L"This machine has the following logical drives:\n"); 

    for (LPWSTR drive = myDrives; *drive != 0; drive += 4) 
    { 
     driveType = GetDriveTypeW(drive); 
     wprintf(L"Drive %s is type %d - ", drive, driveType); 

     switch (driveType) 
     { 
      case DRIVE_UNKNOWN: 
       wprintf(L"Cannot be determined!"); 
       break; 
      case DRIVE_NO_ROOT_DIR: 
       wprintf(L"Invalid root path/Not available."); 
       break; 
      case DRIVE_REMOVABLE: 
       wprintf(L"Removable."); 
       break; 
      case DRIVE_FIXED: 
       wprintf(L"Fixed."); 
       break; 
      case DRIVE_REMOTE: 
       wprintf(L"Network."); 
       break; 
      case DRIVE_CDROM: 
       wprintf(L"CD-ROM."); 
       break; 
      case DRIVE_RAMDISK: 
       wprintf(L"RAMDISK."); 
       break; 
      default: 
       wprintf(L"Unknown value!"); 
     } 
     wprintf(L"\n"); 

     if (driveType == DRIVE_CDROM) 
     { 
      if (GetVolumeInformationW(drive, volumeName, ARRAYSIZE(volumeName), &serialNumber, &maxComponentLen, &fileSystemFlags, fileSystemName, ARRAYSIZE(fileSystemName))) 
      { 
       wprintf(L" There is a CD/DVD in the drive:\n"); 
       wprintf(L" Volume Name: %s\n", volumeName); 
       wprintf(L" Serial Number: %08X\n", serialNumber); 
       wprintf(L" File System Name: %s\n", fileSystemName); 
       wprintf(L" Max Component Length: %lu\n", maxComponentLen); 
      } 
      else 
      { 
       wprintf(L" There is NO CD/DVD in the drive"); 
      } 
     } 
    } 
} 
+0

ありがとうございます。この例を作るのに時間がかかったことに本当に感謝しています。これにより、コードが簡単になります。 – user1754598

1

私は簡単にドライブpathesのベクトルを処理するために取得するために)(GetLogicalDrivesのためのラッパー関数を作成します。

std::vector<std::wstring> GetLogicalDrivePathes() 
{ 
    std::vector<std::wstring> result; 

    DWORD mask = GetLogicalDrives(); 
    for(wchar_t drive = 'A'; drive <= 'Z'; ++drive) 
    { 
     if(mask & 1) 
     { 
      std::wstring rootPath; 
      rootPath += drive; 
      rootPath += L":\\"; 
      result.push_back(rootPath); 
     } 
     mask >>= 1; 
    } 

    return result; 
} 

それは次のように使用することができます:

for(auto& path : GetLogicalDrivePathes()) 
{ 
    test = GetDriveType(path.data()); 

} 
+2

'GetLogicalDriveStrings()'は 'GetLogicalDrivePathes()'を大幅に単純化します。 –

+1

二重ゼロ終端文字列が嫌いです。OPでどのように機能するかを示すためにOPで使用されている関数を使用したいと思います。あなたが好きなら、代わりにGetLogicalDriveStrings()を使用する方法を示す回答を投稿することができます。 – zett42

+0

この例が本当に助けてくれてありがとうございます。 – user1754598