大家开发网络程序,经常要连接其他主机,如果在xp上运行,一定会提示你,只有选择解除阻止才能实现正常的网络连接.那么有没有办法在防火墙的例外列表里面通过编程的方式加入自己的程序呢?
 当然有了,不然就不要介绍了
xp的系统目录下面有个hnetcfg.dll就是这个编程接口,头文件是netfw.h,初始化代码如下:
INetFwProfile* m_pFireWallProfile=NULL;
		
		
				 HRESULT hr 
				=
				 S_FALSE;
				HRESULT hr 
				=
				 S_FALSE;
 INetFwMgr
				*
				 fwMgr 
				=
				 NULL;
    INetFwMgr
				*
				 fwMgr 
				=
				 NULL;
 INetFwPolicy
				*
				 fwPolicy 
				=
				 NULL;
    INetFwPolicy
				*
				 fwPolicy 
				=
				 NULL;

 FW_ERROR_CODE ret 
				=
				 FW_NOERROR;
    FW_ERROR_CODE ret 
				=
				 FW_NOERROR;
 try
    
				try
				
						
						 
						 
    
				
						 {
				
				
						{
 if
						( m_pFireWallProfile )
        
						if
						( m_pFireWallProfile )
 throw
						 FW_ERR_INITIALIZED;
            
						throw
						 FW_ERR_INITIALIZED;

 //
						 Create an instance of the firewall settings manager.
        
						//
						 Create an instance of the firewall settings manager.
						
								
								 hr 
						=
						 CoCreateInstance( __uuidof(NetFwMgr), NULL, CLSCTX_INPROC_SERVER, __uuidof( INetFwMgr), (
						void
						**
						)
						&
						fwMgr );
						
						        hr 
						=
						 CoCreateInstance( __uuidof(NetFwMgr), NULL, CLSCTX_INPROC_SERVER, __uuidof( INetFwMgr), (
						void
						**
						)
						&
						fwMgr );

 if
						( FAILED( hr ))
        
						if
						( FAILED( hr ))
 throw
						 FW_ERR_CREATE_SETTING_MANAGER;
            
						throw
						 FW_ERR_CREATE_SETTING_MANAGER;

 //
						 Retrieve the local firewall policy.
        
						//
						 Retrieve the local firewall policy.
						
								
								 hr 
						=
						 fwMgr
						->
						get_LocalPolicy( 
						&
						fwPolicy );
						
						        hr 
						=
						 fwMgr
						->
						get_LocalPolicy( 
						&
						fwPolicy );
 if
						( FAILED( hr ))
        
						if
						( FAILED( hr ))
 throw
						 FW_ERR_LOCAL_POLICY;
            
						throw
						 FW_ERR_LOCAL_POLICY;

 //
						 Retrieve the firewall profile currently in effect
        
						//
						 Retrieve the firewall profile currently in effect
						
								
								 hr 
						=
						 fwPolicy
						->
						get_CurrentProfile( 
						&
						m_pFireWallProfile );
						
						        hr 
						=
						 fwPolicy
						->
						get_CurrentProfile( 
						&
						m_pFireWallProfile );
 if
						( FAILED( hr ))
        
						if
						( FAILED( hr ))
 throw
						 FW_ERR_PROFILE;
            
						throw
						 FW_ERR_PROFILE;

 }
    }
				
				
						
						 catch
				( FW_ERROR_CODE nError)
    
				catch
				( FW_ERROR_CODE nError)

 
    
				
						 {
				
				
						{
 ret 
						=
						 nError;
        ret 
						=
						 nError;
 }
    }
				
				
						
						 
						
						 if
				( fwPolicy )
    
				if
				( fwPolicy )
 fwPolicy
				->
				Release();
        fwPolicy
				->
				Release();
 if
				( fwMgr )
    
				if
				( fwMgr )
 fwMgr
				->
				Release();
        fwMgr
				->
				Release();

 return
				 ret;
    
				return
				 ret;
		 将程序名称加入例外列表:
 WinXPSP2FireWall::AddApplication( const wchar_t* lpszProcessImageFileName, const wchar_t* lpszRegisterName )
