Private Function GetMACAddress(ByVal Adapter As String) As String
Dim mc As System.Management.ManagementClass
Dim mo As Management.ManagementObject
mc = New Management.ManagementClass("Win32_NetworkAdapterConfiguration")
Dim moc As Management.ManagementObjectCollection = mc.GetInstances()
For Each mo In moc
If mo.Item("IPEnabled") = True Then
Dim strAdapter As String
strAdapter = mo.Item("Caption").ToString().Substring(11)
If strAdapter = Adapter Then
Return mo.Item("MacAddress").ToString()
End If
End If
Next
End Function
[/code
The End function says in green that it can't return value on All code paths.
This is the WHOLE code ot the program.
[Code]
Option Explicit On
Imports System.Management
Imports System.Net
Imports System.Net.NetworkInformation
Public Class Form1
Dim bool As Boolean = True
Private Function GetMACAddress(ByVal Adapter As String) As String
Dim mc As System.Management.ManagementClass
Dim mo As Management.ManagementObject
mc = New Management.ManagementClass("Win32_NetworkAdapterConfiguration")
Dim moc As Management.ManagementObjectCollection = mc.GetInstances()
For Each mo In moc
If mo.Item("IPEnabled") = True Then
Dim strAdapter As String
strAdapter = mo.Item("Caption").ToString().Substring(11)
If strAdapter = Adapter Then
Return mo.Item("MacAddress").ToString()
End If
End If
Next
End Function
Public Sub gateway1()
Dim gatewayip As String
Dim l As Integer = 0
Dim arr(l) As String
Dim myNetworkAdapters() As NetworkInterface = NetworkInterface.GetAllNetworkInterfaces
Dim myAdapterProps As IPInterfaceProperties = Nothing
Dim myGateways As GatewayIPAddressInformationCollection = Nothing
For Each adapter As NetworkInterface In myNetworkAdapters
myAdapterProps = adapter.GetIPProperties
myGateways = myAdapterProps.GatewayAddresses
For Each Gateway As GatewayIPAddressInformation In myGateways
gatewayip = Gateway.Address.ToString
arr(l) = gatewayip
TextBox6.Text = Gateway.Address.ToString
'l += 1
Next
Next
' MsgBox(arr(0))
WebBrowser2.Navigate(TextBox6.Text)
End Sub
Private Sub FillNetworkAdapters()
Dim mc As System.Management.ManagementClass
Dim mo As Management.ManagementObject
mc = New Management.ManagementClass("Win32_NetworkAdapterConfiguration")
Dim moc As Management.ManagementObjectCollection = mc.GetInstances()
For Each mo In moc
If mo.Item("IPEnabled") = True Then
Dim strAdapter As String
strAdapter = mo.Item("Caption").ToString().Substring(11)
combo_network.Items.Add(strAdapter)
End If
Next
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer7.Enabled = True
FillNetworkAdapters()
gateway1()
WebBrowser3.Visible = False
WebBrowser4.Visible = False
WebBrowser3.Navigate("http://www.ipligence.com/geolocation")
WebBrowser4.Navigate("http://www.batchgeocode.com/lookup/")
Timer3.Enabled = True
Timer5.Enabled = True
TextBox1.Text = System.Net.Dns.GetHostName
Me.Text = "Wait..."
WebBrowser1.Navigate("http://www.whatismyip.com/tools/ip-address-lookup.asp")
bool = False
TextBox3.Text = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName).AddressList(0).ToString()
Dim mac As String
Dim l As Integer = 0
Dim br(l) As String
For Each nic As System.Net.NetworkInformation.NetworkInterface In System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
'MessageBox.Show(String.Format("The MAC address of {0} is{1}{2}", nic.Description, Environment.NewLine, nic.GetPhysicalAddress()))
mac = String.Format("{2}", nic.Description, Environment.NewLine, nic.GetPhysicalAddress()).ToString
Try
br(l) = mac
Catch ex As Exception
End Try
l += 1
Next
TextBox4.Text = br(0)
mac_text.Text = br(0)
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If WebBrowser1.IsBusy = False Then
adresa()
Timer1.Enabled = False
End If
End Sub
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
If WebBrowser1.IsBusy = False And bool = False Then
Timer1.Enabled = True
bool = True
End If
End Sub
Public Sub adresa()
Try
TextBox2.Text = WebBrowser1.Document.All("IP").GetAttribute("value")
Me.Text = "IPChanger"
Catch ex As Exception
MsgBox("You are not connected to internet!")
Me.Text = "IPChanger"
End Try
End Sub
Private Sub Info_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Function DoPadding(ByVal x As String) As String
Dim Ret As String
Dim z As Integer
Ret = x
If Len(x) < 4 Then
For z = 1 To 4 - Len(x)
Ret = "0" & Ret
Next
End If
Return Ret
End Function
Private Sub ShowRestart()
Dim res As MsgBoxResult = MsgBox("Your MAC address is changed. You need to restart your computer or turn off and turn on your router.", MsgBoxStyle.Information)
End Sub
Private Sub ChangeMac_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChaneMac.Click
If IsOkay() = False Then
Exit Sub
End If
Try
Catch ex As Exception
MsgBox("Error! Try again.")
Dim regKey As Microsoft.Win32.RegistryKey
Dim Addr As String = GetRoot(combo_network.SelectedItem.ToString)
regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(Addr, True)
regKey.SetValue("NetworkAddress", mac_text.Text.Replace(":", ""))
ShowRestart()
End Try
End Sub
Private Function GetRoot(ByVal Adapter As String) As String
Dim regKey As Microsoft.Win32.RegistryKey
Dim i As Integer = 1
Do
Dim Root As String = "SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}"
Dim Last As String = DoPadding(i)
regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(Root & Last, True)
Try
Dim cAdapter As String = regKey.GetValue("DriverDesc").ToString()
If cAdapter = Adapter Then
Return Root & Last
Exit Do
End If
Catch ex1 As Exception
MsgBox("Error! Try again.")
End Try
i += 1
Loop
End Function
Private Function IsOkay() As Boolean
If mac_text.Text = "" Then
MsgBox("You have not enterd new MAC address!", MsgBoxStyle.Critical)
Return False
End If
Dim ed As String = mac_text.Text.Replace(":", "")
If ed.Length <> 12 Then
MsgBox("MAC address have to be in 12 characters!", MsgBoxStyle.Critical)
Return False
End If
Try
If combo_network.SelectedItem.ToString = "" Then
MsgBox("You have not select a router!", MsgBoxStyle.Critical)
Return False
End If
Catch
MsgBox("You have not select a router!", MsgBoxStyle.Critical)
Return False
End Try
Dim noerror As Boolean = True
Dim i As Integer
For i = 0 To ed.Length - 1
If IsHex(ed.Substring(i, 1)) = False Then
MsgBox("MAC address have to be hexadecimal! (0 - 9 & A - F)", MsgBoxStyle.Critical)
Return False
End If
Next
Return True
End Function
Private Function IsHex(ByVal l As String) As Boolean
Dim table As String = "0123456789ABCDEF"
Dim i As Integer
For i = 0 To table.Length - 1
If l = table.Substring(i, 1) Then
Return True
End If
Next
Return False
End Function
Private Sub Default1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Default1.Click
If IsOkay() = False Then
Exit Sub
End If
Dim regKey As Microsoft.Win32.RegistryKey
Dim Addr As String = GetRoot(combo_network.SelectedItem.ToString())
regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(Addr, True)
Try
regKey.DeleteValue("NetworkAddress")
Catch
'Do NOTHING
End Try
ShowRestart()
End Sub
Private Sub combo_network_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles combo_network.SelectedIndexChanged
End Sub
Private Sub CompNAME_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CompName.Click
If TextBox5.Text <> "" Then
Try
Dim sNewName = TextBox5.Text
Dim oShell = CreateObject("WSCript.shell")
Dim sCCS = "HKLM\SYSTEM\CurrentControlSet"
Dim sTcpipParamsRegPath = sCCS & "Services\Tcpip\Parameters"
Dim sCompNameRegPath = sCCS & "Control\ComputerName"
With oShell
.RegDelete(sTcpipParamsRegPath & "Hostname")
.RegDelete(sTcpipParamsRegPath & "NV Hostname")
.RegWrite(sCompNameRegPath & "ComputerName\ComputerName", sNewName)
.RegWrite(sCompNameRegPath & "ActiveComputerName\ComputerName", sNewName)
.RegWrite(sTcpipParamsRegPath & "Hostname", sNewName)
.RegWrite(sTcpipParamsRegPath & "NV Hostname", sNewName)
End With ' oShell
MsgBox("Computer name is changed!")
Catch ex As Exception
MsgBox("Error! Computer name is not changed.")
End Try
Else
MsgBox("Write new computer name!")
End If
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
Try
If WebBrowser2.IsBusy = False Then
WebBrowser2.Document.All("12").InvokeMember("Click") '"12" . these are names of button on my router root web site. this wont work for you so you have to set this to be like it is on your router root web site.
Timer2.Enabled = False
MsgBox("Your IP address is changed.")
Me.Text = "IPChanger"
End If
Catch ex As Exception
Timer2.Enabled = False
MsgBox("Error! Try again.")
Me.Text = "IPChanger"
End Try
End Sub
Private Sub ChangeIP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChangeIP.Click
Try
If WebBrowser2.IsBusy = False Then
Me.Text = "Wait..."
WebBrowser2.Document.All("13").InvokeMember("Click") '"13" . these are names of button on my router root web site. this wont work for you so you have to set this to be like it is on your router root web site.
Timer2.Enabled = True
Else
MsgBox("Wait until web site is ready!")
End If
Catch ex As Exception
MsgBox("Error! Try again.")
End Try
End Sub
Private Sub Timer7_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer7.Tick
Timer7.Stop()
End Sub
Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged
TextBox4.CharacterCasing = CharacterCasing.Upper
End Sub
Private Sub mac_text_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mac_text.TextChanged
mac_text.CharacterCasing = CharacterCasing.Upper
End Sub
Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
TextBox3.CharacterCasing = CharacterCasing.Upper
End Sub
End Class
[/Code]
I know there is some things enabled that are not suppose to be which don't make a difference but I didn't feel like going through the work of removing them just to make this thread. Also I still haven't used some things because im not done with it yet.
I have all the references correct for my codes which is how I used them and saw the suggestions and such. It's suppose to be a MAc and IP address changer. Although the get mac code's end function does not work?
____________________
|
|