操作数类型不兼容" BSTR"和" const char *"

时间:2014-07-04 19:00:30

标签: c++ visual-c++ wmi-query operands

您好我正在使用visual studio c ++ 2010而我仍然不那么擅长。我正在使用wmi查询,我想在使用if之前使用if cout结果这里是代码

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

if (FAILED(hres))
{
    cout << "Query for CS failed."
        << " Error code = 0x" 
        << hex << hres << endl;
    pSvc->Release();
    pLoc->Release();
    CoUninitialize();
    return 1;               // Program has failed.
}


// Get the data from the query 

IWbemClassObject *pclsObj;
ULONG uReturn = 0;

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

    if(0 == uReturn)
    {
        break;
    }

    VARIANT vtProp;


    // Get the value of the Name property
    hr = pclsObj->Get(L"Manufacturer", 0, &vtProp, 0, 0);


    if (vtProp.bstrVal == "test")  //// HERE IS THE PROB HOW TO FIX HERE?I WANT TO SAY IF THE RESULT ="WHATEVER" THEN 

    wcout << "\Manufacturer : " << vtProp.bstrVal << endl; // or do stuff 

    VariantClear(&vtProp);

    pclsObj->Release();

错误是:错误C2446:&#39; ==&#39; :没有来自&#39; const char *&#39;到BSTR&#39; 我还是初学者,我不知道要解决这个问题,请帮忙!!!

1 个答案:

答案 0 :(得分:2)

这里至少有两个问题:

  • BSTR看起来像一个字符串但不完全像C字符串。阅读更多here。所以你必须要小心差异。

  • 即使vtProp.bstrVal是C字符串,您的比较也不会起作用,因为vtProp.bstrVal == "test"会比较指针的地址而不是字符串的内容。

对于简单的比较,就像在代码中一样,解决方案是用

替换您的测试
wcscmp(vtProp.bstrVal, L"test")==0