×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

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

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

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

(OP)
This the code i found in Stack Overflow but in Delphi not C++:

CODE --> Delphi

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 --> C++

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:

Quote (Compile Error)


add' is not a member of 'variant' borland c++ builder

Please i really need some help

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

Try RulesObject->Add(NewRule);

James P. Cottingham
I'm number 1,229!
I'm number 1,229!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close