WinXPSP2FireWall::AddApplication( const wchar_t* lpszProcessImageFileName, const wchar_t* lpszRegisterName )


 {
{
 FW_ERROR_CODE ret = FW_NOERROR;
    FW_ERROR_CODE ret = FW_NOERROR;
 HRESULT hr;
    HRESULT hr;
 BOOL bAppEnable;
    BOOL bAppEnable;
 BSTR bstrProcessImageFileName = NULL;
    BSTR bstrProcessImageFileName = NULL;
 BSTR bstrRegisterName = NULL;
    BSTR bstrRegisterName = NULL;
 INetFwAuthorizedApplication* pFWApp = NULL;
    INetFwAuthorizedApplication* pFWApp = NULL;
 INetFwAuthorizedApplications* pFWApps = NULL;
    INetFwAuthorizedApplications* pFWApps = NULL;

 try
    try

 
     {
{
 if( m_pFireWallProfile == NULL )
        if( m_pFireWallProfile == NULL )
 throw FW_ERR_INITIALIZED;
            throw FW_ERR_INITIALIZED;
 if( lpszProcessImageFileName == NULL || lpszRegisterName  == NULL )
        if( lpszProcessImageFileName == NULL || lpszRegisterName  == NULL )
 throw FW_ERR_INVALID_ARG;
            throw FW_ERR_INVALID_ARG;

 // First of all, check the application is already authorized;
        // First of all, check the application is already authorized;
 FW_ERROR_CODE  nError = this->IsAppEnabled( lpszProcessImageFileName, bAppEnable );
        FW_ERROR_CODE  nError = this->IsAppEnabled( lpszProcessImageFileName, bAppEnable );
 if( nError != FW_NOERROR )
        if( nError != FW_NOERROR )
 throw nError;
            throw nError;

 // Only add the application if it isn't authorized
        // Only add the application if it isn't authorized
 if( bAppEnable == FALSE )
        if( bAppEnable == FALSE )

 
         {
{
 // Retrieve the authorized application collection
            // Retrieve the authorized application collection
 hr = m_pFireWallProfile->get_AuthorizedApplications( &pFWApps );
            hr = m_pFireWallProfile->get_AuthorizedApplications( &pFWApps );
 if( FAILED( hr ))
            if( FAILED( hr ))
 throw FW_ERR_AUTH_APPLICATIONS;
                throw FW_ERR_AUTH_APPLICATIONS;

 // Create an instance of an authorized application
            // Create an instance of an authorized application
 hr = CoCreateInstance( __uuidof(NetFwAuthorizedApplication), NULL, CLSCTX_INPROC_SERVER, __uuidof(INetFwAuthorizedApplication), (void**)&pFWApp);
            hr = CoCreateInstance( __uuidof(NetFwAuthorizedApplication), NULL, CLSCTX_INPROC_SERVER, __uuidof(INetFwAuthorizedApplication), (void**)&pFWApp);
 if( FAILED( hr ))
            if( FAILED( hr ))
 throw FW_ERR_CREATE_APP_INSTANCE;
                throw FW_ERR_CREATE_APP_INSTANCE;

 // Allocate a BSTR for the Process Image FileName
            // Allocate a BSTR for the Process Image FileName
 bstrProcessImageFileName = SysAllocString( lpszProcessImageFileName );
            bstrProcessImageFileName = SysAllocString( lpszProcessImageFileName );
 if( SysStringLen( bstrProcessImageFileName ) == 0)
            if( SysStringLen( bstrProcessImageFileName ) == 0)
 throw FW_ERR_SYS_ALLOC_STRING;
                throw FW_ERR_SYS_ALLOC_STRING;

 // Set the process image file name
            // Set the process image file name
 hr = pFWApp->put_ProcessImageFileName( bstrProcessImageFileName );
            hr = pFWApp->put_ProcessImageFileName( bstrProcessImageFileName );
 if( FAILED( hr ) )
            if( FAILED( hr ) )
 throw FW_ERR_PUT_PROCESS_IMAGE_NAME;
                throw FW_ERR_PUT_PROCESS_IMAGE_NAME;

 // Allocate a BSTR for register name
            // Allocate a BSTR for register name
 bstrRegisterName = SysAllocString( lpszRegisterName );
            bstrRegisterName = SysAllocString( lpszRegisterName );
 if( SysStringLen( bstrRegisterName ) == 0)
            if( SysStringLen( bstrRegisterName ) == 0)
 throw FW_ERR_SYS_ALLOC_STRING;
                throw FW_ERR_SYS_ALLOC_STRING;
 // Set a registered name of the process
            // Set a registered name of the process
 hr = pFWApp->put_Name( bstrRegisterName );
            hr = pFWApp->put_Name( bstrRegisterName );
 if( FAILED( hr ))
            if( FAILED( hr ))
 throw FW_ERR_PUT_REGISTER_NAME;
                throw FW_ERR_PUT_REGISTER_NAME;
 
            
 // Add the application to the collection
            // Add the application to the collection
 hr = pFWApps->Add( pFWApp );
            hr = pFWApps->Add( pFWApp );
 if( FAILED( hr ))
            if( FAILED( hr ))
 throw FW_ERR_ADD_TO_COLLECTION;
                throw FW_ERR_ADD_TO_COLLECTION;
 }
        }
 }
    }
 catch( FW_ERROR_CODE nError )
    catch( FW_ERROR_CODE nError )

 
     {
{
 ret = nError;
        ret = nError;
 }
    }

 SysFreeString( bstrProcessImageFileName );
    SysFreeString( bstrProcessImageFileName );
 SysFreeString( bstrRegisterName );
    SysFreeString( bstrRegisterName );

 if( pFWApp )
    if( pFWApp )
 pFWApp->Release();
        pFWApp->Release();
 if( pFWApps )
    if( pFWApps )
 pFWApps->Release();
        pFWApps->Release();

 return ret;
    return ret;
 }
}