我目前正在寫一個C++程序來自動獲取關於樣本的分區信息的硬盤驅動器的形象,問題是在磁盤上,併爲每個分區的分區數量的信息其起始扇區,大小和文件系統類型。不正確的函數調用「IOCTL_DISK_GET_DRIVE_LAYOUT_EX」
我敢肯定,在這一點上,以實現這一目標是通過MSDN的功能,微軟內置命令的最佳方式。我試圖使用「IOCTL_DISK_GET_DRIVE_LAYOUT_EX」函數,但根據我的錯誤調用我的函數是不正確的。當我調試程序看來,布爾值也是「IOCTL_DISK_GET_DRIVE_LAYOUT_EX」呼叫後保持不變,這意味着它不會返回bResult值。
我使用Microsoft Visual C++速成版。如果人們可以看看我的代碼,並告訴我他們認爲我做錯了什麼,這將非常感激。
#define UNICODE 1
#define _UNICODE 1
#include <windows.h>
#include <winioctl.h>
#include <stdio.h>
#define wszDrive L"\\\\.\\PhysicalDrive6"
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, // handle to device
IOCTL_DISK_GET_DRIVE_LAYOUT_EX, // dwIoControlCode
NULL, // lpInBuffer
0, // nInBufferSize
pdg, // lpOutBuffer
sizeof(*pdg), // nOutBufferSize
&junk, // lpBytesReturned
NULL); // lpOverlapped
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);
system("Pause");
}
else
{
wprintf (L"GetDrivePartition failed. Error %ld.\n", GetLastError());
system("Pause");
}
return ((int)bResult);
}
請致電'GetLastError'的功能之後,看看是什麼問題? – 2013-03-15 13:48:04
錯誤122意味着「傳遞給系統調用的數據錯誤太小」......您是否需要首先初始化結構? – 2013-03-15 13:53:40
感謝您的評論,只要我能夠,我會嘗試一下。將報告結果。 – 2013-03-15 14:36:06