最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

setupapi - Windows Setup API, is there a way to get the interface instances from a device instance? - Stack Overflow

programmeradmin14浏览0评论

Say I want to iterate through all of the devices on a system, and for each device, iterate through its interfaces.

Given the information available from an SP_DEVINFO_DATA object, how can I iterate through its interfaces? Do I need the interface class guid to do that, and if so, how do I get it?

Here is the code I've tried (error checking removed for clarity):

#include <iostream>

#include <Windows.h>
#include <SetupAPI.h>

using namespace std;

int
main()
{
    HDEVINFO devInfo;
    HDEVINFO devInfo2;
    SP_DEVINFO_DATA devInfoData;
    SP_DEVICE_INTERFACE_DATA interfaceData;

    // Iterate over all devices
    devInfo = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT);
    devInfoData.cbSize = sizeof(devInfoData);
    for (DWORD i = 0; SetupDiEnumDeviceInfo(devInfo, i, &devInfoData); ++i)
    {
        DWORD idSize;
        SetupDiGetDeviceInstanceIdA(devInfo, &devInfoData, NULL, 0, &idSize);
        char * id = new char[idSize + 1];
        SetupDiGetDeviceInstanceIdA(devInfo, &devInfoData, id, idSize, NULL);
        cout << i << ": devinst = " << devInfoData.DevInst << "  " << id << endl;

        // Iterate over all interfaces for this device
        // This part is broken; I assume because SP_DEVINFO_DATA.ClassGuid is
        // not the same as the interface class.
        devInfo2 = SetupDiGetClassDevs(&devInfoData.ClassGuid, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);
        interfaceData.cbSize = sizeof(interfaceData);
        for (DWORD j = 0; SetupDiEnumDeviceInterfaces(devInfo2, NULL, &devInfoData.ClassGuid, j, &interfaceData); ++j) {
            PSP_DEVICE_INTERFACE_DETAIL_DATA_A detailData = NULL;
            DWORD requiredLength;
            SetupDiGetDeviceInterfaceDetailA(devInfo2, &interfaceData, NULL, 0, &requiredLength, NULL);
            detailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA_A)malloc(requiredLength);
            detailData->cbSize = sizeof(*detailData);
            SetupDiGetDeviceInterfaceDetailA(devInfo2, &interfaceData, detailData, requiredLength, NULL, NULL);
            cout << "    " << j << ": " << detailData->DevicePath << endl;
            free(detailData);
        }
        SetupDiDestroyDeviceInfoList(devInfo2);
    }
    SetupDiDestroyDeviceInfoList(devInfo);
    return 0;
}

(I can add code with error checking if people want to see it.)

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论