Unable to retrieve AntiVirus details from Windows Server 2016 using WMI

0

We are using WMI services for retrieving AntiVirus details (name of AntiVirus and installed status) from Windows. And the code works fine for Windows 7, 8.1, 10, but it failed on Windows Server 2016 OS. So kindly help us to solve this problem.Here is the code we are using:

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;
}

Kindly review the code and share your views.

c++
wmi
antivirus
asked on Stack Overflow Mar 21, 2018 by Jithin Jose • edited Mar 21, 2018 by Jithin Jose

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0