Resumen del desarrollo de funciones comunes de VC ++ (lista de artículos de columna, bienvenido a suscribirse, actualizaciones continuas ...) https://blog.csdn.net/chenlycly/article/details/124272585 Serie de tutoriales de solución de problemas de anomalías del software C ++ de entrada a la competencia (lista de artículos de la columna), bienvenido a suscribirse y continuar actualizando...) https://blog.csdn.net/chenlycly/article/details/125529931 Herramientas de análisis de software C ++ desde la entrada hasta la recopilación de casos de dominio (columna El artículo se está actualizando...) https://blog.csdn.net/chenlycly/article/details/131405795 Conceptos básicos y avanzados de C/C++ (artículo en columna, actualizado continuamente...) https://blog.csdn.net /chenlycly/category_11931267.html Este artículo se presenta a través de interfaces COM como INetFwMgr, INetFwPolicy e INetFwProfile que se utilizan para detectar si la protección contra incendios está habilitada y si el proceso actual se ha agregado a la lista blanca del firewall.
Para sistemas Windows 10, la página para habilitar/deshabilitar el firewall del sistema es la siguiente:
La página para configurar la comunicación a través del Firewall de Windows es la siguiente:
El código completo para usar el componente COM para detectar si el firewall está activado y si el programa puede comunicarse a través del firewall (agregar a la lista blanca de firewall) es el siguiente:
BOOL DetectFirewallSettings( BOOL* bExeEnableFW )
{
BOOL status = FALSE;
HRESULT hr = S_FALSE;
INetFwMgr* fwMgr = NULL;
INetFwPolicy* fwPolicy = NULL;
INetFwProfile* fwProfile = NULL;
INetFwAuthorizedApplications *apps = NULL;
INetFwAuthorizedApplication *app = NULL;
FW_ERROR_CODE ret = FW_NOERROR;
VARIANT_BOOL bFWEnabled;
VARIANT_BOOL bDoNoteAllowExceptions;
CoInitialize( NULL );
try
{
// 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( &fwProfile );
if( FAILED( hr ) )
{
throw FW_ERR_PROFILE;
}
// 1、是否开启了防火墙
hr = fwProfile->get_FirewallEnabled( &bFWEnabled );
if( FAILED( hr ) )
{
throw FW_ERR_FIREWALL_IS_ENABLED;
}
if( bFWEnabled )
{
// 开启了防火墙,再检测是否允许例外
hr = fwProfile->get_ExceptionsNotAllowed( &bDoNoteAllowExceptions );
if( FAILED(hr) )
{
throw FW_ERR_FIREWALL_IS_ENABLED;
}
if( bDoNoteAllowExceptions )
{
status = TRUE;
}
else
{
// 允许意外
// 2、得到授权应用程序
hr = fwProfile->get_AuthorizedApplications( &apps );
if( FAILED(hr) )
{
status = TRUE;
throw FW_ERR_FIREWALL_IS_ENABLED;
}
// 3、获取当前进程的进程名
TCHAR szPEBuff[MAX_PATH] = {0};
GetModuleFileName( NULL, szPEBuff, MAX_PATH );
BSTR bstrTemp = _bstr_t( szPEBuff );
// 看当前进程在不在允许列表中
hr = apps->Item( bstrTemp, &app );
if( FAILED(hr) )
{
// 不在允许列表中
status = TRUE;
throw FW_ERR_FIREWALL_IS_ENABLED;
}
else
{
//在允许列表中,查看是否允许通过防火墙
hr = app->get_Enabled( &bFWEnabled );
if( FAILED(hr) )
{
status = TRUE;
throw FW_ERR_FIREWALL_IS_ENABLED;
}
if( bFWEnabled != VARIANT_FALSE )
{
status = FALSE;
if ( bExeEnableFW != NULL )
{
*bExeEnableFW = true;
}
}
else
{
status = TRUE;
}
}
}
}
}
catch( FW_ERROR_CODE nError )
{
ret = nError;
}
if( fwPolicy )
{
fwPolicy->Release();
}
if( fwMgr )
{
fwMgr->Release();
}
CoUninitialize();
return status;
}
El valor de retorno de la interfaz anterior identifica si el muro de protección está habilitado y el parámetro saliente bExeEnableFW de la interfaz identifica si el programa actual puede comunicarse a través del firewall.