0

我正在尝试使用以下代码在 C++ 中的防火墙中找到特定规则,

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中提供的这个VBScript,结果和上面一样。VBScript 代码或 C++ 代码无法定位命名规则。

4

0 回答 0