#1 2012-07-07 16:43:39

array81
Member
From: Italy
Registered: 2010-07-23
Posts: 411

Windows Firewall Ports

On SQLite3UI.pas I have found these procedures:

procedure AddApplicationToXPFirewall(const EntryName, ApplicationPathAndExe: string);
procedure AddPortToXPFirewall(const EntryName: string; PortNumber: cardinal);

I need use them on my application to change firewall settings. I just have fix AddApplicationToXPFirewall to works also with Private profile (in this moment in fact it works only with Public profile - on Windows 7).

Now I need use AddPortToXPFirewall on Windows 7 but I don't know how I need edit it. In fact in this moment it doesn't work.
I need of AddPortToXPFirewall to open 2 ports by my application (both on Private and Pubblic profiles).

Thanks

Offline

#2 2012-07-07 16:49:41

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,666
Website

Re: Windows Firewall Ports

This function is meant to run as administrator.
During program install.

I'm not sure you will be allowed to change firewall configuration with. User rights, for obvious security reasons...

Online

#3 2012-07-07 22:40:04

array81
Member
From: Italy
Registered: 2010-07-23
Posts: 411

Re: Windows Firewall Ports

This is OK, but AddPortToXPFirewall  doesn't work on Windows 7 (as administrator), I need fix it to run as administrator not as user.

Offline

#4 2012-07-08 10:40:31

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,666
Website

Re: Windows Firewall Ports

Sounds like if http://msdn.microsoft.com/en-us/library … 8(v=vs.85) is to be taken in account for Seven.

Online

#5 2012-07-08 10:56:46

array81
Member
From: Italy
Registered: 2010-07-23
Posts: 411

Re: Windows Firewall Ports

I wrote this code:

procedure AddPortTo7Firewall(const EntryName: string; PortNumber: cardinal);
var
  fwMgr, profile, NewRule, RulesObject: OleVariant;
begin
  fwMgr := CreateOleObject('HNetCfg.FwPolicy2');
  profile := NET_FW_PROFILE2_PRIVATE OR NET_FW_PROFILE2_PUBLIC;

  try
    RulesObject         := fwMgr.Rules;
    NewRule             := CreateOleObject('HNetCfg.FWRule');
    try
      NewRule.Name        := EntryName;
      NewRule.Description := EntryName;
      NewRule.LocalPorts := PortNumber;
      NewRule.Protocol := NET_FW_IP_PROTOCOL_TCP;
      NewRule.Enabled := TRUE;
      NewRule.Profiles := Profile;
      NewRule.Action := NET_FW_ACTION_ALLOW;
      RulesObject.Add(NewRule);
    finally
      NewRule := varNull;
      RulesObject := varNull;
    end;
  finally
    profile := varNull;
    fwMgr := varNull;
  end;
end;

I think this is the right way but I get an error on NewRule.LocalPorts, and I don't understand why, Any ideas?

Offline

#6 2012-07-08 12:57:57

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,666
Website

Re: Windows Firewall Ports

Did you try to allocate it by application?
See http://stackoverflow.com/questions/9180 … -locations answer.

Online

#7 2012-07-08 13:56:14

array81
Member
From: Italy
Registered: 2010-07-23
Posts: 411

Re: Windows Firewall Ports

Yes, I just see it and AddExceptionToFirewall (of stackoverflow) works well, but with it I can only add application not port. I need open a port.

Any ideas?

Offline

#8 2012-07-09 05:26:01

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,666
Website

Re: Windows Firewall Ports

LocalPorts property sounds to be a string (BSTR=WideString).
See http://msdn.microsoft.com/en-us/library … 5).aspx#Y0

Try

NewRule.LocalPorts := IntToStr(PortNumber);

Online

#9 2012-07-09 07:39:58

array81
Member
From: Italy
Registered: 2010-07-23
Posts: 411

Re: Windows Firewall Ports

If I remember correctly I have tried to use an string without success. I'll try again (in this moment I'm not in my office) and I'll let you know.

Offline

#10 2012-07-09 13:02:40

array81
Member
From: Italy
Registered: 2010-07-23
Posts: 411

Re: Windows Firewall Ports

I have try it but I get an error on IntToStr(PortNumber); "Parameter is not correct.".

I don't understand why...

Offline

#11 2012-07-09 20:16:19

array81
Member
From: Italy
Registered: 2010-07-23
Posts: 411

Re: Windows Firewall Ports

OK I think I have found the problem.
This code works:

procedure AddPortTo7Firewall(const EntryName: string; PortNumber: cardinal);
var
  fwMgr, profile, NewRule, RulesObject: OleVariant;
begin
  fwMgr := CreateOleObject('HNetCfg.FwPolicy2');
  profile := NET_FW_PROFILE2_PRIVATE OR NET_FW_PROFILE2_PUBLIC;

  try
    RulesObject         := fwMgr.Rules;
    NewRule             := CreateOleObject('HNetCfg.FWRule');
    try
      NewRule.Name        := EntryName;
      NewRule.Description := EntryName;
      NewRule.Protocol := NET_FW_IP_PROTOCOL_TCP;
      NewRule.LocalPorts := PortNumber;
      NewRule.Enabled := TRUE;
      NewRule.Profiles := Profile;
      NewRule.Action := NET_FW_ACTION_ALLOW;
      RulesObject.Add(NewRule);
    finally
      NewRule := varNull;
      RulesObject := varNull;
    end;
  finally
    profile := varNull;
    fwMgr := varNull;
  end;
end;

NOTE: set NewRule.Protocol before NewRule.LocalPorts.

Thanks again.

Offline

#12 2012-07-10 11:41:33

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,666
Website

Re: Windows Firewall Ports

I tried to add the Vista/Seven firewall version of these functions to SQLite3UI.pas.

See http://synopse.info/fossil/info/b517ce66e9

Online

#13 2016-05-19 06:39:55

edwinsn
Member
Registered: 2010-07-02
Posts: 1,218

Re: Windows Firewall Ports

Any info about if the AddApplicationToFirewall() function works under Windows 8/8.1/10?


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

Board footer

Powered by FluxBB