Remove Windows Firewall Rule (Exception) Using Delphi

I am trying to manage firewall rules (exceptions) in Windows 7 using Delphi XE3. I found very interesting code to add a rule to the Windows firewall, but did not delete (delete) it. Can someone please help?

Here is the code to add the rule:

procedure AddExceptToFirewall(const Caption, AppPath: String); // Uses ComObj const NET_FW_PROFILE2_PRIVATE = 2; NET_FW_PROFILE2_PUBLIC = 4; NET_FW_IP_PROTOCOL_TCP = 6; NET_FW_ACTION_ALLOW = 1; var Profile: Integer; Policy2: OleVariant; RObject: OleVariant; NewRule: OleVariant; begin Profile := NET_FW_PROFILE2_PRIVATE OR NET_FW_PROFILE2_PUBLIC; Policy2 := CreateOleObject('HNetCfg.FwPolicy2'); RObject := Policy2.Rules; NewRule := CreateOleObject('HNetCfg.FWRule'); NewRule.Name := Caption; NewRule.Description := Caption; NewRule.ApplicationName := AppPath; NewRule.Protocol := NET_FW_IP_PROTOCOL_TCP; NewRule.Enabled := True; NewRule.Grouping := ''; NewRule.Profiles := Profile; NewRule.Action := NET_FW_ACTION_ALLOW; RObject.Add(NewRule); end; 

Thanks!

+5
source share
1 answer

You simply call INetFWRules.Remove , passing the name of the rule. The name is the same name that you used when creating it ( RObject.Name in the code you specified above).

 // Note: Normal COM exception handling should be used. Omitted for clarity. procedure RemoveExceptFromFirewall(const RuleName: String); const NET_FW_PROFILE2_PRIVATE = 2; NET_FW_PROFILE2_PUBLIC = 4; var Profile: Integer; Policy2: OleVariant; RObject: OleVariant; begin Profile := NET_FW_PROFILE2_PRIVATE OR NET_FW_PROFILE2_PUBLIC; Policy2 := CreateOleObject('HNetCfg.FwPolicy2'); RObject := Policy2.Rules; RObject.Remove(RuleName); end; 

The BTW related documentation contains almost nothing. I have provided the link for reference only.

+5
source

Source: https://habr.com/ru/post/1208458/


All Articles