Imports System.IO
Imports System.Net
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.
New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected
Overloads Overrides Sub Dispose(
ByVal disposing
As Boolean)
If disposing
Then
If Not (components
Is Nothing)
Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components
As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1
As System.Windows.Forms.Button
Friend WithEvents ListBox1
As System.Windows.Forms.ListBox
Friend WithEvents TextBox1
As System.Windows.Forms.TextBox
Friend WithEvents TextBox2
As System.Windows.Forms.TextBox
Friend WithEvents Button2
As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()>
Private Sub InitializeComponent()
Me.Button1 =
New System.Windows.Forms.Button()
Me.ListBox1 =
New System.Windows.Forms.ListBox()
Me.TextBox1 =
New System.Windows.Forms.TextBox()
Me.TextBox2 =
New System.Windows.Forms.TextBox()
Me.Button2 =
New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location =
New System.Drawing.Point(8, 8)
Me.Button1.
Name = "Button1"
Me.Button1.Size =
New System.Drawing.Size(128, 24)
Me.Button1.TabIndex = 0
Me.Button1.Text = "get Mac Address"
'
'ListBox1
'
Me.ListBox1.Location =
New System.Drawing.Point(8, 40)
Me.ListBox1.
Name = "ListBox1"
Me.ListBox1.Size =
New System.Drawing.Size(376, 108)
Me.ListBox1.TabIndex = 1
'
'TextBox1
'
Me.TextBox1.Location =
New System.Drawing.Point(8, 152)
Me.TextBox1.Multiline =
True
Me.TextBox1.
Name = "TextBox1"
Me.TextBox1.Size =
New System.Drawing.Size(376, 112)
Me.TextBox1.TabIndex = 2
Me.TextBox1.Text = "TextBox1"
'
'TextBox2
'
Me.TextBox2.Location =
New System.Drawing.Point(256, 8)
Me.TextBox2.
Name = "TextBox2"
Me.TextBox2.Size =
New System.Drawing.Size(128, 20)
Me.TextBox2.TabIndex = 3
Me.TextBox2.Text = "ipaddress here"
'
'Button2
'
Me.Button2.Location =
New System.Drawing.Point(152, 8)
Me.Button2.
Name = "Button2"
Me.Button2.Size =
New System.Drawing.Size(96, 24)
Me.Button2.TabIndex = 4
Me.Button2.Text = "get Host
Name"
'
'Form1
'
Me.AutoScaleBaseSize =
New System.Drawing.Size(5, 13)
Me.ClientSize =
New System.Drawing.Size(394, 273)
Me.Controls.AddRange(
New System.Windows.Forms.Control() {
Me.Button2,
Me.TextBox2,
Me.TextBox1,
Me.ListBox1,
Me.Button1})
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
Me.
Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(
False)
End Sub
#
End Region
Private Sub Button1_Click(
ByVal sender
As System.
Object,
ByVal e
As System.EventArgs)
Handles Button1.Click
'microsoft.VisualBasic.Shell(
' MessageBox.Show("Hi"
' EnumNetworkAdapters()
' MsgBox(GetMacAddress())
' IP = New IPEndPoint
TextBox2.Text = HosttoIP(TextBox1.Text.Trim)
TextBox1.Text = GetMac(TextBox2.Text)
End Sub
Private Function HostToIP(
ByVal HostStr
As String)
As String
Try
Dim dns
As System.Net.Dns
Dim Host
As New System.Net.IPHostEntry()
Host = dns.GetHostByName(hoststr)
Dim ToStr
As IPAddress
ToStr = Host.AddressList.GetValue(0)
HostToIP = ToStr.ToString
Catch err
As Exception
MsgBox(err.Message)
End Try
End Function
Private Function IPtoHost(
ByVal IPstr
As String)
As String
Try
Dim dns
As System.Net.Dns
Dim Host
As New System.Net.IPHostEntry()
Host = dns.GetHostByAddress(IPstr)
IPtoHost = Host.HostName
Catch err
As Exception
MsgBox(err.Message)
End Try
End Function
' Private IP As IPEndPoint()
Public Function ValueSepR(
ByVal RawString
As String,
ByVal SepChar
As String)
As String
If InStr(1, RawString, SepChar) <> 0
Then
ValueSepR = Microsoft.VisualBasic.Right(RawString,
Len(RawString) - InStr(1, RawString, SepChar))
Else
ValueSepR = RawString
End If
End Function
Public Function GetMac(
ByVal IP
As String)
As String
Try
Dim dirResults
As String
Dim psi
As ProcessStartInfo =
New ProcessStartInfo()
Dim proc
As New Process()
psi.FileName = "nbtstat"
psi.RedirectStandardInput =
False
psi.RedirectStandardOutput =
True
psi.Arguments = "-A " & IP
psi.UseShellExecute =
False
proc = Process.Start(psi)
Dim x
As Integer = -1
Do Until x > -1
If dirResults <>
Nothing Then
x = dirResults.Trim.ToLower.IndexOf("mac address", 0)
If x > -1
Then
Exit Do
End If
End If
dirResults = proc.StandardOutput.ReadLine
Loop
proc.WaitForExit()
GetMac = ValueSepR(dirResults.Trim, "="
.Trim
Catch err
As Exception
MsgBox(err.Message & err.StackTrace)
End Try
End Function
Public Sub EnumNetworkAdapters()
Try
Dim query
As System.Management.ManagementObjectSearcher =
New System.Management.ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration"
Dim queryCollection
As System.Management.ManagementObjectCollection = query.
Get()
Dim mo
As New System.Management.ManagementObject()
For Each mo
In queryCollection
If IsDBNull(mo.Item("
escription"
) =
False Then
ListBox1.Items.Add(mo.Item("
escription"
)
End If
If IsDBNull(mo.Item("macaddress"
) =
False Then
If mo.Item("macaddress"
<> ""
Then
ListBox1.Items.Add(mo.Item("macaddress"
)
End If
End If
If IsDBNull(mo.Item("IPAddress"
) =
False Then
Dim temp
As String
temp = Join(mo.Item("IPAddress"
, "."
If temp <> ""
Then
ListBox1.Items.Add(temp)
End If
End If
Next
Catch err
As Exception
MsgBox(err.Message & err.StackTrace)
End Try
End Sub
Public Function GetMacAddress(
Optional ByVal IP
As String =
Nothing)
As String
Try
Dim query
As System.Management.ManagementObjectSearcher =
New System.Management.ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration"
Dim queryCollection
As System.Management.ManagementObjectCollection = query.
Get()
Dim mo
As New System.Management.ManagementObject()
Dim Found
As Boolean =
False
For Each mo
In queryCollection
If IsDBNull(mo.Item("IPAddress"
) =
False Then
Dim temp
As String
temp = Join(mo.Item("IPAddress"
, "."
If temp <> ""
Then
If IP <> ""
Then
If temp = IP.Trim
Then
Found =
True
End If
Else
Found =
True
End If
End If
End If
If Found =
True Then
If IsDBNull(mo.Item("macaddress"
) =
False Then
If mo.Item("macaddress"
<> ""
Then
GetMacAddress = mo.Item("macaddress"
Exit Function
End If
End If
Else
Found =
False
End If
Next
If Found =
False Then
MsgBox("No Mac Address Found"
GetMacAddress = ""
End If
Catch err
As Exception
MsgBox(err.Message & err.StackTrace)
End Try
End Function
Private Sub Button2_Click(
ByVal sender
As System.
Object,
ByVal e
As System.EventArgs)
Handles Button2.Click
TextBox1.Text = IPtoHost(TextBox2.Text)
End Sub
End Class