簡體   English   中英

如何使用Delphi在Win7防火牆中打開端口

[英]How to open port in Win7 Firewall using Delphi

我想在 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 防火牆中打開一個端口? 適用於多個配置文件(私人、公共)。

謝謝你的問候。

要在 Windows 7 下打開端口,您必須指定HNetCfg.FWRule對象的LocalPortsDirection屬性。 這在Using Windows Firewall with Advanced Security的 MSDN 文檔中進行了解釋

{$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.

注意:此代碼需要提升。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM