Windows Firewalls and .NET

Introduction

Isn’t it nice that we as developers can play with system settings? Sometimes, I feel so fortunate to be able to know all the intricacies, but it has its disadvantages as well. The problem is that many people do not understand me (well, normally they don’t) and my joy these small things bring me.

I find it difficult to talk about my work and my projects because very few people understand what I do, hence me writing so many articles

Before I bore you even more, let me start.

Firewalls

Most people would know what a firewall is, hopefully. A firewall isn’t a wall made of fire. It isn’t a glorified Matrix-type-rollercoaster such as movies about hackers portray. It is a relatively simple thing.

A firewall is simply a network security system that monitors and controls incoming and outgoing traffic based on predetermined security rules.

Our Project

The aim of today’s project is to see whether or not your Firewall is enabled. Open Visual Studio and create either a Visual Basic.NET or C# Windows Forms project and design it shown in Figure 1.

Design
Figure 1: Design

Before continuing, you need to add a Reference to the Firewall.DLL file located in your Windows System32 directory. Click Project, Add reference and browse to the Firewall.DLL file, as shown in Figure 2.

Add Reference
Figure 2: Add Reference

Add the following namespaces for your Firewall calls to work.

C#

using System;
using System.Windows.Forms;
using NetFwTypeLib;

VB.NET

Imports System
Imports System.Windows.Forms
Imports NetFwTypeLib

Add the following fields.

C#

      private bool blnEnabled = false;
      private const string clsidFireWall =
         "{304CE942-6E39-40D8-943A-B913C40C9CD4}";

VB.NET

   Private blnEnabled As Boolean = False
   Private Const clsidFireWall As String = _
      "{304CE942-6E39-40D8-943A-B913C40C9CD4}"

blnEnabled will be used to indicate if the Firewall is active or not. clsidFirewall is the physical firewall object. This is its name and what Windows know it as.

Add the next function.

C#

      private bool CheckFirewal1l()
      {

         try
         {

            Type tpNetFirewall = Type.GetTypeFromProgID
               ("HNetCfg.FwMgr", false);

            INetFwMgr mgrInstance = (INetFwMgr)Activator
               .CreateInstance(tpNetFirewall);

            bool blnEnabled = mgrInstance.LocalPolicy
               .CurrentProfile.FirewallEnabled;

            mgrInstance = null;

            tpNetFirewall = null;

            return this.blnEnabled;

         }
         catch (Exception e)
         {

            return false;

      }

VB.NET

   Private Function CheckFirewal1l() As Boolean

      Try

         Dim tpNetFirewall As Type = Type.GetTypeFromProgID _
            ("HNetCfg.FwMgr", False)
         Dim mgrInstance As INetFwMgr = CType(Activator _
            .CreateInstance(tpNetFirewall), INetFwMgr)
         Dim blnEnabled As Boolean = mgrInstance.LocalPolicy _
            .CurrentProfile.FirewallEnabled

         mgrInstance = Nothing
         tpNetFirewall = Nothing

         Return Me.blnEnabled

      Catch e As Exception

         Return False

      End Try

   End Function

You use late binding to instantiate the Firewall object and to determine its enabled state. Add the second CheckFirewall function.

C#

      private void CheckFirewall2()
      {

         try
         {

            INetFwMgr mgrInstance = GetType();

            if (mgrInstance.LocalPolicy.CurrentProfile
               .FirewallEnabled == false)
            {

               notifyIcon1.BalloonTipText = "Firewall Disabled" ;

            }
            else
            {

               notifyIcon1.BalloonTipText = "Firewall Enabled";

            }

         }
         catch (Exception e)
         {


         }

      }

VB.NET

   Private Sub CheckFirewall2()

      Try

         Dim mgrInstance As INetFwMgr = [GetType]()

         If mgrInstance.LocalPolicy.CurrentProfile.FirewallEnabled _
               = False Then

            notifyIcon1.BalloonTipText = "Firewall Disabled"

         Else

            notifyIcon1.BalloonTipText = "Firewall Enabled"

         End If

      Catch e As Exception

      End Try

   End Sub

Add the GetType function which instantiates the firewall object in the previous function.

C#

      private static NetFwTypeLib.INetFwMgr GetType()
      {

         Type tpCLSID = Type.GetTypeFromCLSID(new
            Guid(clsidFireWall));

         return Activator.CreateInstance(tpCLSID) as
            NetFwTypeLib.INetFwMgr;

      }

VB.NET

   Private Shared Function [GetType]() As NetFwTypeLib.INetFwMgr

      Dim tpCLSID As Type = Type.GetTypeFromCLSID(New _
         Guid(clsidFireWall))
      Return TryCast(Activator.CreateInstance(tpCLSID), _
         NetFwTypeLib.INetFwMgr)

   End Function

The GetType function uses the Firewall’s CLSID to instantiate it and load into memory so that you can test whether or not the firewall is enabled.

Add the code for the buttons to call the two CheckFirewall functions.

C#

      private void button1_Click(object sender, EventArgs e)
      {

         if (CheckFirewal1l())
         {

            notifyIcon1.BalloonTipText = "Firewall Enabled";
            notifyIcon1.Text = "Firewall Enabled";

            notifyIcon1.ShowBalloonTip(1000);

         }
      }

      private void button2_Click(object sender, EventArgs e)
      {

         CheckFirewall2();

      }

VB.NET

   Private Sub button1_Click(ByVal sender As Object, _
         ByVal e As EventArgs)

      If CheckFirewal1l() Then

         notifyIcon1.BalloonTipText = "Firewall Enabled"
         notifyIcon1.Text = "Firewall Enabled"

         notifyIcon1.ShowBalloonTip(1000)

      End If

   End Sub

   Private Sub button2_Click(ByVal sender As Object, _
         ByVal e As EventArgs)

      CheckFirewall2()

   End Sub

As you can probably can tell, this application will be run from the Taskbar because it makes use of a Notifyicon. Ensure that you set the Form’s WindowState property to minimized and its Visible property to False. Also, do not forget to set the ShowInTaskBar property to False. After you have set all these properties for your Form, add the necessary code to launch this app from the Taskbar.

C#

      private void exitToolStripMenuItem_Click(object sender,
         EventArgs e)
      {

         Application.Exit();

      }


      private void showToolStripMenuItem_Click(object sender,
         EventArgs e)
      {

         this.WindowState = FormWindowState.Normal;

      }

VB.NET

   Private Sub exitToolStripMenuItem_Click(ByVal sender As Object, _
         ByVal e As EventArgs)

      Application.[Exit]()

   End Sub


   Private Sub showToolStripMenuItem_Click(ByVal sender As Object, _
         ByVal e As EventArgs)

      Me.WindowState = FormWindowState.Normal

   End Sub

Taskbar icon
Figure 3: Taskbar icon

Shortcut menu
Figure 4: Shortcut menu

Conclusion

Use caution when playing around with the system’s properties and tools, but if you know what you’re doing, it can be quite fun. I hope you have learned a thing or two today that you will put to good use.

Hannes DuPreez
Hannes DuPreez
Ockert J. du Preez is a passionate coder and always willing to learn. He has written hundreds of developer articles over the years detailing his programming quests and adventures. He has written the following books: Visual Studio 2019 In-Depth (BpB Publications) JavaScript for Gurus (BpB Publications) He was the Technical Editor for Professional C++, 5th Edition (Wiley) He was a Microsoft Most Valuable Professional for .NET (2008–2017).

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read