Microsoft Internet Security and Acceleration Server 2000

Add the Registration Code

This section describes how to add the registration code to your filter.

To add the registration code

  1. In the Visual C++ Project Settings dialog box, select the C/C++ tab, and from the Category drop-down menu select Preprocessor. In the Additional include directories entry field, add to the include path of the project the appropriate directory for your compilation. This ensures that the compiler can find the msfpccom.tlb file (if the file is not located in a current include directory). Type the same path information in the Additional include directories entry field of the MIDL tab.
  2. In Project Settings, select the C++ tab, and in the C++ Language Category drop-down menu, click Enable Exception Handling, and click OK.
  3. Add the following code to StdAfx.h, before the #include Wspfwext.h directive:
    #import "msfpccom.tlb" rename_namespace("MSFPCCOM") named_guids
    //define the GUID string with your filter GUID value
    #define FILTER_GUID_STRING "{7CCF6FF0-D995-4da1-B452-4228047B2D1D}"
    //define the FILTER_PROTOCOL_GUID
    static const GUID FILTER_PROTOCOL_GUID = { 0x8226e96a, 0xb578, 0x4c28, { 0xb6, 0x86, 0x7e, 0xc9, 0x2b, 0xaf, 0x1e, 0xe7 } }; 
    #define FILTER_PROTOCOL_GUID_STRING "{8226E96A-B578-4c28-B686-7EC92BAF1EE7}"
    

    In SMTPFltr.cpp, add the following code to your filter registration code:

    // This function is used to register an application filter
    static HRESULT RegisterFWXFilter (BOOL fRegister, FwxScope Scope)
    {
    	HRESULT hr;
    	MSFPCCOM::IFPCFilterProtocolPtr comptrIFPCFilterProtocol;
    	CComPtr<IFWXFilterAdmin> pIFWXFilterAdmin;
     
    	hr = CoCreateInstance (CLSID_FWXFilterAdmin,
    				NULL,
    				CLSCTX_SERVER,
    				IID_IFWXFilterAdmin,
    				(LPVOID *) &pIFWXFilterAdmin);
    	if (FAILED(hr))
    		return hr;
    
    	if (fRegister)
    	{
    		//Strings should be Unicode in this call
    		hr = pIFWXFilterAdmin->
    		InstallFilter (CLSID_SMTPFilter,// GUID
    
    					L"SMTPFilter",// Name
    					L"Screen SMTP messages",// Description
    					L"Microsoft",// Vendor
    					L"1.0",// Version
    					NULL,//Reserved parameter, must be NULL
    					Scope);
    		//OK if already installed.
    		if (hr == HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS))
    			hr = S_OK;
    
    		if (FAILED(hr))
    			return hr;
    
    		hr = pIFWXFilterAdmin
    			 ->RegisterProtocolForFilter(CLSID_SMTPFilter,
    										 FILTER_PROTOCOL_GUID,
    										 L"SMTP server",
    										 L"SMTP inbound via SMTPFLTR",
    										 NULL,//Reserved parameter, must be NULL
    										 Scope);
      
    		// Returns OK if already installed.
    		if (hr == HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS))
    			hr = S_OK;
    
    		if (FAILED(hr))
    			return hr;
    
    		//Get a pointer to the protocol just registered
    		hr = pIFWXFilterAdmin->GetProtocol(CLSID_SMTPFilter,
    									FILTER_PROTOCOL_GUID,
    									Scope,
    									(struct IFPCFilterProtocol **)&comptrIFPCFilterProtocol);
    
    		if (FAILED(hr))
    			return hr;
    
    		try
    		{
    
    			//Add primary connection to the protocol just created
    			comptrIFPCFilterProtocol->PrimaryConnections->AddTCP(MSFPCCOM::fpcInbound,
    											25,
    											25);
    
    			comptrIFPCFilterProtocol->PrimaryConnections->Save(); 
    	}
    		catch(_com_error& err)
    		{
    			//If adding a primary connection to the protocol fails, it is not considered an error
    			if(err.Error() != HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS))
    				return err.Error();
    	}
    
    }
    	else
    	{
    		//Unregister protocol
    		hr = pIFWXFilterAdmin->UnregisterProtocolForFilter(CLSID_SMTPFilter,
    														 FILTER_PROTOCOL_GUID,
    														 Scope);
    		//Unregister filter
    		hr = pIFWXFilterAdmin->UninstallFilter (CLSID_SMTPFilter, Scope);
    
    }
    
    	return hr;
    }
    //////////
    // DllInstall - Application filter installation
    STDAPI
    DllInstall(
    	BOOL bInstall,
    	LPCWSTR pszCmdLine
    )
    {
    	FwxScope Scope;
    	// Must have command line, must be either enterprise or array
    	if (!pszCmdLine) return E_INVALIDARG;
    	if (wcscmp(pszCmdLine, L"enterprise") == 0) {
    		Scope = fwx_EnterpriseScope;
    } else if (wcscmp(pszCmdLine, L"array") == 0) {
    		Scope = fwx_ArrayScope;
    } else {
    		return E_INVALIDARG;
    }
    
    	return RegisterFWXFilter(bInstall, Scope);
    }
    
  4. Add the following code to smtpfltr.def:
    DllInstall		@5 PRIVATE
    
  5. In the implementation of IFWXSessionFilter::FirewallEventHandler in FWXSessionFilter.cpp, add the code shown in bold to set the connection protocol as the filter protocol:
    HRESULT STDMETHODCALLTYPE CFWXSessionFilter::FirewallEventHandler( 
    	/* [out][in] */ const FwxFirewallEvent __RPC_FAR *pFirewallEvent)
    {
    	HRESULT hr = S_OK;
    	IFWXConnection  *piConnection;
     
    	switch (pFirewallEvent->EventType)
    	{
    	// Bind request for TCP sockets
    	case fwx_Bind_Tcp:
    		OutputDebugString ("IFWXSessionFilter::FirewallEventHandler –		fwx_Bind_Tcp\n");
    		OutputDebugString (FilterAccessString(
    						 pFirewallEvent->
    						 Parameters.Bind.FilterAccess));
     
    		//Get a pointer to the connection
    		piConnection=pFirewallEvent->Parameters.Bind.piConnection;
     
    		//Set the protocol of this connection to be the filter
    		//protocol
    		hr = piConnection->SetProtocol(FILTER_PROTOCOL_GUID,0);
     
    		if (FAILED(hr))
    		{
    			OutputDebugString("\tFail to set protocol of connection to  SMTP filter protocol\n");
    			return hr;
    	}
     
    		break;
    
  6. Build the project (Rebuild All).

To test the registration and installation code, follow the setup procedures described in Filter Setup, as appropriate for an enterprise or array installation. You will need to reinstall the filter when you complete the filter creation procedure.

Note  Filter objects are created when the Firewall service is loaded, so the service must be restarted when you add a new filter or a new version of an existing filter.