![]() |
Platforms, Frameworks & Libraries »
Win32/64 SDK & OS »
General
Intermediate
Windows XP SP2 Firewall ControllerBy moahHow to control a base firewall included in Windows XP SP2 using COM. |
C#, VC7.1, .NET, WinXP, COM, VS.NET2003, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

Windows XP SP2 basically has a small firewall. It's very easy to control, but sometimes it bothers you. When your application tries to connect to the internet, Windows shows a warning message-box, and this makes users feel your application is an Ad-Ware.
To prevent such things, I suggest you add your program to a Firewall Collection List. (This list contains the allowed programs.)
This wrapper class is very easy, simple, and useful. You don't need to see this 'Security Alert' message box any more:

It's very easy. Just connect to the Windows XP Firewall as a COM, and do what you want.
FW_ERROR_CODE WinXPSP2FireWall::Initialize()
{
HRESULT hr = S_FALSE;
INetFwMgr* fwMgr = NULL;
INetFwPolicy* fwPolicy = NULL;
FW_ERROR_CODE ret = FW_NOERROR;
try
{
if( m_pFireWallProfile )
throw FW_ERR_INITIALIZED;
/* 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;
}
It's very easy to use. Just make an instance, and call Initialize(). Ensure that you call the Initialize() function after calling CoInitialize(). You can call Uninitialize() by yourself, but the destructor calls the function too. Also, you should know that you must let Uninitialize() be called before CoUninitialize() is called.
{
WinXPSP2FireWall fw;
fw.Initialize();
wchar_t szApplication[MAX_PATH];
GetCurrentDirectoryW( MAX_PATH, szApplication );
#ifdef _DEBUG
wcscat(szApplication, L"file://Debug//WindowsFirewall.exe");
#else
wcscat( szApplication, L"file://Release//WindowsFirewall.exe");
#endif
fw.AddApplication( szApplication, L"FireWallTest");
fw.RemoveApplication( szApplication );
fw.AddPort( 4321, NET_FW_IP_PROTOCOL_TCP, L"FireWallPortTest" );
fw.RemovePort( 4321, NET_FW_IP_PROTOCOL_TCP );
fw.Uninitialize();
}
Usage in C# is very similar to that in C++. But it doesn't require APIs for COM interfaces, this makes it easier to implement this in C# than C++. Allocate an instance of the WinXPSP2FireWall, and call Initialize() first. And then use the methods you want. That's all! :)
Moah.WinXPSP2FireWall fw = new Moah.WinXPSP2FireWall();
fw.Initialize();
string strApplication = System.Environment.CurrentDirectory +
"\\WindowsFirewall.exe";
fw.AddApplication(strApplication, "FireWallTest");
fw.RemoveApplication(strApplication);
fw.AddPort(4321, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP,
"FireWallPortTest");
fw.RemovePort(4321, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP);
You need four files to compile this project: netfw.h, netfw.idl, icftypes.h, and icftypes.idl. You can get those files from the Windows XP SP2 PSDK. Or I have included the files in the downloads. You can just use them, instead of downloading and installing the SDK.
If you have errors with NetFwTypeLib, add a reference to hnetcfg.dll. You can do that through "Project -> Add Reference... -> Browse" in Visual Studio, and the DLL file is usually located in "C:\Windows\System32\hnetcfg.dll".
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 10 Jul 2006 Editor: Smitha Vijayan |
Copyright 2005 by moah Everything else Copyright © CodeProject, 1999-2009 Web17 | Advertise on the Code Project |