将AP加入防火墙例外C++ 方法可对比C#方法参考
2011-12-01 15:24
316 查看
头文件 #include "netfw.h"
INetFwProfile* m_pFireWallProfile=NULL;
//int FW_NOERROR = 0;
//int FW_ERR_INITIALIZED = 1;
//int FW_ERR_CREATE_SETTING_MANAGER = 2;
//int FW_ERR_LOCAL_POLICY = 3;
//int FW_ERR_PROFILE = 4;
enum FW_ERROR_CODE {FW_NOERROR , FW_ERR_INITIALIZED, FW_ERR_CREATE_SETTING_MANAGER, FW_ERR_LOCAL_POLICY,FW_ERR_PROFILE,
FW_ERR_INVALID_ARG,FW_ERR_AUTH_APPLICATIONS,FW_ERR_CREATE_APP_INSTANCE,FW_ERR_SYS_ALLOC_STRING,FW_ERR_PUT_PROCESS_IMAGE_NAME,
FW_ERR_PUT_REGISTER_NAME,FW_ERR_ADD_TO_COLLECTION
} ;
int test(){
HRESULT hr = S_FALSE;
INetFwMgr * fwMgr = NULL;
INetFwPolicy * fwPolicy = NULL;
int ret = FW_NOERROR;
try
{
if ( m_pFireWallProfile )
throw FW_ERR_INITIALIZED;
CoInitialize(0);
// Create an instance of the firewall settings manager.
hr = CoCreateInstance( __uuidof(NetFwMgr), NULL, CLSCTX_INPROC_SERVER, __uuidof( INetFwMgr), ( void ** ) & fwMgr );
if ( FAILED( hr ))
throw FW_ERR_CREATE_SETTING_MANAGER;
// Retrieve the local firewall policy.
hr = fwMgr -> get_LocalPolicy( & fwPolicy );
if ( FAILED( hr ))
throw FW_ERR_LOCAL_POLICY;
// Retrieve the firewall profile currently in effect
hr = fwPolicy -> get_CurrentProfile( & m_pFireWallProfile );
if ( FAILED( hr ))
throw FW_ERR_PROFILE;
}
catch ( FW_ERROR_CODE nError)
{
ret = nError;
}
if ( fwPolicy )
fwPolicy -> Release();
if ( fwMgr )
fwMgr -> Release();
return ret; //将程序名称加入例外列表:
}
int AddApplication( const wchar_t* lpszProcessImageFileName, const wchar_t*
lpszRegisterName )
{
FW_ERROR_CODE ret = FW_NOERROR;
HRESULT hr;
BOOL bAppEnable = false;
BSTR bstrProcessImageFileName = NULL;
BSTR bstrRegisterName = NULL;
INetFwAuthorizedApplication* pFWApp = NULL;
INetFwAuthorizedApplications* pFWApps = NULL;
INetFwServices * services = NULL;
INetFwService * remote = NULL;
try
{
if( m_pFireWallProfile == NULL )
throw FW_ERR_INITIALIZED;
if( lpszProcessImageFileName == NULL || lpszRegisterName == NULL )
throw FW_ERR_INVALID_ARG;
// First of all, check the application is already authorized;
// FW_ERROR_CODE nError = this->IsAppEnabled( lpszProcessImageFileName, bAppEnable );
//if( nError != FW_NOERROR )
// throw nError;
// Only add the application if it isn't authorized
if( bAppEnable == FALSE )
{
// Retrieve the authorized application collection
hr = m_pFireWallProfile->get_AuthorizedApplications( &pFWApps );
if( FAILED( hr ))
throw FW_ERR_AUTH_APPLICATIONS;
// Create an instance of an authorized application
hr = CoCreateInstance( __uuidof(NetFwAuthorizedApplication), NULL, CLSCTX_INPROC_SERVER, __uuidof(INetFwAuthorizedApplication), (void**)&pFWApp);
if( FAILED( hr ))
throw FW_ERR_CREATE_APP_INSTANCE;
// Allocate a BSTR for the Process Image FileName
bstrProcessImageFileName = SysAllocString( lpszProcessImageFileName );
if( SysStringLen( bstrProcessImageFileName ) == 0)
throw FW_ERR_SYS_ALLOC_STRING;
// Set the process image file name
hr = pFWApp->put_ProcessImageFileName( bstrProcessImageFileName );
if( FAILED( hr ) )
throw FW_ERR_PUT_PROCESS_IMAGE_NAME;
// Allocate a BSTR for register name
bstrRegisterName = SysAllocString( lpszRegisterName );
if( SysStringLen( bstrRegisterName ) == 0)
throw FW_ERR_SYS_ALLOC_STRING;
// Set a registered name of the process
hr = pFWApp->put_Name( bstrRegisterName );
if( FAILED( hr ))
throw FW_ERR_PUT_REGISTER_NAME;
// Add the application to the collection
hr = pFWApps->Add( pFWApp );
if( FAILED( hr ))
throw FW_ERR_ADD_TO_COLLECTION;
}
}
catch( FW_ERROR_CODE nError )
{
ret = nError;
}
SysFreeString( bstrProcessImageFileName );
SysFreeString( bstrRegisterName );
if( pFWApp )
pFWApp->Release();
if( pFWApps )
pFWApps->Release();
return ret;
}
INetFwProfile* m_pFireWallProfile=NULL;
//int FW_NOERROR = 0;
//int FW_ERR_INITIALIZED = 1;
//int FW_ERR_CREATE_SETTING_MANAGER = 2;
//int FW_ERR_LOCAL_POLICY = 3;
//int FW_ERR_PROFILE = 4;
enum FW_ERROR_CODE {FW_NOERROR , FW_ERR_INITIALIZED, FW_ERR_CREATE_SETTING_MANAGER, FW_ERR_LOCAL_POLICY,FW_ERR_PROFILE,
FW_ERR_INVALID_ARG,FW_ERR_AUTH_APPLICATIONS,FW_ERR_CREATE_APP_INSTANCE,FW_ERR_SYS_ALLOC_STRING,FW_ERR_PUT_PROCESS_IMAGE_NAME,
FW_ERR_PUT_REGISTER_NAME,FW_ERR_ADD_TO_COLLECTION
} ;
int test(){
HRESULT hr = S_FALSE;
INetFwMgr * fwMgr = NULL;
INetFwPolicy * fwPolicy = NULL;
int ret = FW_NOERROR;
try
{
if ( m_pFireWallProfile )
throw FW_ERR_INITIALIZED;
CoInitialize(0);
// Create an instance of the firewall settings manager.
hr = CoCreateInstance( __uuidof(NetFwMgr), NULL, CLSCTX_INPROC_SERVER, __uuidof( INetFwMgr), ( void ** ) & fwMgr );
if ( FAILED( hr ))
throw FW_ERR_CREATE_SETTING_MANAGER;
// Retrieve the local firewall policy.
hr = fwMgr -> get_LocalPolicy( & fwPolicy );
if ( FAILED( hr ))
throw FW_ERR_LOCAL_POLICY;
// Retrieve the firewall profile currently in effect
hr = fwPolicy -> get_CurrentProfile( & m_pFireWallProfile );
if ( FAILED( hr ))
throw FW_ERR_PROFILE;
}
catch ( FW_ERROR_CODE nError)
{
ret = nError;
}
if ( fwPolicy )
fwPolicy -> Release();
if ( fwMgr )
fwMgr -> Release();
return ret; //将程序名称加入例外列表:
}
int AddApplication( const wchar_t* lpszProcessImageFileName, const wchar_t*
lpszRegisterName )
{
FW_ERROR_CODE ret = FW_NOERROR;
HRESULT hr;
BOOL bAppEnable = false;
BSTR bstrProcessImageFileName = NULL;
BSTR bstrRegisterName = NULL;
INetFwAuthorizedApplication* pFWApp = NULL;
INetFwAuthorizedApplications* pFWApps = NULL;
INetFwServices * services = NULL;
INetFwService * remote = NULL;
try
{
if( m_pFireWallProfile == NULL )
throw FW_ERR_INITIALIZED;
if( lpszProcessImageFileName == NULL || lpszRegisterName == NULL )
throw FW_ERR_INVALID_ARG;
// First of all, check the application is already authorized;
// FW_ERROR_CODE nError = this->IsAppEnabled( lpszProcessImageFileName, bAppEnable );
//if( nError != FW_NOERROR )
// throw nError;
// Only add the application if it isn't authorized
if( bAppEnable == FALSE )
{
// Retrieve the authorized application collection
hr = m_pFireWallProfile->get_AuthorizedApplications( &pFWApps );
if( FAILED( hr ))
throw FW_ERR_AUTH_APPLICATIONS;
// Create an instance of an authorized application
hr = CoCreateInstance( __uuidof(NetFwAuthorizedApplication), NULL, CLSCTX_INPROC_SERVER, __uuidof(INetFwAuthorizedApplication), (void**)&pFWApp);
if( FAILED( hr ))
throw FW_ERR_CREATE_APP_INSTANCE;
// Allocate a BSTR for the Process Image FileName
bstrProcessImageFileName = SysAllocString( lpszProcessImageFileName );
if( SysStringLen( bstrProcessImageFileName ) == 0)
throw FW_ERR_SYS_ALLOC_STRING;
// Set the process image file name
hr = pFWApp->put_ProcessImageFileName( bstrProcessImageFileName );
if( FAILED( hr ) )
throw FW_ERR_PUT_PROCESS_IMAGE_NAME;
// Allocate a BSTR for register name
bstrRegisterName = SysAllocString( lpszRegisterName );
if( SysStringLen( bstrRegisterName ) == 0)
throw FW_ERR_SYS_ALLOC_STRING;
// Set a registered name of the process
hr = pFWApp->put_Name( bstrRegisterName );
if( FAILED( hr ))
throw FW_ERR_PUT_REGISTER_NAME;
// Add the application to the collection
hr = pFWApps->Add( pFWApp );
if( FAILED( hr ))
throw FW_ERR_ADD_TO_COLLECTION;
}
}
catch( FW_ERROR_CODE nError )
{
ret = nError;
}
SysFreeString( bstrProcessImageFileName );
SysFreeString( bstrRegisterName );
if( pFWApp )
pFWApp->Release();
if( pFWApps )
pFWApps->Release();
return ret;
}
相关文章推荐
- 将AP加入防火墙例外C# 方法
- (钟豪原创) C#与C++中的方法的对比分析
- java、C++、C#、 Objective-c语言对比
- C++与C#对比学习:数字的故事
- C++与C#对常用数据结构封装的对比(STL vs System.Collections.Generic)
- C++转向C#的疑惑:C#中类间通信方法初探
- C++ VS C#(13):隐藏基类方法,部分类定义
- com调用的几种方法 及 C#调用C++编写的的COM DLL
- C++与C#对常用数据结构封装的对比(STL vs System.Collections.Generic)
- C#调用C++的dll两种方法(托管与非托管)
- C#调用C++的方法
- C++与C#中的虚函数、纯虚函数、抽象类的对比
- C#学习笔记 对比C++
- C++与C#编写的DLL/COM的各种调用方法
- C#调用C++动态库时出错的解决方法
- C# 参考之方法参数关键字:params、ref及out
- 陈广老师C#参考视频 方法的参数传递 总结
- C#.NET 通用权限管理系统组件 大数据多表分页获取部分列的参考方法
- C# 常用数据结构简单总结(与C++对比)
- c#调用c++方法介绍,window api