Vedivi Free Trial.
No setup fee. No monthly payments. No credit cards. No hassle.
| Configure your firewall in C++ |
|
I did work recently on configuring Firewalls programmatically within an application, so I thought I should share my experience here. ChallengeMost computers run a Personal Firewall these days (Windows Firewall, PC-Cillin, Norton, AVG...). So in many cases, software developers will have to deal with getting their application configured in their customer's firewall. You could either choose to tell your users how they should configure their firewall (in a help document) or do it automatically (during the install process). Some application only require a static configuration, i.e a configuration that will never change. For instance "myservice.exe" should be added to the list of "exceptions" or trusted applications. This can be performed during the installation process and is normally supported by most installers (InstallShield) But in other cases, the configuration depends on the user configuration and needs to be done after the install. Then you will need to deal with it within your application. So let's see how to do it in C++ ScopeTo keep it simple, we will consider the following usages: Check if Windows Firewall is enabledSee if Windows Firewall (Windows XP & Vista) is installed and enabled. Port Enable/DisableCheck if a specific port is enabled and eventually enable it if it exists. Configure the Firewall in C++Windows Firewall API is available through COM, so we will need the usual COM initialize/uninitialize. CFirewallUtil.hThe class described is called CFirewallUtil and has the following header (.h): class CFirewallUtil InitializeInitialize COM, instanciate the FirewallManager object (NetFwMgr) and loads the firewall profile (INetFwProfile) to query/modify.HRESULT CFirewallUtil::Initialize(INetFwProfile** fwProfile) UnitializeFree the profile and any initialized object and unitialize COMvoid CFirewallUtil::Uninitialize(INetFwProfile* fwProfile) IsFirewallEnabledChecks if Windows firewall is enabled for the given profile.HRESULT CFirewallUtil::IsFirewallEnabled(INetFwProfile* fwProfile, BOOL * pEnabled) IsPortEnabledChecks if the given port has an existing rule allowing it for the given protocol (TCP or UDP). It basically loops through all the open ports and checks if a matching configuration exists and is enabled.HRESULT CFirewallUtil::IsPortEnabled(INetFwProfile* fwProfile, BOOL *pEnabled, int iPort, bool bTCP) SetPortEnabledChecks if the given port has an existing rule allowing it for the given protocol (TCP or UDP) and if so, enables or disables it (it only works on existing exceptions). It basically loops through all the open ports and if a matching configuration exists, enables or disables it as required.HRESULT CFirewallUtil::SetPortEnabled(INetFwProfile* fwProfile, int iPort, bool bTCP, bool bEnable) Putting it togetherHere it how you would use the class to check for example if the Windows Firewall is enabled:HRESULT hr; ConclusionThis is a small subset of what you can do with the firewall API, basically any configuration you can do through the UI is available in the API, and the COM interfaces makes it quite straightforward to use. Download the complete example class MSDNWindows Firewall: Internet Connection Sharing and Internet Connection Firewall (MSDN) |
| Last Updated on Tuesday, 01 June 2010 14:36 |
Vedivi Business is the latest generation of secure remote access solution, it combines a VPN with Remote Desktop and Remote Assistance integration.
With Vedivi you can:
Get started with Vedivi 30-Day free trial so you can see for yourself why so many businesses trust Vedivi for VPN & remote access.