pdulvp.fr

What's interesting in Easy-Firewall code

> draft This article describes interesting code parts in the Easy Firewall project. How to retrieve icons, how to define Windows Firewall rules with Windows INetFw API

Easy-Firewall

A small UI is easier than a script.

Why .NET

Well, everything was already written.

Thanks to George Mamaladze in 2007, within its article in Code Project, https://www.codeproject.com/Articles/19003/The-managed-classes-to-read-Windows-Firewall-confi

There is an DLL allowing to do this kind of stuff

NetFwTypeLib

Windows Firewall for Windows XP with SP2

How to create a rule

The magic comes to begin !

`

private INetFwRule createRule(String path)

{

INetFwRule firewallRule = (INetFwRule)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule"));

firewallRule.Action = NETFWACTION.NETFWACTIONBLOCK;

firewallRule.ApplicationName = path;

firewallRule.Direction = NETFWRULEDIRECTION.NETFWRULEDIRIN;

firewallRule.Enabled = true;

firewallRule.Profiles = (int)NETFWPROFILETYPE2.NETFWPROFILE2ALL;

firewallRule.Protocol = (int)NETFWIPPROTOCOL.NETFWIPPROTOCOL_ANY;

firewallRule.EdgeTraversal = false;

firewallRule.Name = getRuleReadableName(firewallRule);

  • ";
  • ";

firewallRule.Grouping = GROUP;

return firewallRule;

}

`

One rule for Inbound, another for Outbound. That's it.

``Direction: NETFWRULEDIRIN / NETFWRULEDIROUT``

Retrieve the icon of a program

Define all DLL imports

https://github.com/pdulvp/easy-firewall/blob/master/WinIcons.cs

L19-L68

Create an ImageList

https://github.com/pdulvp/easy-firewall/blob/master/Form1.cs

L46

Retrieve the icon of an program

https://github.com/pdulvp/easy-firewall/blob/master/Form1.cs

L136-L151