IP Address Changer with error

MadMax0843

New member
Joined
Jun 26, 2015
Messages
1
Programming Experience
Beginner
Hey guys, new to vb and hoping I could get some help. This code does not cause any problems in my program in windows xp and windows 7 but will not work on windows 8. Every time I try to change the adapter in Windows 8 I get the error: "Unable to set IP: arithmetic operation resulted in overflow." Does anyone know what I am doing wrong?

VB.NET:
Dim IPAddress As String = TextBoxIPAddress.Text        Dim SubnetMask As String = TextBoxSubnetMask.Text
        Dim Gateway As String = TextBoxGateway.Text


        Dim objMC As ManagementClass = New ManagementClass("Win32_NetworkAdapterConfiguration")
        Dim objMOC As ManagementObjectCollection = objMC.GetInstances()




        If ComboBoxAdapterSelector.SelectedIndex < 0 Then
            MsgBox("Please, select an adapter.")
        Else
            Me.Cursor = Cursors.WaitCursor
            For Each objMO As ManagementObject In objMOC ' iterate through adapters
                If (Not CBool(objMO("IPEnabled"))) Then
                    Continue For
                End If


                Dim nResult As Integer


                Dim strAdapaterName As String = objMO("Description").ToString
                If strAdapaterName = ComboBoxAdapterSelector.Text Then ' found adapter we selected
                    Try
                        Dim objNewIP As ManagementBaseObject = Nothing
                        Dim objSetIP As ManagementBaseObject = Nothing
                        Dim objNewGate As ManagementBaseObject = Nothing
                        Dim objSetGate As ManagementBaseObject = Nothing


                        objNewIP = objMO.GetMethodParameters("EnableStatic")
                        'Set IPAddress and Subnet Mask
                        objNewIP("IPAddress") = New String() {IPAddress}
                        objNewIP("SubnetMask") = New String() {SubnetMask}
                        objSetIP = objMO.InvokeMethod("EnableStatic", objNewIP, Nothing)
                        nResult = objSetIP("returnValue")
                        If nResult <> 0 Then
                            MessageBox.Show(ErrorMessageEnableStatic(nResult))
                        End If




                        objNewGate = objMO.GetMethodParameters("SetGateways")
                        'Set DefaultGateway
                        objNewGate("DefaultIPGateway") = New String() {Gateway}
                        objNewGate("GatewayCostMetric") = New Integer() {1}
                        objSetGate = objMO.InvokeMethod("SetGateways", objNewGate, Nothing)
                        nResult = objSetGate("returnValue")
                        If nResult <> 0 Then
                            MessageBox.Show(ErrorMessageEnableStatic(nResult))
                        End If


                        'objSetIP = objMO.InvokeMethod("SetGateways", objNewGate, Nothing)
                        'nResult = objSetIP("returnValue")


                        'If nResult <> 0 Then
                        ' MessageBox.Show(ErrorMessageSetGateWays(nResult))
                        'End If
                        Console.WriteLine("Updated IPAddress, SubnetMask and Default Gateway!")


                    Catch ex As Exception
                        MessageBox.Show("Unable to Set IP : " & ex.Message)
                    End Try


                    Exit For
                End If
            Next objMO


        End If
 

elroyskimms

Member
Joined
Jul 10, 2015
Messages
19
Programming Experience
10+
It would be helpful if you could also post the stacktrace from the exception. We need to know which line of code is triggering the overflow in order to isolate which variable/property is causing the issue.

Try changing:
MessageBox.Show("Unable to Set IP : " & ex.Message)

Into this:
MessageBox.Show("Unable to Set IP : " & ex.Message & vbcrlf & ex.stacktrace)

That should include the stacktrace in the MessageBox text which will also include the line number of the code that triggers the overflow. Paste the results and we can try and help.

-E
 
Top Bottom