如何在设备管理器Windows中获取带有黄色感叹号的设备列表 - c ++

时间:2012-02-17 08:48:16

标签: c++ windows wmi

我正在使用msdn WMI示例代码来获取设备管理器中带有黄色感叹号的设备列表,但它只返回所有正确的列表 在机器上安装了设备。这里的任何人都可以帮我解决这个问题,只能使用c ++。 感谢

    void GetUnKnownDeviceList()
{
    HRESULT hres;
    hres =  CoInitializeEx(0, COINIT_MULTITHREADED); 

    hres =  CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT,
                                 RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL); 

    IWbemLocator *pLoc = 0;
    hres = CoCreateInstance( CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pLoc);
    IWbemServices *pSvc = 0;                  

    hres = pLoc->ConnectServer(_bstr_t(L"ROOT\\CIMV2"),NULL,NULL,0,NULL,0,0,&pSvc);

    hres = CoSetProxyBlanket(pSvc, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE,  NULL, RPC_C_AUTHN_LEVEL_CALL,
                             RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE);       

    IEnumWbemClassObject* pEnumerator = NULL;    
    hres = pSvc->ExecQuery(
            bstr_t("WQL"), 
            // bstr_t("SELECT * FROM Win32_PnPEntity"), DOES NOT LIST DEVICES SHOWING WITH YELLOW EXCLAMATION MARK
            bstr_t("SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0"), // LIST ONLY PROPERLY INSTALLED DEVICES
            WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, 
            NULL,
            &pEnumerator);

    IWbemClassObject *pclsObj;
    ULONG uReturn = 0;
    while(1)
    {
        HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
        VARIANT vtProp;
        hr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);
        TRACE("Device Name : %s\r\n\", vtProp.bstrVal);
        VariantClear(&vtProp);
        pclsObj->Release();
   }    

}

3 个答案:

答案 0 :(得分:1)

要列出无法正常工作的设备,您需要查询ConfigManagerErrorCode不是0的所有设备,您必须将WQL语句修改为。

SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode <> 0

答案 1 :(得分:0)

为什么不尝试使用setupapi呢?我认为您应该能够使用它来枚举已知和未知设备的设备节点列表。

答案 2 :(得分:0)

可以在c#中使用wmi这个例子:

/ Query the device list trough the WMI. If you want to get
// all the properties listen in the MSDN article mentioned
// below, use "select * from Win32_PnPEntity" instead!
ManagementObjectSearcher deviceList =
new ManagementObjectSearcher("Select Name, Status from Win32_PnPEntity");

// Any results? There should be!
if ( deviceList != null )
// Enumerate the devices
foreach ( ManagementObject device in deviceList.Get() )
{
    // To make the example more simple,
    string name = device.GetPropertyValue("Name").ToString();
    string status = device.GetPropertyValue("Status").ToString();

    // Uncomment these lines and use the "select * query" if you 
    // want a VERY verbose list
    // foreach (PropertyData prop in device.Properties)
    //    Console.WriteLine( "\t" + prop.Name + ": " + prop.Value);

    // More details on the valid properties:
    // http://msdn.microsoft.com/en-us/library/aa394353(VS.85).aspx
    Console.WriteLine( "Device name: {0}", name );  
    Console.WriteLine( "\tStatus: {0}", status );

    // Part II, Evaluate the device status.
    bool working = (( status == "OK" ) || ( status == "Degraded" )
        || ( status == "Pred Fail" ));

    Console.WriteLine( "\tWorking?: {0}", working );
}

http://www.codeproject.com/Articles/30031/Query-hardware-device-status-in-C

只需更改状态或ConfigManagerErrorCode即可获得所需的错误。