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 = NET_FW_ACTION_.NET_FW_ACTION_BLOCK;
            firewallRule.ApplicationName = path;
            firewallRule.Direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_IN;
            firewallRule.Enabled = true;
            firewallRule.Profiles = (int)NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_ALL;
            firewallRule.Protocol = (int)NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_ANY;
            firewallRule.EdgeTraversal = false;
            firewallRule.Name = getRuleReadableName(firewallRule);
            firewallRule.LocalAddresses = "*";
            firewallRule.RemoteAddresses = "*";
            firewallRule.Grouping = GROUP;
            return firewallRule;
        }

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


#### 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