Add a rule blocking the IP address through the API Windows Firewall Delphi

Good afternoon. Nobody faced with the addition of a Windows Firewall rules in Delphi? I need to restrict incoming and outgoing connections from specific IP addresses. This code throws me an application at startup with an error:

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

Does anyone have what error?

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;

Upvotes: 4

Views: 2562

Answers (1)

Sir Rufo
Sir Rufo

Reputation: 19096

You are using txtAddress : OleVariant but without any structure behind. So you cannot use something like txtAddress.text, because there is nothing where this can be mapped.

Simply change the type to string, there is no need for txtAddress to be of type 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;

BTW If you want to block you have to set NewRule.Action := 0; (NET_FW_ACTION_BLOCK)

Upvotes: 4

Related Questions