检测IDE中的硬盘驱动器

时间:2014-08-08 01:22:03

标签: c++ winapi

我正在尝试检测Windows中的驱动器号。 Drive是第二个IDE通道中的主要驱动器。我正在使用GetLogicalDrives()

但这并没有告诉我我正在访问IDE主驱动器。

1 个答案:

答案 0 :(得分:1)

以下是一个例子:

#include <cstdint>
#include <windows.h>
#include <cstdio>

const char* GetTypeOfDrive(const char* Drive)
{
    const char* Result = NULL;
    unsigned int DriveType = GetDriveType(Drive);

    switch(DriveType)
    {
        case DRIVE_FIXED:
            Result = "Hard disk";
            break;

        case DRIVE_CDROM:
            Result = "CD/DVD";
            break;

        case DRIVE_REMOVABLE:
            Result = "Removable";
            break;

        case DRIVE_REMOTE:
            Result = "Network";
            break;

        default:
            Result = "Unknown";
            break;
    }

    return Result;
}

int GetLogicalDrivesList(char Drives[26])
{
    int Res = 0;
    DWORD DrivesMask = GetLogicalDrives();

    for (int I = 0; I < 26; ++I)
    {
        if (DrivesMask & (1 << I))
        {
            Drives[Res++] = 'A' + I;
        }
    }
    return Res;
}

int main()
{
    char temp[4];
    char drives[26];

    int drive_count = GetLogicalDrivesList(drives);

    for (int i = 0; i < drive_count; ++i)
    {
        sprintf(temp, "%c:/", drives[i]);
        printf("%c is a %s\n", drives[i], GetTypeOfDrive(temp));
    }
}