如何使用Delphi在Win7防火墙中打开端口 [英] How to open port in Win7 Firewall using Delphi

查看:485
本文介绍了如何使用Delphi在Win7防火墙中打开端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Windows 7防火墙中打开一个端口,以通过Delphi进行端口转发,但是正如其他线程所说,Windows 7在防火墙中有多个配置文件(公共,专用),下一个代码仅向其中一个添加了例外.

代码:

procedure addPortToFirewall(EntryName:string;PortNumber:Cardinal); 
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;var 
  fwMgr,port:OleVariant; 
  profile:OleVariant; 
begin 
  fwMgr := CreateOLEObject('HNetCfg.FwMgr'); 
  profile := fwMgr.LocalPolicy.CurrentProfile; 
  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; 

我知道这是用于Windows XP的代码,但是用于Win 7的代码无法找到如何打开端口而不是应用程序的代码.

代码:

procedure TForm1.Button4Click(Sender: TObject);
const
NET_FW_PROFILE2_DOMAIN  = 1;
NET_FW_PROFILE2_PRIVATE = 2;
NET_FW_PROFILE2_PUBLIC  = 4;
NET_FW_IP_PROTOCOL_TCP = 6;
NET_FW_ACTION_ALLOW    = 1;
var
  fwPolicy2      : OleVariant;
  RulesObject    : OleVariant;
  Profile        : Integer;
  NewRule        : OleVariant;
begin
  Profile             := NET_FW_PROFILE2_PRIVATE OR NET_FW_PROFILE2_PUBLIC;
  fwPolicy2           := CreateOleObject('HNetCfg.FwPolicy2');
  RulesObject         := fwPolicy2.Rules;
  NewRule             := CreateOleObject('HNetCfg.FWRule');
  NewRule.Name        := 'Test Firwwall';
  NewRule.Description := 'Test Firewall';
  NewRule.Applicationname := 'Exe File';
  NewRule.Protocol := NET_FW_IP_PROTOCOL_TCP;
  NewRule.Enabled := TRUE;
  NewRule.Profiles := Profile;
  NewRule.Action := NET_FW_ACTION_ALLOW;
  RulesObject.Add(NewRule);
end;

最后为了使所有这些变得清楚,我如何使用delphi在Windows 7防火墙中打开A端口?用于多个配置文件(私人,公共).

谢谢.

解决方案

要在Windows 7下打开端口,必须指定HNetCfg.FWRule对象的LocalPortsDirection属性.这在MSDN文档 Using Windows Firewall with Advanced Security Using Windows Firewall with Advanced Security

{$APPTYPE CONSOLE}


uses
  SysUtils,
  ActiveX,
  ComObj,
  Variants;

procedure AddExceptionToFirewall(Const Caption, Executable: String;Port : Word);
const
NET_FW_PROFILE2_DOMAIN  = 1;
NET_FW_PROFILE2_PRIVATE = 2;
NET_FW_PROFILE2_PUBLIC  = 4;

NET_FW_IP_PROTOCOL_TCP = 6;
NET_FW_ACTION_ALLOW    = 1;
NET_FW_RULE_DIR_IN  = 1;
NET_FW_RULE_DIR_OUT = 2;
var
  fwPolicy2      : OleVariant;
  RulesObject    : OleVariant;
  Profile        : Integer;
  NewRule        : OleVariant;
begin
  Profile             := NET_FW_PROFILE2_PRIVATE OR NET_FW_PROFILE2_PUBLIC;
  fwPolicy2           := CreateOleObject('HNetCfg.FwPolicy2');
  RulesObject         := fwPolicy2.Rules;
  NewRule             := CreateOleObject('HNetCfg.FWRule');
  NewRule.Name        := Caption;
  NewRule.Description := Caption;
  NewRule.Applicationname := Executable;
  NewRule.Protocol := NET_FW_IP_PROTOCOL_TCP;
  NewRule.LocalPorts :=  Port;
  NewRule.Direction := NET_FW_RULE_DIR_OUT;
  NewRule.Enabled := TRUE;
  NewRule.Grouping := 'My Group';
  NewRule.Profiles := Profile;
  NewRule.Action := NET_FW_ACTION_ALLOW;
  RulesObject.Add(NewRule);
end;

begin
 try
    CoInitialize(nil);
    try
      AddExceptionToFirewall('MyAppRule','MyApp.exe', 3307);
    finally
      CoUninitialize;
    end;
 except
    on E:EOleException do
        Writeln(Format('EOleException %s %x', [E.Message,E.ErrorCode]));
    on E:Exception do
        Writeln(E.Classname, ':', E.Message);
 end;
 Writeln('Press Enter to exit');
 Readln;
