使用FindVolumeClose()访问冲突

时间:2014-02-01 01:37:46

标签: c++ winapi

在以下函数中,当我点击FindVolumeClose()行时,我总是得到下面列出的错误。任何想法为什么会这样?

BootFixFFS.exe中0x11000000处的未处理异常:0xC0000005:访问冲突(参数:0x00000008)。

char buffer[MAX_PATH];

HANDLE hVol = FindFirstVolume(buffer, MAX_PATH);

if (hVol == INVALID_HANDLE_VALUE) {
    std::cout << "No volumes found\n.";

    return FALSE;
}

std::string strVol = buffer;

strVol.pop_back();

HANDLE hPart = CreateFile(strVol.c_str(), 0, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

if (hPart == INVALID_HANDLE_VALUE) {
    FindVolumeClose(hVol);

    std::cout << "Couldn't create file: " << GetLastError() << "\n";

    return FALSE;
}

int numPartitions = 8;

DWORD dwDliSize = sizeof(DRIVE_LAYOUT_INFORMATION_EX) + numPartitions * sizeof(PARTITION_INFORMATION_EX);

DRIVE_LAYOUT_INFORMATION_EX *dli = (DRIVE_LAYOUT_INFORMATION_EX*) new BYTE[dwDliSize];

DWORD dwSize;

ZeroMemory(&dli, sizeof(dli));

BOOL bSuccess = DeviceIoControl(hPart, IOCTL_DISK_GET_DRIVE_LAYOUT_EX, NULL, 0, &dli, dwDliSize, &dwSize, NULL);

CloseHandle(hPart);

FindVolumeClose(hVol);

if (bSuccess == FALSE) {
    std::cout << "Couldn't get device information: " << GetLastError() << "\n";

    return FALSE;
}   

return TRUE;

1 个答案:

答案 0 :(得分:4)

在我看来,ZeroMemory(&amp; dli,sizeof(dli))没有做你想做的事。它从dli中删除地址,而不是将字节数组的内容归零。也许ZeroMemory(dli,dwDliSize)就是你的意思?

此外,更严重的错误是在调用DeviceIoControl时使用&amp; dli。您基本上覆盖整个堆栈,并破坏局部变量。