|
-
April 20th, 2010, 04:10 AM
#1
Register an EXE as a Windows Firewall exception using C# code
I have a service (say BS.exe) written using C# of which the installer is created using Wix and C# (which means it has a Custom Action program associated with the Wix installer program as well). Now, after the program in installed there is a need to go to Windows Firewall settings and register the BS.exe there. That is, go to Windows Firewall -> Change Settings -> Exceptions tab and add BS.exe there.
However, now we need to do this process automatically during the installation time. I guess the Custom Action program associated with the Wix is the best place. So is there a way to register this EXE as a Firewall exception using C#?
Thanks in advance!
-
April 20th, 2010, 11:03 AM
#2
Re: Register an EXE as a Windows Firewall exception using C# code
-
April 20th, 2010, 09:24 PM
#3
Re: Register an EXE as a Windows Firewall exception using C# code
Thanks a lot.
It works fine. However, I am encountering a problem that needs to be solved. That is, the "NetFwTypeLib" object resides in hnetcfg.dll in XP while it is inside the FirewallAPI.dll in Vista.
Since I need my application be authorized irrespective of the OS, how am I to tackle that problem? I cannot use both the .dlls can I? And it is a must that one code should be able to do this.
-
April 20th, 2010, 10:24 PM
#4
Re: Register an EXE as a Windows Firewall exception using C# code
 Originally Posted by sachintha81
Thanks a lot.
It works fine. However, I am encountering a problem that needs to be solved. That is, the "NetFwTypeLib" object resides in hnetcfg.dll in XP while it is inside the FirewallAPI.dll in Vista.
Since I need my application be authorized irrespective of the OS, how am I to tackle that problem? I cannot use both the .dlls can I? And it is a must that one code should be able to do this.
Idk if there is a better way, but I've used this in the past:
Code:
if(System.Environment.OSVersion.Version.Major > 5)
{
//is Windows Vista or later
}
else
{
//is Windows XP or earlier
}
-
April 20th, 2010, 11:21 PM
#5
Re: Register an EXE as a Windows Firewall exception using C# code
Thanks Chris.
Yes you can check the OS version that way, but the problem is not that. To this program to work the NetFwTypeLib object is needed. However, in Vista environment it resides in FirewallAPI.dll so I need to add it to my Visual Studio Project using references. But in XP environment the same object resides in hnetcfg.dll. So, if I develp the program in Vista adding the FirewallAPI.dll, then when I use the program in an XP environment it will fail to execute because it doesn't have the hnetcfg.dll included in it. Or so I think.
-
April 20th, 2010, 11:37 PM
#6
Re: Register an EXE as a Windows Firewall exception using C# code
 Originally Posted by sachintha81
Thanks Chris.
Yes you can check the OS version that way, but the problem is not that. To this program to work the NetFwTypeLib object is needed. However, in Vista environment it resides in FirewallAPI.dll so I need to add it to my Visual Studio Project using references. But in XP environment the same object resides in hnetcfg.dll. So, if I develp the program in Vista adding the FirewallAPI.dll, then when I use the program in an XP environment it will fail to execute because it doesn't have the hnetcfg.dll included in it. Or so I think.
I don't see what the problem is. Use [DllImport("FirewallAPI.dll")] ... and [DllImport("hnetcfg.dll")] ...
and use System.Environment.OSVersion.Version.Major to figure out which function to call.
-
April 28th, 2010, 09:04 PM
#7
Re: Register an EXE as a Windows Firewall exception using C# code
Hi Chris,
Can you type please the complete sample code for OS conditional using.
Thank you
-
April 28th, 2010, 09:08 PM
#8
Re: Register an EXE as a Windows Firewall exception using C# code
 Originally Posted by Chris_F
I don't see what the problem is. Use [DllImport("FirewallAPI.dll")] ... and [DllImport("hnetcfg.dll")] ...
and use System.Environment.OSVersion.Version.Major to figure out which function to call.
Chris, actually I found out that "FirewallAPI.dll" can in fact be used in XP and older OS as well. It is "hnetcfg.dll" that causes the problem in Vista.
But yeah, your method is also great specially in other cases where you hve to use two dll's. Thanks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|