Click here to Skip to main content
15,173,818 members
Articles / Programming Languages / C#
Posted 5 Jul 2005


90 bookmarked

Windows XP SP2 Firewall Controller

Rate me:
Please Sign up or sign in to vote.
4.82/5 (37 votes)
10 Jul 20062 min read
How to control a base firewall included in Windows XP SP2 using COM.

Sample Image - WinXPSP2Firewall.jpg


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:

Sample Image - Windows Firewall Security Alert Message Box

Implementation - C++

It's very easy. Just connect to the Windows XP Firewall as a COM, and do what you want.

FW_ERROR_CODE WinXPSP2FireWall::Initialize()
 INetFwMgr* fwMgr = NULL;
 INetFwPolicy* fwPolicy = NULL;

  if( m_pFireWallProfile )
  /* Create an instance of the firewall settings manager. */
  hr = CoCreateInstance( __uuidof(NetFwMgr), NULL, 
         CLSCTX_INPROC_SERVER, __uuidof( INetFwMgr), (void**)&fwMgr );
  if( FAILED( hr ))
  /* Retrieve the local firewall policy. */
  hr = fwMgr->get_LocalPolicy( &fwPolicy );
  if( FAILED( hr ))
  /* 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 )
 if( fwMgr )
 return ret;

How to Use in C++

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;
 wchar_t szApplication[MAX_PATH];
 GetCurrentDirectoryW( MAX_PATH, szApplication );

#ifdef _DEBUG

  wcscat(szApplication, L"file://Debug//WindowsFirewall.exe");
 wcscat( szApplication, L"file://Release//WindowsFirewall.exe");

 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 );

How to Use in C#

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();

string strApplication = System.Environment.CurrentDirectory + 
fw.AddApplication(strApplication, "FireWallTest");


Acknowledgment for C++

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.

Acknowledgment for C#

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".


  • 6, Jul. 2005
    • First release.
  • 10, Jul. 2006
    • Added a C# version of the Windows XP SP2 Firewall Controller.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
Korea (Republic of) Korea (Republic of)
Youngjin Kim lives in South Korea. I'm interested in every part of Computer Science, cause it has not been long time since graduate a University.
But now I'm working and researching on Pattern Recognition. Using that trying to recognize a Handwriting Prints. Korean and Chinese are my interesting Research Part.

Comments and Discussions

QuestionVisual studio 2017 Pin
Apprieu (Apprieu)4-Sep-19 22:33
MemberApprieu (Apprieu)4-Sep-19 22:33 
QuestionLicense Pin
Member 1391457316-Jul-18 23:18
MemberMember 1391457316-Jul-18 23:18 
GeneralMy vote of 5 Pin
Member 114299875-Feb-15 5:23
MemberMember 114299875-Feb-15 5:23 
QuestionMultiple profiles Pin
tianjin2241-Nov-13 17:15
Membertianjin2241-Nov-13 17:15 
QuestionLicense Pin
Member 896106529-May-12 0:27
MemberMember 896106529-May-12 0:27 
Questionhow to run this source code Pin
Member 39583127-Sep-08 0:24
MemberMember 39583127-Sep-08 0:24 
QuestionHow to add Windws Service Exception??? Pin
erickbp212-Sep-08 10:20
Membererickbp212-Sep-08 10:20 
Generalbug under Vista Pin
noelhx27-Mar-07 19:55
Membernoelhx27-Mar-07 19:55 
QuestionXP firewall gets automatically DISABLED when using this?? Pin
Orion Richardson228-Dec-06 5:28
MemberOrion Richardson228-Dec-06 5:28 
AnswerRe: XP firewall gets automatically DISABLED when using this?? Pin
moah28-Dec-06 7:02
Membermoah28-Dec-06 7:02 
AnswerRe: XP firewall gets automatically DISABLED when using this?? Pin
Orion Richardson27-Feb-07 9:49
MemberOrion Richardson27-Feb-07 9:49 
GeneralRe: XP firewall gets automatically DISABLED when using this?? Pin
chinnu_j14-Feb-07 0:19
Memberchinnu_j14-Feb-07 0:19 
GeneralRe: XP firewall gets automatically DISABLED when using this?? Pin
el delo15-May-07 13:52
Memberel delo15-May-07 13:52 
QuestionDoes this work on Vista? Pin
thomastom19-Sep-06 12:00
Memberthomastom19-Sep-06 12:00 
AnswerRe: Does this work on Vista? Pin
thomastom19-Sep-06 15:02
Memberthomastom19-Sep-06 15:02 
GeneralRe: Does this work on Vista? Pin
moah20-Sep-06 6:45
Membermoah20-Sep-06 6:45 
AnswerRe: Does this work on Vista? Pin
Ed Dixon28-Dec-06 13:50
MemberEd Dixon28-Dec-06 13:50 
GeneralDon't listen to the complainers Pin
TomM11-Aug-06 12:40
MemberTomM11-Aug-06 12:40 
GeneralRe: Don't listen to the complainers Pin
moah12-Aug-06 3:37
Membermoah12-Aug-06 3:37 
Questionwhat if there is no SP? Pin
Mario M.21-Jul-06 14:35
MemberMario M.21-Jul-06 14:35 
AnswerRe: what if there is no SP? Pin
moah23-Jul-06 17:05
Membermoah23-Jul-06 17:05 
GeneralFirewall on Windows 2000 and XP SP1 Pin
Fornazin19-Jul-06 5:46
MemberFornazin19-Jul-06 5:46 
GeneralRe: Firewall on Windows 2000 and XP SP1 Pin
moah19-Jul-06 21:22
Membermoah19-Jul-06 21:22 
GeneralRe: Firewall on Windows 2000 and XP SP1 Pin
padma81015-Nov-06 0:30
Memberpadma81015-Nov-06 0:30 
GeneralRe: Firewall on Windows 2000 and XP SP1 Pin
moah15-Nov-06 1:49
Membermoah15-Nov-06 1:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.