Question

I have a rather interesting problem for which I'm unable to find a resolution. I'm using Setup API to list drives in the system. I have no trouble using the code listed below when setting the enumerator to "IDE". My angst comes when the enumerator value is set to "SCSI". Code which reproduces this problem is below:

#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;
}

As you can see, there is nothing remarkable about this code. The problem is, on certain laptops, this code errors at SetupDiGetClassDevs(). Checking GetLastError() reveals that it failed for ERROR_INVALID_DATA (0xd). What I don't understand is why. This exact same program, run on my development box both as my user (with administrator rights) and as an unprivileged user, works just fine whether or not SCSI drives are present.

I know that the GUID in use is correct. It's defined in devguid.h. "SCSI" is a valid PnP enumerator as is referenced on this MSDN page and also from examining the "Enumerator" property in the Device Manager. The third argument may be NULL and the fourth is a valid defined flag for this function. I know this because, except for these laptops, this works on all systems I've ever tried it on (which, in my organization, is quite a few). I'm hoping that someone here may know about what would cause SetupDiGetClassDevs() to fail for this error with these conditions, or could at least point me in the right direction. I'm not a Windows expert and I could be missing something on system configuration or permissions (although not implied from the error).

As I hope is clear, I've run this code on the one laptop I can test it on as both a user with Administrator privileges and as the Administrator user: both with the same result. The laptop is an HP EliteBook 8460p running Windows 7 64-bit Service Pack 1. Compiling this code in 32 or 64 bits makes no difference.

Was it helpful?

Solution

I'm going to post the answer I got from a fellow on the MSDN support forums to help someone who may be confounded by this same issue. Apparently, this is expected behavior for Windows 7. If the system has never seen hardware with the enumerator specified to SetupDiGetClassDevs(), then a failure occurs and this error code is expected.

For reference, the thread where I asked this question is linked here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top