Well I'm not sure how I missed it, but as it happens even that is more effort than really required. It turns out there are more VB-friendly interfaces available, and they work even in XP if the user has the necessary components installed. This MS KB article helps XPers get up to speed:
Vista and Win7 generally install the necessary items with no special effort on the user's part.
The NATUPnP Typelib
Well it turns out there is another typelib you can use. The result is that you can add and remove "port mappings" ("port forwardings?") and get the external IP address of your router with relative ease.
With more effort you can do things like enumerate the existing port mappings to avoid potential collisions and be notified is the external IP address changes on you during a run. These aren't needed by most programs though, so we can keep things simple here.
Stripping out the UI management logic, the attached demo is doing just a few things:
Bare Bones: Adding a Mapping & Retrieving External IP
Code:
Private Sub cmdAdd_Click()
Dim UPnPNAT As NATUPNPLib.UPnPNAT
Dim StaticPortMapping As NATUPNPLib.IStaticPortMapping
Set UPnPNAT = New NATUPNPLib.UPnPNAT
On Error Resume Next
Set StaticPortMapping = _
UPnPNAT.StaticPortMappingCollection.Add(CLng(txtExtPort.Text), _
"UDP", _
CLng(txtIntPort.Text), _
Winsock.LocalIP, _
True, _
"Demo Port Mapping")
If Err Then
MsgBox CStr(Err.Number)
Else
On Error GoTo 0
MsgBox "Success!"
ExternalIPAddress = StaticPortMapping.ExternalIPAddress
End Sub
Bare Bones: Removing a Port Mapping
Code:
Private Sub cmdRemove_Click()
Dim UPnPNAT As NATUPNPLib.UPnPNAT
Set UPnPNAT = New NATUPNPLib.UPnPNAT
On Error Resume Next
UPnPNAT.StaticPortMappingCollection.Remove CLng(txtExtPort.Text), "UDP"
If Err Then
MsgBox CStr(Err.Number)
Else
On Error GoTo 0
MsgBox "Success!"
End If
End Sub
The Demo
In the demo program attached here, you specify your external and internal port numbers. Then click Add to add the mapping, and click Ping as many times as you like to send yourself a UDP "ping" message via the external IP address and external port. Finally click Remove to remove the mapping.
Your router's logs should show these add/remove actions too.
Pretty basic.
Note
Firewall rules on the PC are another story. Yes, there is another set of interfaces for opening access to your program. Yes, they require admin rights in order to succeed - unlike port mapping.
The recommendation is that you set up the firewall rules required during installation. Port mappings should be more dynamic though, since small routers normally support a fairly limited total number of mappings. Depending on how you use the ports the user will generally get a firewall prompt on the first run anyway.
Hmm, I went back and looked at my notes on the other approach and it seems I had known about this and tried it. It looks like it was unreliable though. Perhaps I wasn't using it right before or I'm just "lucky" this time around.
Perhaps somebody else can shed some light on this. The notes I retained aren't detailed and I'm no longer sure where I got in trouble before using this approach.
When I clicked Add, it says error 'Object Variable Or with Block Variable Not set' at line Set StaticPortMapping = UPnPNAT.StaticPortMappingCollection.Add(CLng(txtExtPort.Text), _
"TCP", _
CLng(txtIntPort.Text), _
Winsock.LocalIP, _
True, _
"FAS")
I am using Windows 7 64-Bit.The projetc reference is set to c:\Windows\system32\hnetcfg.dll\2.
I login to internet using WAN Miniport PPPOE.If the Router/Gateway does not support UPnP does this Error raise?. The error displayed in your program is "Add failed, error 0000005B".
I am using Windows 7 64-Bit.The projetc reference is set to c:\Windows\system32\hnetcfg.dll\2.
I login to internet using WAN Miniport PPPOE.If the Router/Gateway does not support UPnP does this Error raise?. The error displayed in your program is "Add failed, error 0000005B".[/QUOTE]
I am using Windows 7 64-Bit.The projetc reference is set to c:\Windows\system32\hnetcfg.dll\2.
I login to internet using WAN Miniport PPPOE.If the Router/Gateway does not support UPnP does this Error raise?. The error displayed in your program is "Add failed, error 0000005B".
Error &H5B (91) is the same exception as "Object Variable Or with Block Variable Not set."
Yes, this exception is raised if UPnP is unsupported or turned off at the router. You might also see it if a firewall is blocking UPnP traffic.
Thanks for your reply.....
I have allowed the following programs in Firewall :
Network Discovery
Windows Live Communications Platform(SSDP)
Windows Live communications Platform(UPnP)
Routing and Remote Access
I have also turned On 'Network Discovery' on home and public networks.
Still the same error.
The thing is I want VB client to connect my SQL Server from outside the network.So the solution is I need to manually Port Forward on router right??
By the way how this UTorrent works on my PC?How my PC gets Incoming connections routed Via Utorrent?
Within my LAN Network VB Client connects to SQL Server, Outside it doesnt.
Any other soultion for this to work from outside my LAN Network?I dont have access(username/pwd) to my Mikrotik router.So I htought I would use your code and port forward on router.. This doesnt work...
If your router has UPnP disabled this code won't help. You'd need access to turn it on or to manually map the required ports inbound to your SQL Server instance (default is TCP 1433).
I won't comment on the morality of most uses of BitTorrent clients, but yours probably uses a "hole punching" technique not easily achieved using VB.
OK, here's the deal. My home network is set up like this: Internet->Verizon router->wireless router. When I run the program it says Add successful. Then I try Ping but nothing happens. Then I remove and it says Remove successful. But the weirdest thing is that for external IP it shows the ip of my wireless router, not my actual external ip!! What do I do? Also the firewall on my pc is disabled if anything)
Why do you have a NAT router behind a NAT router though? That's pretty... strange.
In your case you should be using a wireless access point, not a router. Can you disable the routing in this wireless device to make it work as a pass-through access point?
If so then it becomes a question of whether or not the Verison router accepts UPnP configuration. if not there isn't anything you can do.
Those home "wireless router" devices are sold at high volume so they are cheap even though they incorporate about three network devices in one: a router, a switch, and a wireless bridge/access point.
What you really need in your case is a separate wireless access point device. These aren't as popular though so they tend to cost as much as (or more than) a consumer wireless router, but they can be found for about the same price if you shop around.
Cisco, Linksys, D-Link, ZyXEL, and TrendNET are just a few of the brands that you can find out there.
I know this post is old, but it intrigues me. It actually adds the port forwarding and returns the external IP. But when I try to remove the port forward, it returns Error 80070002. There is not a whole lot of info available on this error, and the closest I can come to it is:
System.IO.FileNotFoundException
Well 0x80070002 is ERROR_FILE_NOT_FOUND. But I suspect this error code is being used in a broad, generic sense. What it probably means is that access for that action was denied by your router.
I just tried the sample program without even recompiling it, as a standard user. Works just fine and this is on a newer OS with a different router than I was using back then.
Well 0x80070002 is ERROR_FILE_NOT_FOUND. But I suspect this error code is being used in a broad, generic sense. What it probably means is that access for that action was denied by your router.
That's kinda the conclusion I came to as well, although I have no way of verifying it. I have had very little success at getting any of the other API calls to work. I was hoping to use this control to recover the external IP address, but I have given up and found another way.