C ++如何知道与哪个IRQNumber关联的设备?

时间:2018-08-03 13:19:39

标签: c++ wmi wql

我尝试确定特定设备与哪个IRQNumber相关联。然后,当我有IRQNumber时,我想列出该IRQ上的所有设备,以查看特定设备是该IRQ上的唯一设备还是共享它。

感谢外围设备管理器,我知道了特定设备的名称和关联的IRQNumber,但是我想用C ++代码完成上面的操作。

我找到了Win32_PnPAllocatedResource类,但无法在查询中选择设备。

查询:

sprintf(szTemp, "SELECT * FROM Win32_PnPAllocatedResource Where Dependent LIKE '\\\\%ws\\root\\cimv2:Win32_PnPEntity.DeviceID=\"%s\"'", ComputerName, DriverIDm);
hres = pSvc->ExecQuery(
    bstr_t("WQL"),
    bstr_t(szTemp),
    WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
    NULL,
    &pEnumerator2);
if (FAILED(hres))
{
    cout << "Query for Win32_PnPAllocatedResource name failed."
        << " Error code = 0x"
        << hex << hres << endl;
    pSvc2->Release();
    pLoc2->Release();
    CoUninitialize();
    //return 1;               // Program has failed.
}

对于之前获取的计算机名称和设备ID:

IEnumWbemClassObject* pEnumerator = NULL;
IEnumWbemClassObject* pEnumerator2 = NULL;
char szTemp[1024];
char szTemp2[1024];
LPCTSTR DriverID;

//////////////////////////////////////////////////////////////////
//
//  DeviceID
//
//////////////////////////////////////////////////////////////////

hres = pSvc->ExecQuery(
        bstr_t("WQL"),
        bstr_t("SELECT * FROM Win32_PnPEntity Where Name LIKE 'Name'"), //Name of the device find in the peripheral manager
        WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
        NULL,
        &pEnumerator);
    if (FAILED(hres))
    {
        cout << "Query for PnPEntity name failed."
            << " Error code = 0x"
            << hex << hres << endl;
        pSvc->Release();
        pLoc->Release();
        CoUninitialize();
        //return 1;               // Program has failed.
    }


    IWbemClassObject *pclsObj = NULL;
    ULONG uReturn = 0;
    HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
        &pclsObj, &uReturn);

    if (0 == uReturn)
    {
        sprintf_s(szTemp, "Device not found !");
        MessageBoxA(NULL, szTemp, "Error", MB_ICONERROR);
    }
    VARIANT vtProp;
    hr = pclsObj->Get(L"DeviceID", 0, &vtProp, 0, 0);
    wcout << " DeviceID : " << vtProp.bstrVal << endl;
    DriverID = vtProp.bstrVal;
    int size = 0, cpt = 0, cpt2 = 0;
    sprintf(szTemp2, "%ws", DriverID);
    size = strlen(szTemp2);
    char DriverIDm[1024];

    for (cpt = 0; cpt < size + 1; cpt++)
    {
        if (cpt == size )
        {
            DriverIDm[cpt2] = '\0';
            break;
        }
        if (szTemp2[cpt] == '\\')
        {
            DriverIDm[cpt2] = szTemp2[cpt];
            cpt2++;
            DriverIDm[cpt2] = szTemp2[cpt];
            cpt2++;
        }
        else
        {
            DriverIDm[cpt2] = szTemp2[cpt];
            cpt2++;
        }
    }


    //////////////////////////////////////////////////////////////////
    //
    //  Computer Name
    //
    //////////////////////////////////////////////////////////////////

    LPCTSTR ComputerName;

    hres = pSvc->ExecQuery(
        bstr_t("WQL"),
        bstr_t("SELECT * FROM Win32_IRQResource"),
        WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
        NULL,
        &pEnumerator);
    if (FAILED(hres))
    {
        cout << "Query for IRQResource failed."
            << " Error code = 0x"
            << hex << hres << endl;
        pSvc->Release();
        pLoc->Release();
        CoUninitialize();
        //return 1;               // Program has failed.
    }


    hr = pEnumerator->Next(WBEM_INFINITE, 1,
        &pclsObj, &uReturn);

    if (0 == uReturn)
    {
        sprintf_s(szTemp, "Error query !");
        MessageBoxA(NULL, szTemp, "Error", MB_ICONERROR);
    }

    hr = pclsObj->Get(L"CSName", 0, &vtProp, 0, 0);
    wcout << " CSName : " << vtProp.bstrVal << endl;
    ComputerName = vtProp.bstrVal;

计算机名称和DeviceID正确无误。

现在出现错误的代码:

IWbemClassObject *pclsObj2 = NULL;

ULONG uReturn2 = 0;

while (pEnumerator2)
{
    HRESULT hr2 = pEnumerator2->Next(WBEM_INFINITE, 1,
        &pclsObj2, &uReturn2);
    DWORD nError2 = GetLastError();
    if (0 == uReturn2)
    {
        sprintf_s(szTemp, "Device not found !");
        MessageBoxA(NULL, szTemp, "Error", MB_ICONERROR);
        break;
    }

hr2的值为0x80041017(WBEM_E_INVALID_QUERY:查询在语法上无效。)。 GetLastError()返回的值为1008(ERROR_NO_TOKEN:试图引用不存在的令牌。)。 而Return2 = 0。

问题出在哪里?

Win32_PnPAllocatedResource是正确的类,以了解哪个IRQ是特定设备吗?

谢谢

0 个答案:

没有答案