0

我从 msdn 获得了一个 c++ 代码示例,我在其中尝试获取有关我的分区的信息,但在此代码中,我的 DeviceIoControl 方法返回 0,结束错误代码 3。如何解决此错误?

代码在这里:

#define WINVER 0x0500

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

void DisplayVolumeInfo(CHAR *volume)
{
    HANDLE hDevice = CreateFileA(
            volume,
            GENERIC_READ | GENERIC_WRITE,
            FILE_SHARE_WRITE | FILE_SHARE_READ,
            NULL,
            OPEN_EXISTING,
            FILE_ATTRIBUTE_SYSTEM,
            NULL);

if (hDevice != INVALID_HANDLE_VALUE)
{
    PARTITION_INFORMATION partInfo;
    DWORD retcount = 0;

    BOOL res = DeviceIoControl(
                hDevice,
                IOCTL_DISK_GET_PARTITION_INFO,
                (LPVOID)NULL,
                (DWORD)0,
                (LPVOID)&partInfo,
                sizeof(partInfo),
                &retcount,
                (LPOVERLAPPED)NULL);

    if (res)
        std::cout << "Volume size = " << partInfo.PartitionLength.QuadPart << " bytes (" << (unsigned long long)(partInfo.PartitionLength.QuadPart / 1024 / 1024) << "Mb)" << std::endl;
    else
        std::cout << "Can't do IOCTL_DISK_GET_PARTITION_INFO (error code=" << GetLastError() << ")" << std::endl;

    DISK_GEOMETRY diskGeometry;
    retcount = 0;
    res = DeviceIoControl(
                hDevice,
                IOCTL_DISK_GET_DRIVE_GEOMETRY,
                NULL, 0,
                &diskGeometry, sizeof(diskGeometry),
                &retcount,
                (LPOVERLAPPED)NULL);

    if (res)
    {
        std::cout << "Cylinders = " << diskGeometry.Cylinders.QuadPart << std::endl;
        std::cout << "Tracks/cylinder = " << diskGeometry.TracksPerCylinder << std::endl;
        std::cout << "Sectors/track = " << diskGeometry.SectorsPerTrack << std::endl;
        std::cout << "Bytes/sector = " << diskGeometry.BytesPerSector << std::endl;
    }
    else
        std::cout << "Can't do IOCTL_DISK_GET_DRIVE_GEOMETRY (error code=" << GetLastError() << ")" << std::endl;

    CloseHandle(hDevice);
}
else
    std::cout << "Error opening volume " << volume << " (error code=" << GetLastError() << ")" << std::endl;
}


int main()
{
    DWORD  CharCount            = 0;
    char   DeviceName[MAX_PATH] = "";
    DWORD  Error                = ERROR_SUCCESS;
    HANDLE FindHandle           = INVALID_HANDLE_VALUE;
    BOOL   Found                = FALSE;
    size_t Index                = 0;
    BOOL   Success              = FALSE;
    char   VolumeName[MAX_PATH] = "";
    //
    //  Enumerate all volumes in the system.
    FindHandle = FindFirstVolumeA(VolumeName, MAX_PATH);

if (FindHandle == INVALID_HANDLE_VALUE)
{
    Error = GetLastError();
    printf("FindFirstVolume failed with error code %d\n", Error);
    return 0;
}

for (;;)
{
    //
    //  Skip the \\?\ prefix and remove the trailing backslash.

    Index = strlen(VolumeName) - 1;

    if (VolumeName[0]     != '\\' ||
        VolumeName[1]     != '\\' ||
        VolumeName[2]     != '?'  ||
        VolumeName[3]     != '\\' ||
        VolumeName[Index] != '\\')
    {
        Error = ERROR_BAD_PATHNAME;
        printf("FindFirstVolume/FindNextVolume returned a bad path: %s\n", VolumeName);
        break;
    }

    //
    //  QueryDosDeviceW does not allow a trailing backslash,
    //  so temporarily remove it.
    VolumeName[Index] = '\0';

    CharCount = QueryDosDeviceA(&VolumeName[4], DeviceName, MAX_PATH);

    VolumeName[Index] = '\\';

    if ( CharCount == 0 )
    {
        Error = GetLastError();
        printf("QueryDosDevice failed with error code %d\n", Error);
        break;
    }

    printf("\nFound a device:\n %s", DeviceName);
    printf("\nVolume name: %s", VolumeName);
    printf("\n");

    DisplayVolumeInfo(VolumeName);

    //
    //  Move on to the next volume.
    Success = FindNextVolumeA(FindHandle, VolumeName, MAX_PATH);

    if ( !Success )
    {
        Error = GetLastError();

        if (Error != ERROR_NO_MORE_FILES)
        {
            printf("FindNextVolumeW failed with error code %d\n", Error);
            break;
        }

        //
        //  Finished iterating
        //  through all the volumes.
        Error = ERROR_SUCCESS;
        break;
    }
}

FindVolumeClose(FindHandle);
FindHandle = INVALID_HANDLE_VALUE;

return 0;
}

有人遇到过这个问题吗?

PS我在Windows 7 x32上工作

4

0 回答 0