I am trying to create a program that will map ports to my uPnP router as my ports keep getting reset ever so often and I can't connect to my house. I create (more like copied) the following code, and I always get an error when clicking the button.
Code:
Imports NATUPNPLib
Public Class Form1
Dim entry As System.Net.IPHostEntry = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName)
Dim upnp As New NATUPNPLib.UPnPNAT
Dim portmaps As NATUPNPLib.IStaticPortMappingCollection = upnp.StaticPortMappingCollection
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
portmaps.Add("5901", "TCP", "5901", "10.175.3.22", True, "VNC")
End Sub
End Class
And the error:
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.Runtime.InteropServices.COMException (0x80040208): A user-supplied component or subscriber raised an exception (Exception from HRESULT: 0x80040208)
at NATUPNPLib.IStaticPortMappingCollection.Add(Int32 lExternalPort, String bstrProtocol, Int32 lInternalPort, String bstrInternalClient, Boolean bEnabled, String bstrDescription)
at WindowsApplication1.Form1.Button1_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
As I am not the most skilled in .NET, does anyone have any suggestions?
Looks to me like you're just adding the same port/ip every time the user clicks the button. That might be causing an exception if the user clicks the button more than once.
Perhaps you would want see if port 5901 is already in the portmaps before you call portmaps.Add(...).
User contributions licensed under CC BY-SA 3.0