473,466 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Adding a port to the XP-Firewall

Hi,

is there a programmatical way to add a port to the XP SP2 Firewall and
open it or does this have to be configured manually??

Martin
Nov 16 '05 #1
2 16927
Hi Martin,

We can use the hnetcfg.dll Com object.
Here is a link you may take a look.
Adding a Port to Current Profile (VBScript)
http://msdn.microsoft.com/library/de...us/ics/ics/wf_
adding_port_to_profile.asp

We can convert it into C# as below.
Here is the code below.
[NOTE: we need add a COM reference to the hnetcfg.dll
first(<windows>\system32\hnetcfg.dll)

using System;
using NetFwTypeLib;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Diagnostics;
namespace OpenPort
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
INetFwMgr icfMgr =null;
try
{
Type TicfMgr = Type.GetTypeFromProgID("HNetCfg.FwMgr");
icfMgr = (INetFwMgr)Activator.CreateInstance(TicfMgr);
}
catch (Exception ex)
{
Trace.WriteLine(ex.ToString());
return;
}
try
{

INetFwProfile profile;
INetFwOpenPort portClass;
Type TportClass = Type.GetTypeFromProgID("HNetCfg.FWOpenPort");
portClass = (INetFwOpenPort)Activator.CreateInstance(TportClas s);
// Get the current profile
profile = icfMgr.LocalPolicy.CurrentProfile;

// Set the port properties
portClass.Scope = NetFwTypeLib.NET_FW_SCOPE_.NET_FW_SCOPE_ALL;
portClass.Enabled = true;
portClass.Name = "TestPort";
portClass.Port = 1124;
portClass.Protocol =
NetFwTypeLib.NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCO L_TCP;;

// Add the port to the ICF Permissions List
profile.GloballyOpenPorts.Add(portClass);
return;
}
catch (Exception ex)
{
Trace.WriteLine(ex.ToString());
}
}
}
}
Best regards,

Perter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 16 '05 #2
Thanks Peter!

This is a really good one.

Martin
Peter Huang [MSFT] wrote:
Hi Martin,

We can use the hnetcfg.dll Com object.
Here is a link you may take a look.
Adding a Port to Current Profile (VBScript)
http://msdn.microsoft.com/library/de...us/ics/ics/wf_
adding_port_to_profile.asp

We can convert it into C# as below.
Here is the code below.
[NOTE: we need add a COM reference to the hnetcfg.dll
first(<windows>\system32\hnetcfg.dll)

using System;
using NetFwTypeLib;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Diagnostics;
namespace OpenPort
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
INetFwMgr icfMgr =null;
try
{
Type TicfMgr = Type.GetTypeFromProgID("HNetCfg.FwMgr");
icfMgr = (INetFwMgr)Activator.CreateInstance(TicfMgr);
}
catch (Exception ex)
{
Trace.WriteLine(ex.ToString());
return;
}
try
{

INetFwProfile profile;
INetFwOpenPort portClass;
Type TportClass = Type.GetTypeFromProgID("HNetCfg.FWOpenPort");
portClass = (INetFwOpenPort)Activator.CreateInstance(TportClas s);
// Get the current profile
profile = icfMgr.LocalPolicy.CurrentProfile;

// Set the port properties
portClass.Scope = NetFwTypeLib.NET_FW_SCOPE_.NET_FW_SCOPE_ALL;
portClass.Enabled = true;
portClass.Name = "TestPort";
portClass.Port = 1124;
portClass.Protocol =
NetFwTypeLib.NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCO L_TCP;;

// Add the port to the ICF Permissions List
profile.GloballyOpenPorts.Add(portClass);
return;
}
catch (Exception ex)
{
Trace.WriteLine(ex.ToString());
}
}
}
}
Best regards,

Perter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ed | last post by:
Here's the scene: 1) XP Pro, with IIS installed and running, as well as complete .NET Architect Enterprise development environment 2) Tomcat server 4.1.27 in the default directory...
0
by: I.P. | last post by:
Hi, it's my story. I have two 4.0.14 mysql server on one machine with win XP Professional polish version. First acts as master: on port 3300 Second acts as slave: on port 3301 below my...
0
by: I.P. | last post by:
No one has replied to my post. ----- Original Message ----- From: "I.P." <jancio_wodnik@wp.pl> To: <mysql@lists.mysql.com> Sent: Monday, August 18, 2003 1:01 PM Subject: mysql 4.0.14 +...
0
by: I.P. | last post by:
------=_NextPart_000_03FF_01C368A4.75720DC0 Content-Type: text/plain; charset="iso-8859-2" Content-Transfer-Encoding: quoted-printable Hi, it's my story. I have two 4.0.14 mysql server on...
3
by: Billism | last post by:
My email client (I programmed) recently has stopped being able to send email with my XP machine. I have had 3 other friends test it and the same thing happens. The program has not changed, but...
6
by: Novice Experl | last post by:
I'd like to write a simple application that interfaces with the parallel port, and changes the data on it according to keyboard input. I hope I can get it to run under windows xp and / or windows...
20
by: Jason Dravet | last post by:
Some time ago I wrote an application that controls the projectors in our classrooms(turn on, turn off, and volume control). This application is written in VB.net 2003. It was working perfectly...
3
by: Sells, Fred | last post by:
I'm using MSW XP Pro with Python 2.4 to develop but production will be Linux with Python 2.3. (could upgrade to 2.4 if absolutely necessary) I can also switch to Linux for development if...
9
by: Alexandre Brisebois | last post by:
Hi all, How can I have my application running on port 80 while ISS and other applications also run through port 80? for remoting (without hosting it in IIS) and or simply using sockets. ...
6
by: abhi | last post by:
Hello, I want to create a small project which will display scrolling text on LED that will be attached to Parallel port. I want to create this project in C language(possibly in Mingw) on Windows...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.