Поделиться через


Windows Firewall interfaces, INetFwProducts, and InetFwProduct.

 

Jeff here again. Today I have samples of the Windows Firewall interfaces, INetFwProducts, and InetFwProduct.

#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <netfw.h> 

#pragma comment( lib, "ole32.lib" ) 

// Forward declarations

HRESULT    FWProductsCOMInitialize(INetFwProducts** ppFwProducts); 

int _tmain(int argc, _TCHAR* argv[])
{
    HRESULT hrComInit = S_OK;
    HRESULT hr = S_OK;
    long cnt = 0;
    INetFwProduct *pFwProduct = NULL;
    INetFwProducts *pFwProducts = NULL; 

    // Initialize COM.

    hrComInit = CoInitializeEx(0, COINIT_APARTMENTTHREADED );  

    if (hrComInit != RPC_E_CHANGED_MODE)
    {
        if (FAILED(hrComInit))
        {
            printf("CoInitializeEx failed: 0x%08lx\n", hrComInit);
            goto Cleanup;
        }
    } 

       hr = FWProductsCOMInitialize(&pFwProducts);

    if (FAILED(hr))
    {
        goto Cleanup;
    } 

       hr = pFwProducts->get_Count(&cnt);

       if (SUCCEEDED(hr))
       {
              printf("get_Count: %d\n",cnt);
              hr = pFwProducts->Item(cnt - 1 , &pFwProduct);

              if (SUCCEEDED(hr))
              {
                     BSTR bsDisplayName = NULL;
                     hr = pFwProduct->get_DisplayName(&bsDisplayName);

                     if (SUCCEEDED(hr))
                     {
                           printf("Display name: %S\n", bsDisplayName);
                           SysFreeString(bsDisplayName);
                     } 

                     BSTR bsExe = NULL;

                     hr = pFwProduct->get_PathToSignedProductExe(&bsExe);

                     if (SUCCEEDED(hr))
                     {

                           printf("Path to signed exe: %S\n", bsExe);
                           SysFreeString(bsExe);

                     }

 

                     IUnknown *pUnkNewEnum;                    
                     hr = pFwProducts->get__NewEnum(&pUnkNewEnum);

                     if (SUCCEEDED(hr))
                     {

                           IEnumVARIANT*pUnkVariant;

                           hr = pUnkNewEnum->QueryInterface(IID_IEnumVARIANT,(void**) &pUnkVariant);

                           if (SUCCEEDED(hr))
                           {

                                  do
                                  {

                                         ULONG celt=1;
                                         VARIANT var1[1];
                                         VariantInit(&var1[0]);
                                         ULONG CeltReturned=0; 

                                         hr = pUnkVariant->Next(celt,&var1[0],&CeltReturned); 

                                         if (SUCCEEDED(hr))
                                         {

                                                if(S_FALSE==hr)
                                                {
                                                      //noop possible

                                                }
                                                else
                                                {

                                                       switch (var1[0].vt)
                                                       {

                                                           case VT_UNKNOWN:
                                                              case VT_DISPATCH:

                                                              {
                                                                     INetFwProduct  *pFwProductQI = NULL;
                                                                     HRESULT hrQI;
                                                                     hrQI =var1[0].punkVal->QueryInterface(IID_INetFwProduct, (void**)&pFwProductQI);

                                                                     if(SUCCEEDED(hrQI))
                                                                     {

                                                                           HRESULT hrDisplayName;

                                                                           BSTR displayNameEnum;

                                                                           hrDisplayName = pFwProductQI->get_DisplayName(&displayNameEnum);

                                                                           if(SUCCEEDED(hrQI))

                                                                           {
                                                                                  printf("\nEnum Display Name: %S\n", displayNameEnum); 

                                                                                  //clean-up

                                                                                  SysFreeString(displayNameEnum);

                                                                                  VariantClear(&var1[0]);

                                                                           }

                                                                     }

                                                              }

                                                              break;

                                                       default:

                                                              break;

                                                       }

                                                }

                                         }

                                  }

                                  while(hr !=S_FALSE && !FAILED(hr));

                           }

                     }

              }

       }

 

Cleanup:

 

    // Release INetFwPolicy2

    if (pFwProduct != NULL)
    {
        pFwProduct->Release();
    }

     // Uninitialize COM.

    if (SUCCEEDED(hrComInit))
    {
        CoUninitialize();

    }  

    return 0;

HRESULT FWProductsCOMInitialize(INetFwProducts** ppFwProducts)

{
      HRESULT hr = S_OK;

 

    hr = CoCreateInstance(         __uuidof(NetFwProducts),

        NULL,

        CLSCTX_INPROC_SERVER,

        __uuidof(INetFwProducts),

        (void**)ppFwProducts);

     if (FAILED(hr))
    {
        printf("CoCreateInstance for INetFwProducts failed: 0x%08lx\n", hr);

        goto Cleanup;       

    } 

Cleanup:

    return hr;

For these APIs to work, the Windows Firewall Service needs to be running, otherwise get_Count() returns a bogus number. These APIs only work with 3rd party firewalls, they won’t return the Microsoft firewall.

 

Follow us on Twitter, www.twitter.com/WindowsSDK.

 

/Jeff