예제 #1
0
//
// Purpose: 
//   Entry point for the process
//
// Parameters:
//   None
// 
// Return value:
//   None
//
void __cdecl main() 
{ 
    DWORD result = NO_ERROR;
	HRESULT hr = S_OK;
    INetFwProduct* product = NULL;
    INetFwProducts* products = NULL;
    IUnknown* registration = NULL;
    BSTR displayName = NULL;
    VARIANT varCategories = { VT_EMPTY };
   
    long count=0;
    BOOL comInit =  FALSE;

    

	displayName = SysAllocString(L"@RegisterWithoutCategoryOwnership.exe,-127");
    BAIL_ON_ALLOC_FAILURE(displayName, SysAllocString);

    hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
    if (FAILED(hr))
    {
        //COM initialize failed
        wprintf(L"CoInitialize failed: 0x%08lx\n", hr);
        goto CLEANUP;
    }
    comInit = TRUE;


    hr = CoCreateInstance(__uuidof(NetFwProduct),NULL,CLSCTX_INPROC_SERVER,__uuidof(INetFwProduct),(void**)&product );

    if (FAILED(hr))
    {
        //CoCreateInstance Failed
        wprintf(L"CoCreateInstance for INetFwProduct failed: 0x%08lx\n", hr);
        goto CLEANUP;
    }

    hr = product->put_DisplayName(displayName);

    if (FAILED(hr))
    {
        //Put_displayName failed
        wprintf(L"put_DisplayName for INetFwProduct failed Error: 0x%08lx\n", hr);
        goto CLEANUP;
    }
    hr = product->put_RuleCategories(varCategories);

    if (FAILED(hr))
    {
        //Put_rulecategories failed
        wprintf(L"put_RuleCategories failed for INetFwProduct Error: 0x%08lx\n", hr);
        goto CLEANUP;
    }


    hr = CoCreateInstance(__uuidof(NetFwProducts),NULL,CLSCTX_INPROC_SERVER,__uuidof(INetFwProducts),(void**)&products );

    if (FAILED(hr))
    {
        //CoCreateInstance Failed
        wprintf(L"CoCreateInstance for INetFwProducts failed: 0x%08lx\n", hr);
        goto CLEANUP;
    }

    hr = products->Register(product, &registration);
    if (FAILED(hr))
    {
        //Failed to Register Products
        wprintf(L"Register failed: 0x%08lx\n", hr);
        goto CLEANUP;
    }			

    hr = products->get_Count( &count);
    if (FAILED(hr))
    {
        //Failed to get Count of Products
        wprintf(L"Get count failed: 0x%08lx\n", hr);
        goto CLEANUP;
    }		
    wprintf(L"INetFwProducts_Count returned %ld.\n", count);

    wprintf(L"Hit any key to unregister.\n");
    
	_getch();



CLEANUP:
	if (registration != NULL)
   {
      registration->Release();
   }
   if (products != NULL)
   {
      products->Release();
   }
   if (product != NULL)
   {
      product->Release();
   }
   if (comInit)
   {
      CoUninitialize();
   }
   
   SysFreeString(displayName);
   VariantClear(&varCategories);
   return;
} 
UINT32 WindowsFirewallAcquireFirewallCategory()
{
    UINT32 status = NO_ERROR;

    if(HlprServiceQueryState(L"MPSSvc") == SERVICE_RUNNING)
    {
        BSTR            pProductName      = SysAllocString(g_pServiceName);
        INetFwProduct*  pNetFwProduct     = 0;
        INetFwProducts* pNetFwProducts    = 0;
        SAFEARRAY*      pFirewallCategory = 0;
        SAFEARRAY*      pBootTimeCategory = 0;
        SAFEARRAYBOUND  boundary          = {0};
        VARIANT         firewallCategory  = {0};
        VARIANT         boottimeCategory  = {0};

        if(pProductName == 0)
        {
            status = ERROR_GEN_FAILURE;

            ServiceEventLogError(L"WindowsFirewallAcquireFirewallCategory : SysAllocString()",
                                 status);

            HLPR_BAIL;
        }

        boundary.lLbound   = 0;
        boundary.cElements = 1;

        pBootTimeCategory = SafeArrayCreate(VT_VARIANT,
                                            1,
                                            &boundary);
        if(pBootTimeCategory == 0)
        {
            status = ERROR_GEN_FAILURE;

            ServiceEventLogError(L"WindowsFirewallAcquireFirewallCategory : SafeArrayCreate()",
                                 status);

            HLPR_BAIL;
        }

        V_VT((VARIANT*)pBootTimeCategory->pvData) = VT_I4;
        V_I4((VARIANT*)pBootTimeCategory->pvData) = NET_FW_RULE_CATEGORY_BOOT;

        VariantInit(&firewallCategory);

        V_VT(&boottimeCategory)    = VT_ARRAY | VT_VARIANT;
        V_ARRAY(&boottimeCategory) = pBootTimeCategory;

        pFirewallCategory = SafeArrayCreate(VT_VARIANT,
                                            1,
                                            &boundary);
        if(pFirewallCategory == 0)
        {
            status = ERROR_GEN_FAILURE;

            ServiceEventLogError(L"WindowsFirewallAcquireFirewallCategory : SafeArrayCreate()",
                                 status);

            HLPR_BAIL;
        }

        V_VT((VARIANT*)pFirewallCategory->pvData) = VT_I4;
        V_I4((VARIANT*)pFirewallCategory->pvData) = NET_FW_RULE_CATEGORY_FIREWALL;

        VariantInit(&firewallCategory);

        V_VT(&firewallCategory)    = VT_ARRAY | VT_VARIANT;
        V_ARRAY(&firewallCategory) = pFirewallCategory;

        /// Initialize the COM library
        if(!g_isCOMInitialized)
        {
            status = CoInitializeEx(0,
                                    COINIT_MULTITHREADED);
            if(FAILED(status))
            {
                ServiceEventLogError(L"WindowsFirewallAcquireFirewallCategory : CoInitializeEx()",
                                     status);

                HLPR_BAIL;
            }

            g_isCOMInitialized = TRUE;
        }

        /// Create an instance of the NetFwProduct class
        status = CoCreateInstance(__uuidof(NetFwProduct),
                                  0,
                                  CLSCTX_INPROC_SERVER,
                                  __uuidof(INetFwProduct),
                                  (LPVOID*)&pNetFwProduct);
        if(FAILED(status))
        {
            ServiceEventLogError(L"WindowsFirewallAcquireFirewallCategory : CoCreateInstance()",
                                 status);

            HLPR_BAIL;
        }

        /// Set the DisplayName
        status = pNetFwProduct->put_DisplayName(pProductName);
        if(FAILED(status))
        {
            ServiceEventLogError(L"WindowsFirewallAcquireFirewallCategory : INetFwProduct::put_DisplayName()",
                                 status);

            HLPR_BAIL;
        }

        /// Take the category
        status = pNetFwProduct->put_RuleCategories(boottimeCategory);
        if(FAILED(status))
        {
            ServiceEventLogError(L"WindowsFirewallAcquireFirewallCategory : INetFwProduct::put_RuleCategories()",
                                 status);

            HLPR_BAIL;
        }

        status = pNetFwProduct->put_RuleCategories(firewallCategory);
        if(FAILED(status))
        {
            ServiceEventLogError(L"WindowsFirewallAcquireFirewallCategory : INetFwProduct::put_RuleCategories()",
                                 status);

            HLPR_BAIL;
        }

        /// Create an instance of the NetFwProducts class
        status = CoCreateInstance(__uuidof(NetFwProducts),
                                  0,
                                  CLSCTX_INPROC_SERVER,
                                  __uuidof(INetFwProducts),
                                  (LPVOID*)&pNetFwProducts);
        if(FAILED(status))
        {
            ServiceEventLogError(L"WindowsFirewallAcquireFirewallCategory : CoCreateInstance()",
                                 status);

            HLPR_BAIL;
        }

        /// Register as a Firewall Product
        if(pFirewallRegistrationHandle == 0)
        {
            status = pNetFwProducts->Register(pNetFwProduct,
                                              &pFirewallRegistrationHandle);
            if(FAILED(status))
            {
                ServiceEventLogError(L"WindowsFirewallAcquireFirewallCategory : INetFwProducts::Register()",
                                     status);

                HLPR_BAIL;
            }
        }

        status = NO_ERROR;

HLPR_BAIL_LABEL:

        if(FAILED(status))
            WindowsFirewallReleaseFirewallCategory();

        if(pNetFwProducts)
            pNetFwProducts->Release();

        if(pNetFwProduct)
            pNetFwProduct->Release();

        if(pProductName)
            SysFreeString(pProductName);
    }
    else
    {
        HLPR_NEW(pWFNotifyThread,
                 THREAD_DATA);
        if(pWFNotifyThread)
        {
            pWFNotifyThread->threadStartRoutine = (LPTHREAD_START_ROUTINE)WindowsFirewallNotifyThreadStartRoutine;

            status = HlprThreadStart(pWFNotifyThread);
        }
        else
            status = ERROR_OUTOFMEMORY;
    }

    if(HlprServiceQueryState(L"WFPSampler") == SERVICE_START_PENDING)
        ServiceStatusReportToSCM(SERVICE_START_PENDING,
                                 status,
                                 2500);

    return status;
}