How do I open ports in the Windows Firewall, part 2

This is the second post in a series of posts that explain how the Windows Media Connect project opened up a particular port through the XP SP2 firewall.

In the last post, we had figured out how to actually open the port, but I hadn’t discussed how you find the INetSharingConfiguration interface.

Well, to get an INetSharingConfiguration, you need to have an INetSharingManager.  The INetSharingManager is a top level COM object, and so you can just call CoCreateInstance on the object. 

The INetSharingManager API is used to determine if the firewall is enabled and to enumerate through “connections”.  Given a connection, it will return the sharing configuration for a specific connection using the INetSharingConfigurationForINetConnection property.  So we need to find an INetConnection. 

Well, an INetConnection represents an entry in the “Network Connections” shell folder (right click on the “My Network Places” and select properties and you’ll get “Network Connections”), so we need to enumerate through the INetConnection objects and figure out which one is associated with the IP address we want.

To enumerate over the connections, I retrieve the INetSharingManager::EnumEveryConnection property.  That returns an INetSharingEveryConnectionCollection object, which implements IEnumVARIANT.

But just having an INetConnection object doesn’t let us know what IP address it’s associated with.  To get that, we need to call the INetConnection::GetProperties method, which returns a pointer to a NETCON_PROPERTIES structure.

So we’re there, right?  Well, no.  If you look through the NETCON_PROPERTIES object, there are no IP addresses in the structure.  All you’ve got is the GUID of the connection, the name of the connection (“Local Area Connection”), the media type, status, stuff like that.  Really useful stuff for the shell, but it doesn’t help us.

But there is that “guidId” field in the structure.  It’s described as being a “Globally-unique identifier (GUID) for this connection”.  Maybe there’s a way to take advantage of that field.

And that’s tomorrow’s post.