setupapi

Can the setup api be used to tell if a device is enabled


I know how to use the Setup API to enable and disable devices. What I need to know is, can I use this same API to determine if a device is enabled/disabled? I think the real question is how to use it thusly because Microsoft's devcon uses the Setup API for its manipulation of hardware and that program will tell you if a device is enabled or disabled (as does the Device Manager). How is this done? My research of the Setup API methods to this point doesn't show a clear answer.

Andy


Solution

  • This API from MS has got to be among the least used, understood and worst documented. As I mentioned in my original post, the Setup API can be used to enable/disable hardware. So, I thought I would take a few moments and supply the community with how I finally figured out how to check the status of hardware.

    So, the short answer: you don't do this from the Setup API. Of course, this makes sense. After all, since you can change the devices state, i.e. enable or disable, using the Setup API: it naturally follows that you'd have to use a completely different API to determine the device's current state. Now, enter the Configuration Manager 32 API. To enable/disable hardware, you have to use the Setup API, but to figure out what state the hardware is in, you have to use the ConfigMgr 32 API (#include cfgmgr32.h). Makes sense, right?

    There may be other ways of doing this, but here's what I did.

    #include <Windows.h>
    #include <cstdlib>
    #include <setupapi.h>
    #include <cfgmgr32.h>
    
    GUID driveGuid = {0x4d36e967, 0xe325, 0x11ce,
        {0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18}};
    
    // first, get a list of hardware you're interested in using Setup API
    HDEVINFO hDevs(SetupDiGetClassDevs(&driveGuid, NULL, NULL, DIGCF_PRESENT));
    if(INVALID_HANDLE_VALUE == hDevs)
    {
        throw std::runtime_error("unable to build a list of hardware");
    }
    
    /* this struct has the dev instance ID that the CFG MGR API wants.
     * The struct must be must be inited to the size of the struct,
     * the cbSize member, all others should be 0
     */
    SP_DEVINFO_DATA devInfo = {sizeof(SP_DEVINFO_DATA)};
    DWORD index = 0;
    LONG devStatus = 0, devProblemCode = 0;
    char devId[256];
    memset(devId, 0, 256)
    while(SetupDiEnumDeviceInfo(hDevs, index++, &devInfo))
    {
        // use Config Mgr to get a nice string to compare against
        CM_Get_Device_ID(devInfo.DevInst, devId, 256, 0);
    
        /* use whatever mechanism you like to search the string
         * to find out if it's the hardware you're after
         */
        if((std::string(devId)).find("MyHardware") != std::string::npos)
        {
            // goody, it's the hardware we're looking for
            CM_Get_DevNode_Status(&devStatus, &devProblemCode, devInfo.DevInst, 0);
    
            /* if the call to getting the status code was successful,
             * do something meaningful with the data returned.
             * The fun part of this is that the return codes aren't
             * really documented on MSDN.  You'll have to look
             * through the CfgMgr32.h file.  Incidentally, these values
             * are what are shown in the Device Manager when you look
             * at the device's status.
             */
        }
    }
    
    SetupDiDestroyDeviceInfoList(hDevs);
    

    You'll have to figure out the GUID for the hardware you're after by searching through the list found here. Some of these, at least, are predefined in various Windows headers. However, at this point, I know of very few and have only stumbled onto them by accident.

    Relevant links to the functions used above: SetupDiDestroyDevieInfoList CM_Get_DevNode_Status CM_Get_Device_ID SetupDiEnumDeviceInfo SetupDiGetClassDevs SP_DEVINFO_DATA

    I hope this helps someone.