gpt4 book ai didi

c++ - 防火墙异常代码仅适用于传出连接

转载 作者:行者123 更新时间:2023-11-30 04:10:17 25 4
gpt4 key购买 nike

我从网上获取这段代码,为我的应用程序添加防火墙异常(exception):

STDAPI AddApplicationToExceptionListW( const WCHAR* strGameExeFullPath, const WCHAR* strFriendlyAppName )
{
HRESULT hr = E_FAIL;
bool bCleanupCOM = false;
BSTR bstrFriendlyAppName = NULL;
BSTR bstrGameExeFullPath = NULL;
INetFwAuthorizedApplication* pFwApp = NULL;
INetFwAuthorizedApplications* pFwApps = NULL;
INetFwProfile* pFwProfile = NULL;

#ifdef SHOW_DEBUG_MSGBOXES
WCHAR sz[1024];
StringCchPrintf( sz, 1024, L"strFriendlyAppName='%s' strGameExeFullPath='%s'", strFriendlyAppName, strGameExeFullPath );
MessageBox( NULL, sz, L"AddApplicationToExceptionListW", MB_OK );
#endif

if( strGameExeFullPath == NULL || strFriendlyAppName == NULL )
{
assert( false );
return E_INVALIDARG;
}

bstrGameExeFullPath = SysAllocString( strGameExeFullPath );
bstrFriendlyAppName = SysAllocString( strFriendlyAppName );
if( bstrGameExeFullPath == NULL || bstrFriendlyAppName == NULL )
{
hr = E_OUTOFMEMORY;
goto LCleanup;
}

hr = CoInitialize( 0 );
bCleanupCOM = SUCCEEDED( hr );

pFwProfile = GetFirewallProfile();
if( pFwProfile == NULL )
{
hr = E_FAIL;
goto LCleanup;
}

hr = pFwProfile->get_AuthorizedApplications( &pFwApps );
if( FAILED( hr ) )
goto LCleanup;

// Create an instance of an authorized application.
hr = CoCreateInstance( __uuidof( NetFwAuthorizedApplication ), NULL,
CLSCTX_INPROC_SERVER, __uuidof( INetFwAuthorizedApplication ), ( void** )&pFwApp );
if( FAILED( hr ) )
goto LCleanup;

// Set the process image file name.
hr = pFwApp->put_ProcessImageFileName( bstrGameExeFullPath );
if( FAILED( hr ) )
goto LCleanup;

// Set the application friendly name.
hr = pFwApp->put_Name( bstrFriendlyAppName );
if( FAILED( hr ) )
goto LCleanup;

// Add the application to the collection.
hr = pFwApps->Add( pFwApp );

LCleanup:
if( bstrFriendlyAppName ) SysFreeString( bstrFriendlyAppName );
if( bstrGameExeFullPath ) SysFreeString( bstrGameExeFullPath );
if( pFwApp ) pFwApp->Release();
if( pFwApps ) pFwApps->Release();
if( pFwProfile ) pFwProfile->Release();
if( bCleanupCOM ) CoUninitialize();

return hr;
}

当我尝试通过 Windows 防火墙发送数据时一切正常,但传入连接仍然被阻止。所以我必须禁用我的防火墙才能接收数据。我想,那个异常(exception)将允许所有连接(传出和传入)......有人知道我应该向这段代码添加什么以便接收传入数据吗?

最佳答案

仅仅自己添加应用是不够的。防火墙无法发现应用程序正在监听哪些端口的入站连接。您必须告诉防火墙应用程序正在使用哪个端口。你通过 INetFwProfile::GloballyOpenPorts 做到这一点集合,例如:

INetFwOpenPorts *pFwPorts = NULL;
INetFwOpenPort *pFWPort = NULL;

...

hr = pFwProfile->get_GloballyOpenPorts( &pFwPorts );
if( FAILED( hr ) )
goto LCleanup;

// Create an instance of an open port.
hr = CoCreateInstance( __uuidof( NetFwOpenPort ), NULL, CLSCTX_INPROC_SERVER, __uuidof( INetFwOpenPort ), ( void** )&pFwPort );
if( FAILED( hr ) )
goto LCleanup;

// Set the port number.
hr = pFWPort->put_Port( ... );
if( FAILED( hr ) )
goto LCleanup;

// Add the port to the collection.
hr = pFwPorts->Add( pFwPort );

...

if( pFwPort ) pFwPort->Release();
if( pFwPorts ) pFwPorts->Release();

关于c++ - 防火墙异常代码仅适用于传出连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20690462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com