Delphi 7 Windows Vista / 7防火墙例外网络位置

时间:2012-02-07 16:51:52

标签: delphi windows-vista delphi-7 windows-firewall

我有根据http://www.activexperts.com/activmonitor/windowsmanagement/scripts/networking/windowsfirewall/

找到并实现的这段代码
procedure AddExceptionToFirewall (Caption: String; Executable: String);
var
  FirewallMsg: OleVariant;
  Application: OleVariant;
  CurrentProfile: OleVariant;
begin
  FirewallMsg:= CreateOLEObject ('HNetCfg.FwMgr');
  CurrentProfile:= FirewallMsg.LocalPolicy.CurrentProfile;
  Application:= CreateOLEObject ('HNetCfg.FwAuthorizedApplication');
  Application.ProcessImageFileName:= Executable;
  Application.Name:= Caption;
  Application.Scope:= FW_SCOPE_ALL;
  Application.IpVersion:= FW_IP_VERSION_ANY;
  Application.Enabled:= True;
  CurrentProfile.AuthorizedApplications.Add (Application);
end;

问题是,在Windows 7上,它仅将例外添加为公共而不是私有,因为您可以在此处 RED 中看到

enter image description here

当设置为Public时,我的程序在通过FTP连接访问我的主机时出现问题,从而导致我的程序无用。 此问题仅适用于Windows Vista / 7;在XP上,当前的配置工作正常。

如果您有任何线索或有用的指示,请分享。

1 个答案:

答案 0 :(得分:9)

从Windows Vista开始,您必须使用INetFwPolicy2INetFwRule接口才能访问新的防火墙功能。

尝试此示例,在公共和私人资料中添加新规则。

procedure AddExceptionToFirewall(Const Caption, Executable: String);
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        := Caption;
  NewRule.Description := Caption;
  NewRule.Applicationname := Executable;
  NewRule.Protocol := NET_FW_IP_PROTOCOL_TCP;
  NewRule.Enabled := TRUE;
  NewRule.Profiles := Profile;
  NewRule.Action := NET_FW_ACTION_ALLOW;
  RulesObject.Add(NewRule);
end;