无法使用WMI从Windows Server 2016检索AntiVirus详细信息

时间:2018-03-21 08:03:52

标签: c++ wmi antivirus

我们正在使用WMI服务从Windows检索AntiVirus详细信息(AntiVirus的名称和已安装状态)。并且该代码适用于Windows 7,8.1,10,但在Windows Server 2016操作系统上失败。所以请帮助我们解决这个问题。这就是我们使用的代码:

void GetAntiVirusDetails()
{
    CoInitialize(0);
    DWORD   dwErr   = 0;
    CString csLog   = "";
    HRESULT hr      = S_OK;
    ////////////////////////
    try
    {
        hr = ::CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);
        CComPtr<IWbemLocator> pWbemLocator; 
        hr = pWbemLocator.CoCreateInstance(CLSID_WbemLocator);
        if(SUCCEEDED(hr))
        {
            CComPtr<IWbemServices> pWbemServices; 
            hr = pWbemLocator->ConnectServer(CComBSTR(L"root\\SecurityCenter2"), NULL, NULL, 0, NULL, 0, NULL, &pWbemServices);
            if(SUCCEEDED(hr))
            {
                CComPtr<IEnumWbemClassObject> pEnum;
                CComBSTR cbsQuery = L"Select * From AntiVirusProduct";
                hr = pWbemServices->ExecQuery(CComBSTR("WQL"), cbsQuery, WBEM_FLAG_FORWARD_ONLY, NULL, &pEnum);
                if(SUCCEEDED(hr))
                {
                    hr = EnumAllProcess(pEnum,csLog);
                }
                else
                {
                    dwErr = GetLastError();
                    csLog.AppendFormat("ExecQuery failed,Error= %ld,Retrun code= 0x%X",dwErr,hr);
                    OutputDebugString(csLog);

                                        // Failed with return value 0x80041010 on Windows Server 2016 OS
                }
            }
            else
            {
                dwErr = GetLastError();
                csLog.AppendFormat("Unable to Connect Server,Error= %ld,Retrun code= 0x%X",dwErr,hr);
                OutputDebugString(csLog);
            }
        }
        else
        {
            dwErr = GetLastError();
            csLog.AppendFormat("CoCreateInstance failed,Error= %ld,Retrun code= 0x%X",dwErr,hr);
            OutputDebugString(csLog);
        }
    }
    catch(...)
    {
        CoUninitialize();
        return;
    }
    CoUninitialize();
}

HRESULT EnumAllProcess(IEnumWbemClassObject *pEnum, CString csLog)
{
        HRESULT hRes = WBEM_S_NO_ERROR;
    ///////////////////////////////
    try
    {
        // Final Next will return WBEM_S_FALSE
        while(WBEM_S_NO_ERROR == hRes)
        {
            ULONG uReturned = 0;
            IWbemClassObject *iwcObj[10];
            //
            hRes = pEnum->Next(WBEM_INFINITE, 10, iwcObj, &uReturned);
            if(SUCCEEDED(hRes))
            {
                if(uReturned > 0)
                {
                    // Do something with the objects.
                    for(ULONG n = 0; n<uReturned; n++)
                    {
                                CComVariant cvtName;
                        HRESULT hr = iwcObj[n]->Get(L"displayName", 0, &cvtName, 0, 0);
                        std::string strAVName = CW2A(cvtName.bstrVal);
                        //
                        CComVariant cvtState;
                        hr = iwcObj[n]->Get(L"productState", 0, &cvtState, 0, 0);
                        int iState = cvtState.intVal;
                        //
                        std::stringstream stream;
                        stream << std::hex << iState;
                        std::string result(stream.str());
                    }//for
                }
            }// If
        }// While
    }
    catch(...)
    {
        return hRes;
    }
        return hRes;
}

请审核代码并分享您的观点。

0 个答案:

没有答案