Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Open Port in Windows Firewall using C++ Builder?

Status
Not open for further replies.

webowner47

Programmer
Nov 26, 2018
16
DZ
This the code i found in Stack Overflow but in Delphi not C++:

Code:
uses
  SysUtils,
  ActiveX,
  ComObj,
  Variants;

procedure AddExceptionToFirewall(Const Caption, Executable: String;Port : Word);
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;
NET_FW_RULE_DIR_IN  = 1;
NET_FW_RULE_DIR_OUT = 2;
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.LocalPorts :=  Port;
  NewRule.Direction := NET_FW_RULE_DIR_OUT;
  NewRule.Enabled := TRUE;
  NewRule.Grouping := 'My Group';
  NewRule.Profiles := Profile;
  NewRule.Action := NET_FW_ACTION_ALLOW;
  RulesObject.Add(NewRule);
end;

So, i tried my best to translate that in C++:

Code:
const NET_FW_PROFILE2_DOMAIN  = 1;
	const NET_FW_PROFILE2_PRIVATE = 2;
	const NET_FW_PROFILE2_PUBLIC  = 4;

	const NET_FW_IP_PROTOCOL_TCP = 6;
	const NET_FW_IP_PROTOCOL_UDP = 17;
	const NET_FW_ACTION_ALLOW    = 1;
	const NET_FW_RULE_DIR_IN  = 1;
	const NET_FW_RULE_DIR_OUT = 2;

	Variant fwPolicy2, RulesObject, NewRule;
	DWORD Profile;

	Profile   = NET_FW_PROFILE2_PRIVATE | NET_FW_PROFILE2_PUBLIC;
	fwPolicy2 = CreateOleObject("HNetCfg.FwPolicy2");
	RulesObject = fwPolicy2.OlePropertyGet("Rules");
	NewRule   = CreateOleObject("HNetCfg.FWRule");
	NewRule.OlePropertyGet("Name") = "Text Firewall";
	NewRule.OlePropertyGet("Description") = "Text Firewall";
	NewRule.OlePropertyGet("Applicationname") = "System_RCC.exe";
	NewRule.OlePropertyGet("Protocol")   = NET_FW_IP_PROTOCOL_TCP;
	NewRule.OlePropertyGet("LocalPorts") = Edit1->Text;
	NewRule.OlePropertyGet("Direction")  = NET_FW_RULE_DIR_OUT;
	NewRule.OlePropertyGet("Enabled")   = true;
	NewRule.OlePropertyGet("Grouping")  = "";
	NewRule.OlePropertyGet("Profiles")  = Profile;
	NewRule.OlePropertyGet("Action")  = NET_FW_ACTION_ALLOW;
	RulesObject.Add(NewRule);

But, I get error like this:

Compile Error said:
add' is not a member of 'variant' borland c++ builder

Please i really need some help
 
Try RulesObject->Add(NewRule);


James P. Cottingham
I'm number 1,229!
I'm number 1,229!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top