使用QueryInterface无法访问某些防火墙规则

时间:2017-05-18 07:17:33

标签: c++ vbscript windows-firewall-api queryinterface

我正在尝试使用以下代码在c ++中找到Firewall中的特定规则,

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

我可以在防火墙中找到名为Mail, Calendar, and People的规则。但是使用QueryInterface它是不可用的(即,if(!wcscmp(bstrName, L"Mail, Calendar, and People"))是所有规则都失败的)。 QueryInterface成功但字符串比较不匹配。但规则是防火墙。

我在msdn中尝试了this VBScript,结果与上面相同。命名规则无法通过VBScript代码或C ++代码定位。

0 个答案:

没有答案