Click here to Skip to main content
15,883,535 members
Articles / Desktop Programming / Windows Forms
Article

Using the Internet Connection Sharing (ICS) and Internet Connection Firewall (ICF) COM Interfaces

Rate me:
Please Sign up or sign in to vote.
4.33/5 (10 votes)
6 Nov 2004CPOL2 min read 65.1K   1.2K   34   8
The NetworkConnectionCollection is a strongly-typed interface that is meant to simplify access to the Internet Connection Sharing and Internet Connection Firewall interfaces for each of the network connections registered on the machine.

Sample Image - NetConColDemo.png

Introduction

With the release of Windows XP's Service Pack 2, Microsoft's Internet Connection Sharing (ICS) and Internet Connection Firewall (ICF) features quickly obtained a large degree of notoriety. Many developers have been actively looking for ways to easily "peek and poke" at the ICS & ICF configurations on a given machine.

The good news is that Microsoft released the interfaces with a COM wrapper. The bad news is that it isn't well advertised and it's not very intuitive to use. Getting a full set of details on a particular connection's configuration can require several method calls.

Overview

This project contains a strongly-typed collection named NetworkConnectionCollection. When you create an instance of this collection, it automatically uses the ICS/ICF COM interfaces to iterate all of the Network Connections on the local machine and create NetworkConnection objects (as members of the NetworkConnectionCollection).

The NetworkConnection class impliments three ICS/ICF interfaces: INetConnection, INetConnectionProps and INetSharingConfiguration. When you use the ICS/ICF COM interfaces directly, you need to reference each of these independantly. By using the NetworkConnection class, you can access all of the members in one place.

Work Remaining

I haven't spent much time on this, since it does what I need at this point. One obvious improvement would be to overload a few of the members to expose/use native types (e.g. the INetConnectionProps.GUID property returns a String, but a System.Guid would be preferable). Another improvement would be to wrap the PortMappings.

References

Microsoft has exposed the API for the ICS & ICF in a couple of places, but this code uses the COM interface HNetCfg.HNetShare. You can add a reference to this interface to your own projects by using the Reference Browser to select HNETCFG.DLL (typically located in the "C:\Windows\System32\" directory).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Wyvern Software
United States United States
Tony Selke is an independant consultant who has spent the last 20 years working with Microsoft technologies (VB, VC++, ASP, J++, C#, VB.NET, SQL Server, etc.) to develop solutions used in all kinds of market verticals (industrial, pharmaceutical, financial, marketing, multimedia, educational, telecommunications, etc.). He obtained his first MCSD certification in 1998 and his second in 2004, with an MCDBA in 2005. In addition, he has taught courses for MCSD certification students as well as programming classes at Penn State University.

Comments and Discussions

 
QuestionWhat about? Pin
erm3nda31-Oct-13 16:26
erm3nda31-Oct-13 16:26 
QuestionWindows Vista Support? Pin
FrEaK_CH2-May-07 3:55
FrEaK_CH2-May-07 3:55 
AnswerRe: Windows Vista Support? Pin
Tony Selke2-May-07 3:59
Tony Selke2-May-07 3:59 
GeneralRe: Windows Vista Support? Pin
FrEaK_CH2-May-07 4:13
FrEaK_CH2-May-07 4:13 
GeneralRe: Windows Vista Support? Pin
MFRESA4-Aug-08 9:26
MFRESA4-Aug-08 9:26 
GeneralThe connect method is not implemented... Pin
juanm20025-Jul-06 7:07
juanm20025-Jul-06 7:07 
GeneralRe: The connect method is not implemented... Pin
Tony Selke10-Jul-06 3:04
Tony Selke10-Jul-06 3:04 
I have not tried doing what you are attempting, since my only need was to be able to enumerate connections and toggle the state of the Windows Firewall. I am not going to have any hands-on, practical advice for you in this case.

However, I can tell you that the Connect() method is clearly implemented and linked to the underlying COM interface. So it must be getting called (can you verify this by single-stepping through the code?). However, what is happening after the call is another matter entirely.

Perhaps it would help to take a look at the Internet Connection Sharing and Internet Connection Firewall Reference for more information. I am not sure what difficulties you may be having.

Sorry I didn't have more to help.

Tony
Generalhelp me please!!!!!! Pin
ahmed el dosoky12-May-06 6:16
ahmed el dosoky12-May-06 6:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.