简体   繁体   English

适用于XP的Windows防火墙规则

[英]Windows firewall rule for XP

如何以编程方式将应用程序或端口添加到Windows XP上的Windows防火墙?

Try this code extracted from our open source SQlite3UI.pas unit: 尝试从我们的开源SQlite3UI.pas单元中提取的代码:

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. 它允许您将应用程序或端口添加到XP防火墙。 Should work from Delphi 6 up to XE. 应该从Delphi 6到XE工作。

Scripting the Windows Firewall is possible, see Scripting the Windows Firewall 可以编写Windows防火墙脚本 ,请参阅编写Windows防火墙脚本

And code examples for example here 这里举例说明代码示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查Windows防火墙中的规则? - How to check a rule in windows firewall? 使用Delphi删除Windows防火墙规则(异常) - Remove Windows Firewall Rule (Exception) using Delphi 在 PowerShell 上添加 Windows 防火墙规则 - Add Windows firewall rule over PowerShell 在Windows Azure上重新启用远程桌面Windows防火墙规则 - Re-enable Remote Desktop Windows Firewall Rule on Windows Azure 使用Windows防火墙API创建适用于服务运行的所有应用程序的规则 - Creating a rule with the Windows Firewall API that applies to all applications run by a service Windows 防火墙 - Laravel 工匠服务 - 允许端口进入入站规则(不起作用) - Windows Firewall - Laravel Artisan Serve - Allow Port in Inbound Rule (not working) C# - 使用 INetFwPolicy2 添加的 Windows 防火墙规则无效 - C# - Windows firewall rule added using INetFwPolicy2 not in effect 对于Windows XP OS,使用Win32 API进行防火墙启动/停止 - Firewall start/stop using win32 api for windows XP os Google windows 实例未启用 445/139 端口,即使防火墙规则定义为允许 - Google windows instance not enabling 445/139 port even though firewall rule is defined to allow 以编程方式将规则添加到AVG Firewall - Programmatically add rule to AVG Firewall
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM