Windows SetupDiGetClassDevs列出SCSI驱动器失败,错误代码为13(错误\u无效\u数据)

Windows SetupDiGetClassDevs列出SCSI驱动器失败,错误代码为13(错误\u无效\u数据),windows,visual-c++,setupapi,Windows,Visual C++,Setupapi,我有一个相当有趣的问题,我找不到解决办法。我正在使用Setup API列出系统中的驱动器。在将枚举数设置为“IDE”时,使用下面列出的代码没有问题。当枚举数值设置为“SCSI”时,我的焦虑就来了。再现此问题的代码如下: #include <iostream> #include <Windows.h> #include <SetupAPI.h> #include <cfgmgr32.h> #include <devguid.h> int

我有一个相当有趣的问题,我找不到解决办法。我正在使用Setup API列出系统中的驱动器。在将枚举数设置为“IDE”时,使用下面列出的代码没有问题。当枚举数值设置为“SCSI”时,我的焦虑就来了。再现此问题的代码如下:

#include <iostream>
#include <Windows.h>
#include <SetupAPI.h>
#include <cfgmgr32.h>
#include <devguid.h>

int main() {
    std::cout << "Looking for only SCSI disks" << std::endl;
    HDEVINFO hDevs(SetupDiGetClassDevs(&GUID_DEVCLASS_DISKDRIVE, "SCSI", NULL, DIGCF_PRESENT));
    if(INVALID_HANDLE_VALUE == hDevs) {
        DWORD error(GetLastError());
        std::cout << "Handle returned is invalid. Error code: " << error << std::endl;
        return 1;
    }

    SP_DEVINFO_DATA sp = {sizeof(SP_DEVINFO_DATA)};
    char buff[256];
    memset(buff, 0, 256);
    DWORD index(0);

    std::cout << "The handle is valid, listing drives now" << std::endl;
    while(SetupDiEnumDeviceInfo(hDevs, index++, &sp)) {
        CM_Get_Device_ID(sp.DevInst, buff, 256, 0);
        std::cout << buff << std::endl;
        memset(buff, 0, 256);
    }

    SetupDiDestroyDeviceInfoList(hDevs);
    return 0;
}
#包括
#包括
#包括
#包括
#包括
int main(){

std::cout我将在MSDN支持论坛上发布我从一位同事那里得到的答案,以帮助那些可能被同样问题弄糊涂的人。显然,这是Windows 7的预期行为。如果系统从未见过具有指定给SetupDiGetClassDevs()的枚举器的硬件,则会发生故障,预期会出现此错误代码


作为参考,我问这个问题的线程是链接的。

您的错误处理失败了。在做任何其他事情之前,包括写入cout.interest之前,始终获取GetLastError()的值。我不知道这一点,但这似乎是合理的。我已将代码更改为编辑显示的内容。我仍然得到错误代码13(0xd)。