end.

注意:此代码需要高程.

i want to open a port in Windows 7 Firewall for port forwarding via Delphi but as been said in other threads windows 7 has Multiple Profiles in Firewall (Public, Private) and the next code only adds the exception to one of them.

Code:

procedure addPortToFirewall(EntryName:string;PortNumber:Cardinal); 
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;var 
  fwMgr,port:OleVariant; 
  profile:OleVariant; 
begin 
  fwMgr := CreateOLEObject('HNetCfg.FwMgr'); 
  profile := fwMgr.LocalPolicy.CurrentProfile; 
  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; 

I know that this is the code used for Windows XP but the one for Win 7 i cannot locate how to open a port and not an application.

Code:

procedure TForm1.Button4Click(Sender: TObject);
const
NET_FW_PROFILE2_DOMAIN  = 1;
NET_FW_PROFILE2_PRIVATE = 2;
NET_FW_PROFILE2_PUBLIC  = 4;
NET_FW_IP_PROTOCOL_TCP = 6;
NET_FW_ACTION_ALLOW    = 1;
var
  fwPolicy2      : OleVariant;
  RulesObject    : OleVariant;
  Profile        : Integer;
  NewRule        : OleVariant;
begin
  Profile             := NET_FW_PROFILE2_PRIVATE OR NET_FW_PROFILE2_PUBLIC;
  fwPolicy2           := CreateOleObject('HNetCfg.FwPolicy2');
  RulesObject         := fwPolicy2.Rules;
  NewRule             := CreateOleObject('HNetCfg.FWRule');
  NewRule.Name        := 'Test Firwwall';
  NewRule.Description := 'Test Firewall';
  NewRule.Applicationname := 'Exe File';
  NewRule.Protocol := NET_FW_IP_PROTOCOL_TCP;
  NewRule.Enabled := TRUE;
  NewRule.Profiles := Profile;
  NewRule.Action := NET_FW_ACTION_ALLOW;
  RulesObject.Add(NewRule);
end;

For the final just to make all of this clear, how do i open A port in windows 7 Firewall using delphi? for multiple profiles (Private, Public).

Thank you in regards.

解决方案

To open a port under Windows 7 you must specify the LocalPorts and Direction properties of the HNetCfg.FWRule object. This is explainded in the MSDN Documentation Using Windows Firewall with Advanced Security

{$APPTYPE CONSOLE}


uses
  SysUtils,
  ActiveX,
  ComObj,
  Variants;

procedure AddExceptionToFirewall(Const Caption, Executable: String;Port : Word);
const
NET_FW_PROFILE2_DOMAIN  = 1;
NET_FW_PROFILE2_PRIVATE = 2;
NET_FW_PROFILE2_PUBLIC  = 4;

NET_FW_IP_PROTOCOL_TCP = 6;
NET_FW_ACTION_ALLOW    = 1;
NET_FW_RULE_DIR_IN  = 1;
NET_FW_RULE_DIR_OUT = 2;
var
  fwPolicy2      : OleVariant;
  RulesObject    : OleVariant;
  Profile        : Integer;
  NewRule        : OleVariant;
begin
  Profile             := NET_FW_PROFILE2_PRIVATE OR NET_FW_PROFILE2_PUBLIC;
  fwPolicy2           := CreateOleObject('HNetCfg.FwPolicy2');
  RulesObject         := fwPolicy2.Rules;
  NewRule             := CreateOleObject('HNetCfg.FWRule');
  NewRule.Name        := Caption;
  NewRule.Description := Caption;
  NewRule.Applicationname := Executable;
  NewRule.Protocol := NET_FW_IP_PROTOCOL_TCP;
  NewRule.LocalPorts :=  Port;
  NewRule.Direction := NET_FW_RULE_DIR_OUT;
  NewRule.Enabled := TRUE;
  NewRule.Grouping := 'My Group';
  NewRule.Profiles := Profile;
  NewRule.Action := NET_FW_ACTION_ALLOW;
  RulesObject.Add(NewRule);
end;

begin
 try
    CoInitialize(nil);
    try
      AddExceptionToFirewall('MyAppRule','MyApp.exe', 3307);
    finally
      CoUninitialize;
    end;
 except
    on E:EOleException do
        Writeln(Format('EOleException %s %x', [E.Message,E.ErrorCode]));
    on E:Exception do
        Writeln(E.Classname, ':', E.Message);
 end;
 Writeln('Press Enter to exit');
 Readln;
end.

Note : This code requires elevation.

这篇关于如何使用Delphi在Win7防火墙中打开端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