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]
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