0

I am trying to locate a specific rule in Firewall in c++ using the below code,

HRESULT WFCOMInitialize(INetFwPolicy2** ppNetFwPolicy2)
{
    HRESULT hr = S_OK;
    hr = CoCreateInstance(
            __uuidof(NetFwPolicy2),
            NULL,
            CLSCTX_INPROC_SERVER,
            __uuidof(INetFwPolicy2),
            (void**)ppNetFwPolicy2); 
    return hr;
}

int _tmain(int argc, _TCHAR* argv[])
{  

    CComVariant     var;
    IUnknown        *pEnumerator; 
    BSTR            bstrName; 
    HRESULT         hrComInit = S_OK;
    HRESULT         hr = S_OK;
    ULONG           cFetched = 0;
    IEnumVARIANT*   pVariant = NULL;
    INetFwPolicy2   *pNetFwPolicy2 = NULL;
    INetFwRules     *pFwRules = NULL;
    INetFwRule      *pFwRule = NULL; 

    hrComInit = CoInitialize(NULL); 
    if (hrComInit != RPC_E_CHANGED_MODE)
        if (FAILED(hrComInit))
            goto Cleanup; 


    hr = WFCOMInitialize(&pNetFwPolicy2);  

    hr = pNetFwPolicy2->get_Rules(&pFwRules);   
    if (FAILED(hr))
        goto Cleanup;  

    pFwRules->get__NewEnum(&pEnumerator);
    if (pEnumerator)
        hr = pEnumerator->QueryInterface(__uuidof(IEnumVARIANT), (void **)&pVariant); 

    while (SUCCEEDED(hr) && hr != S_FALSE)
    {
        var.Clear();
        hr = pVariant->Next(1, &var, &cFetched);

        if (S_FALSE != hr)
        {
            if (SUCCEEDED(hr))
                hr = var.ChangeType(VT_DISPATCH);

            if (SUCCEEDED(hr))
                hr = (V_DISPATCH(&var))->QueryInterface(__uuidof(INetFwRule3), reinterpret_cast<void**>(&pFwRule)); 

            if (SUCCEEDED(hr))
                if (SUCCEEDED(pFwRule->get_Name(&bstrName))) 
                    if(!wcscmp(bstrName, L"Mail, Calendar, and People"))
                        CurrentProfilesBitMask++;  
        }
    } 

Cleanup: 
    if (pFwRule)
        pFwRule->Release(); 
    if (pNetFwPolicy2)
        pNetFwPolicy2->Release(); 
    if (SUCCEEDED(hrComInit))
        CoUninitialize(); 

    return 0;
}

I can find the Rule named Mail, Calendar, and People in firewall. But using the QueryInterface it is not available(ie, if(!wcscmp(bstrName, L"Mail, Calendar, and People")) is failure for all rules). QueryInterface is success but The string comparison is not match. but the rule is in firewall.

I have tried this VBScript provided in msdn and the result is same as above. The rule named can not be locate by VBScript code or C++ code.

  • It is not clear from the question whether QueryInterface() failed or the string comparison is not a match. Assuming the former, do keep in mind that you are asking for an interface that is only available on later Windows versions. You did not say anything about the version. There is no obvious reason to do so, you only need the INetFwRule interface to use the Name property. – Hans Passant May 18 '17 at 08:45
  • Edited the post, QueryInterface is success and string comparison is failing. But the rule with corresponding name is in firewall. – Marshal Sebastian May 19 '17 at 05:29

0 Answers0