2012-01-11 7 views
3

に可能な重複リスト:
How to list physical disks?自分のコンピュータにインストールされている物理ドライブ

を自分のコンピュータにインストールされている物理ドライブのリストを表示する(最速)C++道「最良の方法」とは何ですか?それを行うための追加ライブラリがありますか?

+1

あなたはどのライブラリを使用できますか?どのオペレーティングシステムですか? –

+0

あなたはどのOSを使用していますか? – Cyclonecode

+0

@ Mr.TAMER Windowsは私が取り組んでいるシステムです、ブーストが好ましい方法ですが、ブーストがそれを受け入れないようにするための他の方法も受け入れられます。 – smallB

答えて

11

GetLogicalDriveStrings()を使用して、使用可能なすべての論理ドライブを取得します。

#include <windows.h> 
#include <stdio.h> 


DWORD mydrives = 100;// buffer length 
char lpBuffer[100];// buffer for drive string storage 

int main() 
{ 
     DWORD test = GetLogicalDriveStrings(mydrives, lpBuffer); 

     printf("The logical drives of this machine are:\n\n"); 

     for(int i = 0; i<100; i++) printf("%c", lpBuffer[i]); 


     printf("\n"); 
     return 0; 
} 

又はGetLogicalDrives()

#include <windows.h> 
#include <direct.h> 
#include <stdio.h> 
#include <tchar.h> 

// initial value 
TCHAR szDrive[ ] = _T(" A:"); 

int main() 
{ 
    DWORD uDriveMask = GetLogicalDrives(); 
    printf("The bitmask of the logical drives in hex: %0X\n", uDriveMask); 
    printf("The bitmask of the logical drives in decimal: %d\n", uDriveMask); 
    if(uDriveMask == 0) 
     printf("\nGetLogicalDrives() failed with failure code: %d\n", GetLastError()); 
    else 
    { 
     printf("\nThis machine has the following logical drives:\n"); 
    while(uDriveMask) 
    {// use the bitwise AND, 1–available, 0-not available 
    if(uDriveMask & 1) 
     printf("%s\n",szDrive); 
    // increment... 
    ++szDrive[1]; 
     // shift the bitmask binary right 
     uDriveMask >>= 1; 
    } 
    printf("\n "); 
    } 
    return 0; 
} 
0

一つの可能​​性を使用することは、Win32_DiskDriveのインスタンスを列挙するためにWMIを使用することです。

関連する問題