Sunday, October 07, 2007

If you've read Raymond Chen's blog long enough, then you know trying to change system stuff directly in Windows registry is discouraged, if not frowned upon.  So when I kept hacking away at the registry trying to get some Windows Firewall exceptions for XP and Vista created, I decided to take a step back and see what Windows's API's are out there to do this.

Doing some Google searches doesn't reveal much (which is why I decided to blog this), except these two hidden gems Syslog daemon for Windows Eventlog, and Adding a port to the XP Firewall.  Both of these gave me pointers in the right direction to create this gem:

  1 private static void ExceptionToFirewall(bool add, string imageFileName, string name)
  2 {
  3 	Type netFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr");
  4 	INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(netFwMgrType);
  5 
  6 	INetFwProfile curProfile = mgr.LocalPolicy.CurrentProfile;
  7 	if (add)
  8 	{
  9 		Type NetFwAuthorizedApplicationType = Type.GetTypeFromProgID("HNetCfg.FwAuthorizedApplication", false);
 10 		INetFwAuthorizedApplication app = (INetFwAuthorizedApplication)Activator.CreateInstance(NetFwAuthorizedApplicationType);
 11 
 12 		app.Name = name;
 13 		app.ProcessImageFileName = imageFileName;
 14 		app.Enabled = true;
 15 		app.RemoteAddresses = "*";
 16 		app.Scope = NET_FW_SCOPE_.NET_FW_SCOPE_ALL;
 17 
 18 		curProfile.AuthorizedApplications.Add(app);
 19 	}
 20 	else
 21 	{
 22 		curProfile.AuthorizedApplications.Remove(imageFileName);
 23 	}
 24 }
 

To use this, you'll need to add a reference to COM component HNetCfg.FwMgr (Guid "{304CE942-6E39-40D8-943A-B913C40C9CD4}", file path C:\windows\system32\hnetcfg.dll).

One note, don't use the IpVersion property of INetFwAuthorizedApplication, under Windows Vista it throws a NotImplimentedException.

kick it on DotNetKicks.com 

del.icio.us Tags: , ,

posted on Sunday, October 07, 2007 5:50:55 PM (Alaskan Standard Time, UTC-09:00)  #    Comments [0]
Related posts:
Windows Server 2008, a better faster Windows Vista
LINQ Goodness
What I like about Windows Vista
First impressions of Windows Vista
Windows cacles at me
De-crapify your code base with AOP using PostSharp

