How can I detect if a firewall product is enabled? How can I detect if a firewall product is enabled? windows windows

How can I detect if a firewall product is enabled?


NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false); INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType);bool Firewallenabled = mgr.LocalPolicy.CurrentProfile.FirewallEnabled;

For details see a link.

http://technet.microsoft.com/en-us/library/cc737845%28WS.10%29.aspx

http://blogs.msdn.com/b/securitytools/archive/2009/08/21/automating-windows-firewall-settings-with-c.aspx


Have a look at this question here about antivirus How to detect antivirus installed on windows 2003 server and 2008 server 2003 server R2and 2008 server R2 using WMI or other then WMI in C++ the same API call can be used to detect firewall settings using the WSC_SECURITY_PROVIDER_FIREWALL enum. The answer there is actually wrong for that question, but it will give you the answer for non-server computers. That code is in C++, but it's just the windows API call you need, you can call that from C# too.


You'll first need to add the following component to your project

  • INetFwMgr

Then, get the object type from the Home Networking Configuration Manager CLSID which is {304CE942-6E39-40D8-943A-B913C40C9CD4}(Links to C:\WINDOWS\system32\hnetcfg.dll and can be found at HKEY_CLASSES_ROOT\CLSID\{304CE942-6E39-40D8-943A-B913C40C9CD4}) and use the type gathered to create an instance using the type's default constructor as a new INetFwMgr which will be used to detect whether the firewall is enabled or not using INetFwMgr.LocalPolicy.CurrentProfile.FirewallEnabled which returns a bool

private const string CLSID_FIREWALL_MANAGER = "{304CE942-6E39-40D8-943A-B913C40C9CD4}"; //This is the CLSID of Home Networking Configuration Manager. We'll use this to detect whether the Firewall is enabled or notprivate static NetFwTypeLib.INetFwMgr GetHNCMType(){    Type objectType = Type.GetTypeFromCLSID(new Guid(CLSID_FIREWALL_MANAGER)); //Creates a new GUID from CLSID_FIREWALL_MANAGER getting its type as objectType    return Activator.CreateInstance(objectType) as NetFwTypeLib.INetFwMgr; //Creates an instance from the object type we gathered as an INetFwMgr object}static void Main(string[] args){    INetFwMgr manager = GetHNCMType(); //Initializes a new INetFwMgr of name manager from GetHNCMType    if (manager.LocalPolicy.CurrentProfile.FirewallEnabled == false) //Continue if the firewall is not enabled    {        //The firewall is not enabled        Console.WriteLine("OFF"); //Writes OFF to the Console in a new line    }    else //Otherwise:    {        //The fire wall is enabled        Console.WriteLine("ON"); //Writes ON to the Console in a new line    }}

Thanks,
I hope you find this helpful :)


To add a component to your project,

  • Right-click References from the Solution Explorer under yourproject name and select Add Reference...
  • Under the tab COM, select the component you'd like to add and click on OK