Certain Firewall rules are not accessible using QueryInterface

204 views Asked by At

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.

0

There are 0 answers