Referred by:
http://www.dotnetkicks.com/csharp/Windows_Firewall_Exception... [Referral]
windows (search.live.com) [Referral]
windows (search.live.com) [Referral]
windows firewall api c# (search.live.com) [Referral]
Windows Firewall C# (search.live.com) [Referral]
Windows Firewall and Windows Firewall C# (search.live.com) [Referral]
windows firewall exception registry (search.msn.com) [Referral]
windows firewall exceptions registry (search.live.com) [Referral]
http://search.msdn.microsoft.com/Default.aspx?query=LocalPol... [Referral]
c# firewall exception vista (search.live.com) [Referral]
"firewall exceptions" c# (www.google.co.uk) [Referral]
c# windows firewal (search.live.com) [Referral]
windows firewall c# (search.live.com) [Referral]
windows firewall exception registry (search.live.com) [Referral]
Vista Firewall API C# (search.live.com) [Referral]
registry windows firewall exceptions (www.google.com.au) [Referral]
vista firewall exceptions registry (search.live.com) [Referral]
registry windows xp firewall exceptions (search.live.com) [Referral]
c# firewall exception (www.google.co.uk) [Referral]
C# Firewall Exceptions (www.google.com) [Referral]
INetFwAuthorizedApplication (search.live.com) [Referral]
HNetCfg.FwMgr csharp (www.google.de) [Referral]
c# firewall (search.msn.com) [Referral]
Windows Firewall exceptions C# (www.google.com.br) [Referral]
msn firewall exceptions (search.msn.com) [Referral]
c# windows firewall exception (www.google.ca) [Referral]
adding firewall exception registry windows (search.live.com) [Referral]
HNetCfg.FwMgr (www.google.com) [Referral]
c# HNetCfg.FwMgr (www.google.com) [Referral]
windows (search.live.com) [Referral]
c# add windows firewall exception (search.live.com) [Referral]
C# firewall exceptions (www.google.com) [Referral]
c# add to "firewall exception" (www.google.com) [Referral]
list firewall exceptions C# (www.google.com) [Referral]
HNetCfg.FwMgr vista (www.google.it) [Referral]
HNetCfg.FwMgr (www.google.ca) [Referral]
C# INetFWAuthorizedApplication (www.google.com) [Referral]
windows firewall exception registry (search.msn.com) [Referral]
windows firewall c# (www.google.co.in) [Referral]
windows firewall exceptions (search.msn.com) [Referral]
"add windows firewall exception" (www.google.co.ma) [Referral]
C# firewall (www.google.cz) [Referral]
c# add exception to firewall (www.google.com) [Referral]
firewall exceptions (www.google.hr) [Referral]
windows firewall doesn't except exception (www.google.co.uk) [Referral]
windows firewall c# (www.google.ca) [Referral]
API disable windows vista firewall (www.google.com) [Referral]
Windows Firewall Exceptions registry (www.google.com) [Referral]
GUID C# firewall (www.google.com) [Referral]
"windows firewall c#" (www.google.co.th) [Referral]
hacking firewall exceptions (www.google.com.au) [Referral]
adding xp firewall exception registry (www.google.com) [Referral]
firewall exception for msn (www.google.co.uk) [Referral]
c# creating a firewall (www.google.com) [Referral]
excepcion+firewall+windows+c# (www.google.com.ar) [Referral]
vista firewall exception registry (www.google.com) [Referral]
c# firewall exception (www.google.com) [Referral]
c# vista firewall (www.google.co.uk) [Referral]
HNetCfg.FwMgr vista (www.google.be) [Referral]
C# firewall (www.google.pt) [Referral]
c# add firewall exception (www.google.com) [Referral]
window firewall registry exception (www.google.com.sg) [Referral]
INetFwAuthorizedApplication enabled vista (www.google.ie) [Referral]
create firewall port exception c# (www.google.com) [Referral]
HNetCfg.FwMgr (www.google.dk) [Referral]
Windows Vista firewall add exception registry (www.google.pl) [Referral]
vista Firewall Exception List via registry (www.google.ie) [Referral]
api programming windows server 2008 firewall scope (www.google.com) [Referral]
xp add firewall exception c# (www.google.com.my) [Referral]
registry enable firewall exception (www.google.co.uk) [Referral]
c# firewall api vista (www.google.com) [Referral]
"windows firewall" registry exceptions vista (www.google.com) [Referral]
windows firewall remoteaddresses (search.yahoo.com) [Referral]
add port firewall exception c# (www.google.com.tr) [Referral]
windows firewall registry exceptions (www.google.com) [Referral]
windows firewall exceptions registry (search.yahoo.com) [Referral]
c# Enable disable Windows Firewall (www.google.co.in) [Referral]
registry windows xp firewall exception (www.google.com) [Referral]
firewall exception registry (www.google.is) [Referral]
csharp windows firewall (www.google.cz) [Referral]
vista firewall exceptions (www.google.com) [Referral]
add exception to firewall via registry (www.google.com) [Referral]
c sharp Windows Firewall reference (www.google.com.tw) [Referral]
Using Windows Firewall API in MSDN with c# (www.google.com.pk) [Referral]
add firewall exception registry windows xp (www.google.es) [Referral]
add firewall exception in vista by registry (www.google.ru) [Referral]
windows xp firewall exception registry (www.google.nl) [Referral]
firewall exception hacking (www.google.com) [Referral]
add service to firewall exceptions c# (www.google.ca) [Referral]
"windows server 2008" firewall registry (www.google.com) [Referral]
Add Firewall Exceptions c# (search.live.com) [Referral]
Add Windows Firewall Exceptions c# (search.live.com) [Referral]
hnetcfg apis (www.google.co.in) [Referral]
C# AND query windows firewall port (www.google.com) [Referral]
C# firewall (www.google.co.kr) [Referral]
windows firewall registry exceptions (www.google.com) [Referral]
HNetCfg FwMgr XP (www.google.de) [Referral]
"Windows Server 2008" Firewall API (www.google.ca) [Referral]
adding exception to windows firewall C# (www.google.com) [Referral]
windows server 2008 firewall add exception (www.google.cz) [Referral]
INetFwAuthorizedApplication (www.google.ca) [Referral]
windows firewall scope registry (www.google.com) [Referral]
HNetCfg firewall (www.google.com) [Referral]
adding a port to firewall c# (www.google.ro) [Referral]
c# windows firewall hack (www.google.com.br) [Referral]
create firewall exceptions in xp (www.google.com) [Referral]
vista windows firewall exception registry (www.google.com) [Referral]
Adding ports Exceptions windows firewall C# (www.google.es) [Referral]
windows 2008 firewall Exception (www.google.com) [Referral]
registry windows firewall exception (www.google.com.au) [Referral]
RemoteAddresses c# firewall (www.google.com) [Referral]
win xp firewall add exception from registry (www.google.com.ar) [Referral]
vista HNetCfg.FwMgr (www.google.de) [Referral]
creating exceptions in the windows firewall on xp (www.google.co.uk) [Referral]
vista firewall exception list registry (www.google.ca) [Referral]
register exception with windows firewall (www.google.ca) [Referral]
"windows vista" "firewall" AuthorizedApplications (www.google.com.vn) [Referral]
add update.microsoft.com to windows firewall exception list (www.google.com) [Referral]
xp regedit firewall exceptions grayout (www.google.com) [Referral]
"windows server 2008" firewall registry (www.google.com) [Referral]
Windows Firewall programming (www.google.ru) [Referral]
vista firewall exceptions registry (www.google.com) [Referral]
server 2008 windows firewall scope registry (www.google.com) [Referral]
hnetcfg.fwmgr (www.google.com) [Referral]
c# firewallapi vista xp (www.google.de) [Referral]
add windows xp firewall exception via registry (www.google.com.au) [Referral]
c# using firewallapi xp (www.google.de) [Referral]
c# firewallapi (www.google.de) [Referral]
"windows firewall" allow program exceptions registry c# (www.google.com) [Referral]
C# windows firewall api (www.google.com) [Referral]
vista firewall exception registry entries adding (www.google.ca) [Referral]
Windows Server 2008 Hnetcfg.dll (www.google.ca) [Referral]
inetfwauthorizedapplication (www.google.co.jp) [Referral]
Windows Vista firewall registry exceptions (www.google.com) [Referral]
vista firewall ports registry exceptions (www.google.co.uk) [Referral]
add port to firewall exceptions C# (www.google.co.za) [Referral]
HNetCfg.FwMgr (www.google.es) [Referral]
firewall exception registry windows (www.google.com) [Referral]
C# Add Windows Firewall Exception (www.google.com) [Referral]
Windows Firewall Exceptions Registry (www.google.com) [Referral]
add exceptions to firewall c sharp (www.google.co.in) [Referral]
hack + add program to windows firewall from registry (www.google.nl) [Referral]
xp firewall hack (www.google.com.au) [Referral]
creating a firewall exception (www.google.co.uk) [Referral]
HNetCfg.FwMgr (www.google.com) [Referral]
enable firewall exceptions registry (www.google.be) [Referral]
registry firewall exception (search.live.com) [Referral]
HNetCfg.FwMgr (www.google.co.jp) [Referral]
windows server 2008 firewall c# INetFwMgr (www.google.com) [Referral]
firewall exception C# (www.google.com) [Referral]
add exception to Firewall in registry (www.google.com) [Referral]
windows firewall exception c# (www.google.com) [Referral]
regedit firewall add exception port (www.google.ca) [Referral]
app name under exceptions list windows firewall (www.google.ca) [Referral]
HNetCfg.FwMgr scope (www.google.co.in) [Referral]
registry firewall add exception (www.google.ca) [Referral]
c# firewall api (www.google.de) [Referral]
http://www.diskusjon.no/index.php?showtopic=1045320 [Referral]
HNetCfg.FwMgr (www.google.be) [Referral]
vista firewall add port registry (www.google.at) [Referral]
vista firewall create exception registry (www.google.at) [Referral]
firewall exception (registry) (www.google.com.ph) [Referral]
c# firewall (www.google.cz) [Referral]
windows firewall registry exception (www.google.com.au) [Referral]
vista firewall + exception registry (www.google.at) [Referral]
remove firewall exceptions winxp registry (www.google.co.uk) [Referral]
xp firewall registry exceptions (www.google.com) [Referral]
creating a firewall windows C# (www.google.co.uk) [Referral]
creating good Windows Firewall Exceptions (www.google.com) [Referral]
"windows 2008" firewall default exceptions (www.google.ca) [Referral]
windows firewall api (www.google.com.ua) [Referral]
+"Server 2008"+firewall+registry+disable (www.google.com) [Referral]
registry add port exception (www.google.com) [Referral]
HNetCfg.FwMgr service enabled (www.google.fr) [Referral]
HNetCfg.FwMgr 2008 (www.google.com) [Referral]
Add exception firewall windows iNetFwMgr (www.google.com.br) [Referral]
changes registry exceptions vista firewall (www.google.com) [Referral]
vista firewall exceptions registry (www.google.com.tr) [Referral]
windows firewall set exceptions via registry (www.google.com) [Referral]
http://blogs.msdn.com/oldnewthing/ [Referral]
Add exceptions windows firewall faster (www.google.ca) [Referral]
should I allow microsoft onenote in firewall exceptions? (search.yahoo.com) [Referral]
windows firewall exceptions add port C# (www.google.co.in) [Referral]
add update to firewall exceptions (www.google.pl) [Referral]
windows firewall registry (search.live.com) [Referral]
xp firewall exceptions (www.google.com) [Referral]
windiws vista firewall exception regedit (www.google.ae) [Referral]
add ports to windows vista firewall registry (www.google.com) [Referral]
c# firewall exception (www.google.co.uk) [Referral]
create firewall exception vista registry (www.google.fr) [Referral]
adding firewall exception c# registry (www.google.com.ph) [Referral]
C# how to add program to windows firewall exceptions (www.google.com) [Referral]
firewall exceptions registry (www.google.com) [Referral]
registry add program firewall vista (www.google.com) [Referral]
Vista Windows Firewall registry (search.live.com) [Referral]
xp firewall port registry (www.google.com) [Referral]
regedit to add exception to firewall (www.google.com) [Referral]
windows firewall add exception registry (www.google.com) [Referral]
Firewall.LocalPolicy.CurrentProfile in C# (www.google.com.sg) [Referral]
firewall c# (www.google.com) [Referral]
server 2008 enable firewall via regedit (www.google.com) [Referral]
windows firewall registry add program (www.google.nl) [Referral]
hnetcfg firewall (www.google.com.tr) [Referral]
vista firewall C# (www.google.com) [Referral]
disable windows firewall + hack (www.google.com) [Referral]
firewall c# (www.google.es) [Referral]
firewall exceptions in Windows registry (www.google.com) [Referral]
"AuthorizedApplications" firewall (www.google.com) [Referral]
hnetcfg on vista (www.google.com.tr) [Referral]
registry enable firewall windows vista AuthorizedApplications (www.google.com) [Referral]
firewall exception MSN (www.google.co.th) [Referral]
windows firewall exceptions registry (www.google.com) [Referral]
msdn+windows firewall programming (www.google.de) [Referral]
add port to vista registry (www.google.co.uk) [Referral]
HNetCfg.FwMgr (www.google.com) [Referral]
windowsfirewall remoteaddresses (www.google.com) [Referral]
c# firewall port (www.google.co.kr) [Referral]
API adding firewall exceptions Windows (www.google.com.au) [Referral]
C# create firewall (www.google.ro) [Referral]
firewall exception registry (www.google.pt) [Referral]
firewall exeptions registry (www.google.nl) [Referral]
add exception to windows firewall via registry (www.google.com) [Referral]
c# add exception to firewall vista (www.google.be) [Referral]
HNetCfg.FwMgr (www.google.com) [Referral]
HNetCfg.FwMgr (www.google.com) [Referral]
c# firewall (www.google.de) [Referral]
change windows firewall via c# (www.google.co.il) [Referral]
add port to Exceptions firewall C# (www.google.com.vn) [Referral]
http://www.forumweb.com.br/foruns/index.php?showtopic=74907&... [Referral]
FwMgr C# (www.google.com) [Referral]
c# add to firewall (www.google.ru) [Referral]
firewall exception registry string (www.google.pt) [Referral]
delete exceptions vista firewall (www.google.bg) [Referral]
create exceptions in firewall (www.google.com) [Referral]
c# set windows firewall vista (www.google.com) [Referral]
C#.net windows firewall vista (www.google.com) [Referral]
xp firewall add program regedit (www.google.com.vn) [Referral]
server 2008 firewall exception (www.google.com) [Referral]
add firewall exception registry (www.google.com) [Referral]
windows server 2008 firewall exception list (www.google.com.do) [Referral]
xp modify windows firewall exceptions registry (www.google.com) [Referral]
add firewall exception on windows server c++ (www.google.hr) [Referral]
firewall hack regedit (www.google.es) [Referral]
windows firewall authorizedapplications (www.google.fr) [Referral]
query exception firewall (www.google.com.gt) [Referral]
create firewall in c# (www.google.com) [Referral]
how to add xp updates to firewall exceptions (www.google.ca) [Referral]
create firewall program in c# (www.google.co.in) [Referral]
create a firewall in windows using c# (www.google.co.in) [Referral]
vista HNetCfg.FwMgr (www.google.com) [Referral]
windows update firewall exceptions (www.google.com) [Referral]
add program exceptions on XP Firewall by regedit (www.google.sk) [Referral]
registry firewall exception (www.google.com.au) [Referral]
HNetCfg.FwMgr (search.live.com) [Referral]
windows firewall exception program list registry (www.google.co.kr) [Referral]
"windows 2008" disable firewall registry (www.google.com) [Referral]
add firewall exception via registry (www.google.com.au) [Referral]
windows firewall exceptions regedit (www.google.com) [Referral]
windows firewall C# change scope (www.google.nl) [Referral]
c# +firewall (www.google.com.tw) [Referral]
c# +firewall (www.google.com.tw) [Referral]
excepciones firewall disabled, regedit (www.google.com.ar) [Referral]
exception windows firewall update.microsoft (www.google.nl) [Referral]
C# Windows Firewall (search.live.com) [Referral]
add firewall excepcion c# windows (www.google.pt) [Referral]
add firewall exceptions code (www.google.com) [Referral]
In C# How to add exception port in windows firewall (www.google.co.in) [Referral]
regedit add windows xp firewall exception (www.google.lv) [Referral]
add port to windows firewall registry (www.google.com) [Referral]
windows firewall registry exceptions (www.google.com) [Referral]
http://www.forumweb.com.br/foruns/index.php?showtopic=74907 [Referral]
registry Firewall Exception (www.google.com) [Referral]
All comments require the approval of the site owner before being displayed.
Name
E-mail
(will show your gravatar icon)
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Live Comment Preview