4297

C# API to test if a network Adapter is firewalled

Question:

Given - .Net 2.0 XP machine with SP2 and multiple network adapters

Is there an API that can be used to check if the network adapter is firewalled?

OneGuyInDC

Answer1:

Give this c# code below a go. It works for Windows 7 (& Vista) and XP. This will get the status of, and enable/disable the Windows firewall for the current profile, eg: Home/Domain/Public access networks.

<strong>Usage:</strong>

getFirewallStatus() --> returns true/false for whether the windows firewall is enable/disabled. setFirewallStatus(newStatus) --> sets the firewall enabled/disabled to the true/false value passed in eg, to enable the firewall: setFirewallStatus(true) getCurrPolicy() --> used by the other two methods isWinXP() --> returns whether windows version is WinXP/2000 or newer, ie: Vista/Win7 used by the other methods to determine which code to use.

<strong>Code:</strong>

using NetFwTypeLib; // (don't forget to add it to your references, its under the COM tab) public bool isWinXP() { OperatingSystem os = Environment.OSVersion; int majorVersion = os.Version.Major; // see http://msdn.microsoft.com/en-us/library/ms724832(v=vs.85).aspx if (majorVersion &lt 6) // if O/S is not Vista or Windows7 { return true; } else { return false; } } private static INetFwPolicy2 getCurrPolicy() { INetFwPolicy2 fwPolicy2; Type tNetFwPolicy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2"); fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(tNetFwPolicy2); return fwPolicy2; } public bool getFirewallStatus() { bool result = false; switch (isWinXP()) { case true: Type NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false); INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType); result = mgr.LocalPolicy.CurrentProfile.FirewallEnabled; break; case false: INetFwPolicy2 fwPolicy2 = getCurrPolicy(); NET_FW_PROFILE_TYPE2_ fwCurrentProfileTypes; //read Current Profile Types (only to increase Performace) //avoids access on CurrentProfileTypes from each Property fwCurrentProfileTypes = (NET_FW_PROFILE_TYPE2_)fwPolicy2.CurrentProfileTypes; result = (fwPolicy2.get_FirewallEnabled(fwCurrentProfileTypes)); break; default: result = false; // assume Win7 by default break; } return result; } public void setFirewallStatus(bool newStatus) { switch (isWinXP()) { case true: Type NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false); INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType); mgr.LocalPolicy.CurrentProfile.FirewallEnabled = newStatus; break; case false: NET_FW_PROFILE_TYPE2_ fwCurrentProfileTypes; INetFwPolicy2 currPolicy = getCurrPolicy(); //read Current Profile Types (only to increase Performace) //avoids access on CurrentProfileTypes from each Property fwCurrentProfileTypes = (NET_FW_PROFILE_TYPE2_)currPolicy.CurrentProfileTypes; currPolicy.set_FirewallEnabled(fwCurrentProfileTypes, newStatus); break; default: NET_FW_PROFILE_TYPE2_ fwCurrentProfileTypes1; INetFwPolicy2 currPolicy1 = getCurrPolicy(); //read Current Profile Types (only to increase Performace) //avoids access on CurrentProfileTypes from each Property fwCurrentProfileTypes1 = (NET_FW_PROFILE_TYPE2_)currPolicy1.CurrentProfileTypes; currPolicy1.set_FirewallEnabled(fwCurrentProfileTypes1, newStatus); break; } }

Answer2:

It is not possible to know <em>in general</em> (e.g. if there is an external firewall) for the following reasons:

<ol><li>If you aren't receiving incoming connections, your external interface may just be down.</li> <li>If you are unable to make outgoing connections, your external interface may just be down.</li> </ol>

But there <em>is</em> an API for finding out if the Windows Firewall is enabled on a given network interface. You will need to use COM interop to get the INetFwProfile (for global firewall status) and INetSharingConfiguration (for a specific network interface) interfaces, and check INetFwProfile.FirewallEnabled and INetSharingConfiguration.InternetFirewallEnabled.

See <a href="http://msdn.microsoft.com/en-us/library/aa364717%28VS.85%29.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/aa364717%28VS.85%29.aspx</a> for links and for how to use these results to determine the effective firewall status. (It's written in terms of VBScript but should be translatable to C#.)

Recommend

  • Creating Many-2-Many reference with EF
  • Why is my internal encoding ISO-8859-1 when I have specified utf-8 in my php.ini file?
  • @selector and return value
  • How to plot and display a square in Octave?
  • Python subprocess on Windows 7 64bit - no output when stdout=PIPE
  • Not a valid Win32 application… Python, PyInstaller, Windows7
  • Why does VB.NET Jet 4.0 app crash if Office not installed?
  • Raw partition access in Windows Vista [closed]
  • Detect which app has been launched in android
  • JSP compilation error on Generics
  • Symfony 3 new project error
  • How to use Xcode Extract refactoring feature?
  • staticfiles and STATIC_URL with virrtualenv - django
  • How to export Selenium Test Suite/Case as C#
  • cast stl::vector containing pointers to stl::vector containing constant pointers
  • How to determine the CCSID used in CPYFRMIMPF command?
  • Add Windows Feature from C#
  • How to discover Font Type?
  • Java Application vs. Java Desktop Application in Netbeans [duplicate]
  • allocating memory to an array of string
  • The plugin 'org.apache.maven.plugins:maven-jboss-as-plugin' does not exist or no valid ver
  • How to rebase a series of branches?
  • swift auto completion not working in Xcode6-Beta
  • Finding past revisions of files in StarTeam w/ .NET SDK / C#
  • Calling of Constructors in a Java
  • Compare two NSDates in iPhone
  • Transpose CSV data with awk (pivot transformation)
  • Cannot Parse HTML Data Using Android / JSOUP
  • Use group_by to filter specific cases while keeping NAs
  • log4net write single file for each call to log.info
  • Benchmarking RAM performance - UWP and C#
  • Acquiring multiple attributes from .xml file in c#
  • C# - Getting references of reference
  • Angular 2 constructor injection vs direct access
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • How can I remove ASP.NET Designer.cs files?
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal