USB序列号显示特殊字符

时间:2017-06-29 10:48:06

标签: c++ windows mfc usb wmi

我有一个c ++应用程序来查找USB设备的序列号。此应用程序工作正常,但对于某些USB设备,它返回特殊字符值,如 6 0

这是我的代码

CString GetSerialNo(CString sDevId)
{
    HRESULT hres;
    CString strQuery    = "",
            strOut      = "";
    sDevId.Replace("\\","\\\\");
    //////////////////////////
    try
    {
        strQuery.Format("SELECT SerialNumber FROM win32_diskdrive where  pnpdeviceid = '%s'",sDevId);

        hres =  CoInitialize(NULL); 
        if (FAILED(hres))
        {
            return strOut;                  // Program has failed.
        }

        IWbemLocator *pLoc = NULL;

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

        if (FAILED(hres))
        {
            CoUninitialize();
            return strOut;                 // Program has failed.
        }

        IWbemServices *pSvc = NULL;

        hres = pLoc->ConnectServer(
             _bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
             NULL,                    // User name. NULL = current user
             NULL,                    // User password. NULL = current
             0,                       // Locale. NULL indicates current
             NULL,                    // Security flags.
             0,                       // Authority (for example, Kerberos)
             0,                       // Context object 
             &pSvc                    // pointer to IWbemServices proxy
             );

        if (FAILED(hres))
        {
            pLoc->Release();     
            CoUninitialize();
            return strOut;                // Program has failed.
        }

        hres = CoSetProxyBlanket(
           pSvc,                        // Indicates the proxy to set
           RPC_C_AUTHN_WINNT,           // RPC_C_AUTHN_xxx
           RPC_C_AUTHZ_NONE,            // RPC_C_AUTHZ_xxx
           NULL,                        // Server principal name 
           RPC_C_AUTHN_LEVEL_CALL,      // RPC_C_AUTHN_LEVEL_xxx 
           RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
           NULL,                        // client identity
           EOAC_NONE                    // proxy capabilities 
        );

        if (FAILED(hres))
        {
            pSvc->Release();
            pLoc->Release();     
            CoUninitialize();
            return strOut;               // Program has failed.
        }

        IEnumWbemClassObject* pEnumerator = NULL;
        hres = pSvc->ExecQuery(
            bstr_t("WQL"), 
            bstr_t(strQuery),
            WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, 
            NULL,
            &pEnumerator);

        if (FAILED(hres))
        {
            pSvc->Release();
            pLoc->Release();
            CoUninitialize();
            return strOut;               // Program has failed.
        }


        IWbemClassObject *pclsObj = NULL;
        ULONG uReturn = 0;

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

            if(0 == uReturn)
            {
                break;
            }

            VARIANT vtProp;

            hr = pclsObj->Get(L"SerialNumber", 0, &vtProp, 0, 0);
            strOut = (CString)vtProp.bstrVal;
            VariantClear(&vtProp);
            pclsObj->Release();
        }

        pSvc->Release();
        pLoc->Release();
        pEnumerator->Release();
        CoUninitialize();
        if(!strOut.IsEmpty())
        {
            strOut.Trim();
        }
    }
    catch(...)
    {
        return strOut;
    }
    return strOut;  
}

我将分享在序列号

中出现问题的设备的输出

标题TOSHIBA TransMemory USB设备 PNPDeviceID USBSTOR \ DISK& VEN_TOSHIBA& PROD_TRANSMEMORY& REV_1.00 \ 0022CFF653F1C060D88C887E& 0

SerialNumber 0

标题金士顿DataTraveler 2.0 USB设备 PNPDeviceID USBSTOR \ DISK& VEN_KINGSTON& PROD_DATATRAVELER_2.0& REV_1.00 \ 60A44C413BF2F0718628B8E0& 0

SerialNumber 6

如果您知道为什么序列号是这样的,请分享。

注意:上面的代码工作正常,WBEMTEST工具的结果相同。

1 个答案:

答案 0 :(得分:1)

查询序列号时会出现HRESULT。

hr = pclsObj->Get(L"SerialNumber", 0, &vtProp, 0, 0);

但是,您不检查操作是否成功。某些USB设备没有SerialNumber属性。