获取有关分区的信息

时间:2012-11-28 15:45:53

标签: c++ windows

我从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;
}

有人遇到过这个问题吗?

P.S。我在Windows 7 x32上工作

0 个答案:

没有答案