2014-01-29 44 views
2

我想獲得驅動器號和名稱。 由於這個原因,我使用了「DeviceIoControl」和「IOCTL_DISK_GET_DRIVE_LAYOUT_EX」。我正在使用Microsoft Visual C++終極版。如何獲得驅動器號和名稱

#define wszDrive L"\\\\.\\PhysicalDrive0" 

BOOL GetDriveParition(LPWSTR wszPath, DRIVE_LAYOUT_INFORMATION_EX *pdg) 
{ 
    HANDLE hDevice = INVALID_HANDLE_VALUE; // handle to the drive to be examined 
    BOOL bResult = FALSE;     // results flag 
    DWORD junk  = 0;      // discard results 

    hDevice = CreateFileW(wszPath,   // drive to open 
         0,    // no access to the drive 
         FILE_SHARE_READ | // share mode 
         FILE_SHARE_WRITE, 
         NULL,    // default security attributes 
         OPEN_EXISTING, // disposition 
         0,    // file attributes 
         NULL);   // do not copy file attributes 

    if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive 
    { 
    return (FALSE); 
    } 


    bResult = DeviceIoControl(hDevice,      // device to be queried 
          IOCTL_DISK_GET_DRIVE_LAYOUT_EX, // operation to perform 
          NULL, 
          0,      // no input buffer 
          pdg, 
          sizeof(*pdg),   // output buffer 
          &junk,       // # bytes returned 
          NULL);   // synchronous I/O 

    CloseHandle(hDevice); 

    return (bResult); 
} 

int wmain(int argc, wchar_t *argv[]) 
{ 

    DRIVE_LAYOUT_INFORMATION_EX pdg; // disk drive partition structure 
    BOOL bResult = FALSE;  // generic results flag 

    bResult = GetDriveParition (wszDrive, &pdg); 


    if (bResult) 
    { 
    wprintf(L"Drive path   = %ws\n", wszDrive); 
    wprintf(L"Partition Style  = %I64d\n", pdg.PartitionStyle); 
    wprintf(L"Partition Count  = %ld\n", pdg.PartitionCount); 
    } 
    else 
    { 
    wprintf (L"GetDrivePartition failed. Error %ld.\n", GetLastError()); 
    } 

    getch(); 
} 

但是當我正在執行時,我遇到了一個錯誤,它是「錯誤122」。

+1

有些東西告訴我你應該學習C++基礎_before_在C++ IDE上花費數千美元。 –

回答

2

我認爲你的意思是說錯誤代碼122而不是22.那個錯誤是ERROR_INSUFFICIENT_BUFFER。由於documented,您將需要分配一個更大的緩衝區,然後重試。

這裏的要點是結構是一個可變大小的結構。您需要分配足夠大的動態內存以容納所有分區的信息。

像這樣的東西應該讓你在正確的方向前進:

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

#define wszDrive L"\\\\.\\PhysicalDrive0" 

BOOL GetDriveParition(LPWSTR wszPath, DRIVE_LAYOUT_INFORMATION_EX *pdg, size_t size) 
{ 
    HANDLE hDevice = INVALID_HANDLE_VALUE; // handle to the drive to be examined 
    BOOL bResult = FALSE;     // results flag 
    DWORD junk  = 0;      // discard results 

    hDevice = CreateFileW(wszPath,   // drive to open 
         0,    // no access to the drive 
         FILE_SHARE_READ | // share mode 
         FILE_SHARE_WRITE, 
         NULL,    // default security attributes 
         OPEN_EXISTING, // disposition 
         0,    // file attributes 
         NULL);   // do not copy file attributes 

    if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive 
    { 
    return (FALSE); 
    } 


    bResult = DeviceIoControl(hDevice,      // device to be queried 
          IOCTL_DISK_GET_DRIVE_LAYOUT_EX, // operation to perform 
          NULL, 
          0,      // no input buffer 
          pdg, 
          size,   // output buffer 
          &junk,       // # bytes returned 
          NULL);   // synchronous I/O 

    CloseHandle(hDevice); 

    return (bResult); 
} 

int wmain(int argc, wchar_t *argv[]) 
{ 

    DRIVE_LAYOUT_INFORMATION_EX* pdg; // disk drive partition structure 
    BOOL bResult = FALSE;  // generic results flag 

    size_t size = sizeof(DRIVE_LAYOUT_INFORMATION_EX) + 10*sizeof(PARTITION_INFORMATION_EX); 
    pdg = (DRIVE_LAYOUT_INFORMATION_EX*) malloc(size); 
    bResult = GetDriveParition (wszDrive, pdg, size); 

    if (bResult) 
    { 
    wprintf(L"Drive path   = %ws\n", wszDrive); 
    wprintf(L"Partition Style  = %I64d\n", pdg->PartitionStyle); 
    wprintf(L"Partition Count  = %ld\n", pdg->PartitionCount); 
    } 
    else 
    { 
    wprintf (L"GetDrivePartition failed. Error %ld.\n", GetLastError()); 
    } 

    free(pdg); 
} 

,因爲你的狀態,你使用的是C++編譯器,我投的malloc返回值。

+0

我做到了這一點,我有這個錯誤:「無法從'void *'轉換爲'DRIVE_LAYOUT_INFORMATION_EX *'爲行:pdg = malloc(size); – user3223395

+0

好的,你正在使用C++。我的代碼是C.編譯我的代碼爲C,它會編譯。或者轉換malloc的返回值。 –

+0

我已經告訴你如何做到這一點。 –

相關問題