How to programmatically add an application or port to Windows Firewall on Windows XP?
Asked
Active
Viewed 3,896 times
13
-
possible duplicate of [Programatically Add Exception to windows vista firewall .](http://stackoverflow.com/questions/1409896/programatically-add-exception-to-windows-vista-firewall) – Rob Kennedy Apr 20 '11 at 13:34
-
possible duplicate of [Add to Firewall Exception list](http://stackoverflow.com/questions/2384718/add-to-firewall-exception-list) – The_Fox Apr 20 '11 at 14:11
-
2This answer only works in xp. edited title since OP accepted this and its useful info, therefore not a dupe since the duplicate works in win7 and vista only. – Warren P Jul 07 '12 at 22:09
2 Answers
17
Try this code extracted from our open source SQlite3UI.pas unit:
function GetXPFirewall(var fwMgr, profile: OleVariant): boolean;
begin
Result := (Win32Platform=VER_PLATFORM_WIN32_NT) and
(Win32MajorVersion>5) or ((Win32MajorVersion=5) and (Win32MinorVersion>0));
if result then // need Windows XP at least
try
fwMgr := CreateOleObject('HNetCfg.FwMgr');
profile := fwMgr.LocalPolicy.CurrentProfile;
except
on E: Exception do
result := false;
end;
end;
const
NET_FW_PROFILE_DOMAIN = 0;
NET_FW_PROFILE_STANDARD = 1;
NET_FW_IP_VERSION_ANY = 2;
NET_FW_IP_PROTOCOL_UDP = 17;
NET_FW_IP_PROTOCOL_TCP = 6;
NET_FW_SCOPE_ALL = 0;
NET_FW_SCOPE_LOCAL_SUBNET = 1;
procedure AddApplicationToXPFirewall(const EntryName, ApplicationPathAndExe: string);
var fwMgr, profile, app: OleVariant;
begin
if GetXPFirewall(fwMgr,profile) then
try
if profile.FirewallEnabled then begin
app := CreateOLEObject('HNetCfg.FwAuthorizedApplication');
try
app.ProcessImageFileName := ApplicationPathAndExe;
app.Name := EntryName;
app.Scope := NET_FW_SCOPE_ALL;
app.IpVersion := NET_FW_IP_VERSION_ANY;
app.Enabled :=true;
profile.AuthorizedApplications.Add(app);
finally
app := varNull;
end;
end;
finally
profile := varNull;
fwMgr := varNull;
end;
end;
procedure AddPortToXPFirewall(const EntryName: string; PortNumber: cardinal);
var fwMgr, profile, port: OleVariant;
begin
if GetXPFirewall(fwMgr,profile) then
try
if profile.FirewallEnabled then begin
port := CreateOLEObject('HNetCfg.FWOpenPort');
port.Name := EntryName;
port.Protocol := NET_FW_IP_PROTOCOL_TCP;
port.Port := PortNumber;
port.Scope := NET_FW_SCOPE_ALL;
port.Enabled := true;
profile.GloballyOpenPorts.Add(port);
end;
finally
port := varNull;
profile := varNull;
fwMgr := varNull;
end;
end;
It will allow you to add an application or a port to the XP firewall. Should work from Delphi 6 up to XE.

Arnaud Bouchez
- 42,305
- 3
- 71
- 159
-
1I've updated the source of the unit to work on XP, Vista and Seven, either for an application, either for a port. See http://synopse.info/forum/viewtopic.php?pid=4652#p4652 – Arnaud Bouchez Jul 11 '12 at 06:51
6
Scripting the Windows Firewall is possible, see Scripting the Windows Firewall
And code examples for example here

mjn
- 36,362
- 28
- 176
- 378
-
-
In this case you should try to import the type library, see my link, it mentions that the type library DLL file `is usually located in "C:\Windows\System32\hnetcfg.dll"` (this article is about XP, I checked in Windows 7 and a file with this name is there) – mjn Apr 20 '11 at 10:27
-
To import it try `tlibimp -P c:\windows\system32\hnetcfg.dll` - however looking at its content I am not sure if this is the correct file – mjn Apr 20 '11 at 10:31
-
4Also can use late binding as in the question [Add to Firewall Exception list](http://stackoverflow.com/questions/2384718/add-to-firewall-exception-list) – Sertac Akyuz Apr 20 '11 at 11:09
-
Hi Michael, due to a new answer I stumbled upon this thread. Your answer according to the current rules to link only to reply, maybe you want to improve it. – bummi Nov 16 '14 at 07:35