gpt4 book ai didi

windows - 通过 API Windows Firewall Delphi 添加规则阻止 IP 地址

转载 作者:可可西里 更新时间:2023-11-01 09:59:07 26 4
gpt4 key购买 nike

下午好。没有人面对在 Delphi 中添加 Windows 防火墙规则的问题吗?我需要限制来自特定 IP 地址的传入和传出连接。此代码在启动时向我抛出一个应用程序错误:

Exception EVariantInvalidOpError in module Project1.exe at 00033E74. Invalid variant operation.

有没有人有什么错误?

procedure TForm1.FormCreate(Sender: TObject);
Const
NET_FW_IP_PROTOCOL_TCP = 6;
NET_FW_IP_PROTOCOL_UDP = 17;
NET_FW_ACTION_ALLOW = 1;
NET_FW_RULE_DIR_in = 1;

var
CurrentProfiles : OleVariant;
fwPolicy2 : OleVariant;
RulesObject : OleVariant;
NewRule : OleVariant;
txtAddress : OleVariant;
NET_FW_ACTION_ : OleVariant;
NET_FW_RULE_DIRECTION_: OleVariant;

begin
// Create the FwPolicy2 object.
fwPolicy2 := CreateOleObject('HNetCfg.FwPolicy2');
RulesObject := fwPolicy2.Rules;
CurrentProfiles := fwPolicy2.CurrentProfileTypes;
txtaddress.text:='192.168.1.33';
//Create a Rule Object.
NewRule := CreateOleObject('HNetCfg.FWRule');

newrule.Name:= 'BrutalNT: IP Access Block ' + txtAddress.Text;
newrule.Description := 'Block Incoming Connections from IP Address.';
newrule.Action := 1;
newrule.Direction := NET_FW_RULE_DIR_IN;
newrule.Enabled := true;
newrule.InterfaceTypes := 'All';
newrule.RemoteAddresses := txtAddress.Text;

//Add a new rule
RulesObject.Add(NewRule);
end;

最佳答案

您正在使用 txtAddress : OleVariant 但背后没有任何结构。所以你不能使用像 txtAddress.text 这样的东西,因为没有任何地方可以映射它。

只需将类型更改为stringtxtAddress无需为OleVariant类型。

procedure TForm1.FormCreate(Sender: TObject);
Const
NET_FW_IP_PROTOCOL_TCP = 6;
NET_FW_IP_PROTOCOL_UDP = 17;
NET_FW_ACTION_BLOCK = 0;
NET_FW_ACTION_ALLOW = 1;
NET_FW_RULE_DIR_IN = 1;

var
CurrentProfiles : OleVariant;
fwPolicy2 : OleVariant;
RulesObject : OleVariant;
NewRule : OleVariant;
txtAddress : string; // OleVariant;

begin
// Create the FwPolicy2 object.
fwPolicy2 := CreateOleObject('HNetCfg.FwPolicy2');
RulesObject := fwPolicy2.Rules;
CurrentProfiles := fwPolicy2.CurrentProfileTypes;
txtaddress{.text}:='192.168.1.33';
//Create a Rule Object.
NewRule := CreateOleObject('HNetCfg.FWRule');

Newrule.Name := 'BrutalNT: IP Access Block ' + txtAddress{.Text};
Newrule.Description := 'Block Incoming Connections from IP Address.';
Newrule.Action := NET_FW_ACTION_BLOCK{1};
Newrule.Direction := NET_FW_RULE_DIR_IN;
Newrule.Enabled := true;
Newrule.InterfaceTypes := 'All';
Newrule.RemoteAddresses := txtAddress{.Text};

//Add a new rule
RulesObject.Add(NewRule);
end;

顺便说一句如果你想阻止你必须设置NewRule.Action := 0; (NET_FW_ACTION_BLOCK)

关于windows - 通过 API Windows Firewall Delphi 添加规则阻止 IP 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25281216/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com