celobateira

Hi everyone,

During my installation program I opened some firewall ports with the netsh command

The thing is...

if I run my program the ports seem to be blocked...

if I turn off the firewall and then run the program again it works great...

if I turn on the firewall again and run the program it works great too

Do I have to re-initiate the firewall so my ports wont be blocked or something

How do I re-initiate(turn off-turn on) the firewall programatically

can I use the command netsh to do so

Thanx for the help


Re: Windows Filtering Platform (WFP) Setting up Fierwall ports

Jeremy Drake

I think this is not the right forum. I thought there was a windows firewall forum, but I can't seem to find it now.

Anyway, I think what you want can be best accomplished using the Windows Firewall APIs. See this url for an example of adding a program to the Windows Firewall:

http://msdn2.microsoft.com/en-us/library/aa366421.aspx

I have found that using the XP-compatible APIs do not always work on Vista. For an example using the Vista APIs, see this url:

http://msdn2.microsoft.com/en-us/library/aa364695.aspx

Hope this helps





Re: Windows Filtering Platform (WFP) Setting up Fierwall ports

celobateira

Thanx for the reply Jeremy... The problem still the same...

Here is the situation:

I have to open the port 1433 in a virtual machine (the sql port), and my app is on another virtual machine so I install my app opening the ports I want in the firewall.

If I go to windows firewall in my VMs the ports are open

But if I run my app it can't connect to the database server

If I disable the firewall in the DB server the app can connect, and if I re-enable the firewall again and run the app it can connect too..

It's odd, I don't know why this is happening








Re: Windows Filtering Platform (WFP) Setting up Fierwall ports

celobateira

I think the problem may be solved by adding the SQLBrowser and SQLServer services to my firewall...

I'm opening some firewall ports with this function:

Code Block

private void OpenFirewallPort(string protocol, int portNumber, string name)

{

INetFwMgr icfMgr = null;

try

{

Type TicfMgr = Type.GetTypeFromProgID("HNetCfg.FwMgr");

icfMgr = (INetFwMgr)Activator.CreateInstance(TicfMgr);

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

try

{

INetFwProfile profile;

INetFwOpenPort portClass;

Type TportClass = Type.GetTypeFromProgID("HNetCfg.FWOpenPort");

portClass = (INetFwOpenPort)Activator.CreateInstance(TportClass);

profile = icfMgr.LocalPolicy.CurrentProfile;

portClass.Scope = NetFwTypeLib.NET_FW_SCOPE_.NET_FW_SCOPE_ALL;

portClass.Enabled = true;

portClass.Name = name;

portClass.Port = portNumber;

portClass.Protocol =

NetFwTypeLib.NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP;

// Add the port to the ICF Permissions List

profile.GloballyOpenPorts.Add(portClass);

return;

}

catch (Exception ex2)

{

MessageBox.Show(ex2.Message);

}

}


I'm using the NetFwTypeLib namespace...

How can I add programs to my firewall using this namespace

Thanx in advance





Re: Windows Filtering Platform (WFP) Setting up Fierwall ports

Jeremy Drake

celobateira wrote:

How can I add programs to my firewall using this namespace

Adding code in to your function below, based on the VBScript code from my first link in the previous post and imitating the conventions of whatever language this is that you are using:

Code Block

private void OpenFirewallPort(string protocol, int portNumber, string name)

{

INetFwMgr icfMgr = null;

try

{

Type TicfMgr = Type.GetTypeFromProgID("HNetCfg.FwMgr");

icfMgr = (INetFwMgr)Activator.CreateInstance(TicfMgr);

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

try

{

INetFwProfile profile;

INetFwOpenPort portClass;

Type TportClass = Type.GetTypeFromProgID("HNetCfg.FWOpenPort");

portClass = (INetFwOpenPort)Activator.CreateInstance(TportClass);

profile = icfMgr.LocalPolicy.CurrentProfile;

portClass.Scope = NetFwTypeLib.NET_FW_SCOPE_.NET_FW_SCOPE_ALL;

portClass.Enabled = true;

portClass.Name = name;

portClass.Port = portNumber;

portClass.Protocol =

NetFwTypeLib.NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP;

// Add the port to the ICF Permissions List

profile.GloballyOpenPorts.Add(portClass);

INetFwAuthorizedApplication appClass;

Type TappClass = Type.GetTypeFromProgID("HNetCfg.FWAuthorizedApplication");

appClass = (INetFwAuthorizedApplication)Activator.CreateInstance(TappClass);

appClass.ProcessImageFileName = "%PROGRAMFILES%\Foo\Bar.exe";

appClass.Name = "Foo Bar";

appClass.Scope = NetFwTypeLib.NET_FW_SCOPE_.NET_FW_SCOPE_ALL;

appClass.IpVersion = NetFwTypeLib.NET_FW_IP_VERSION_.NET_FW_IP_VERSION_ANY;

appClass.Enabled = true;

profile.AuthorizedApplications.Add(appClass);

return;

}

catch (Exception ex2)

{

MessageBox.Show(ex2.Message);

}

}





Re: Windows Filtering Platform (WFP) Setting up Fierwall ports

celobateira

Thanx Jeremy, that should do the trick

Regards