Hi, I found this program that would get the mac address of a computer given its IP. I converted it into vb.net and it gave 2 errors. It said that I couldn't use "any" in declarations, so I found out what variable it was tryign to pass and changed the any to that data type. Now, the program will start in vb.net... but when I click the button, it returns the wrong mac address, while the one in vb6 returned the correct one.
All you have to do is add 2 textboxes to the form and 1 button, dont change their names.
::::::::::VB6 CODE::::::::::
Option Explicit
Private Const NO_ERROR = 0
Private Declare Function inet_addr Lib "wsock32.dll" _
(ByVal s As String) As Long
Private Declare Function SendARP Lib "iphlpapi.dll" _
(ByVal DestIP As Long, ByVal SrcIP As Long, pMacAddr As Long, PhyAddrLen As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dst As Any, src As Any, ByVal bcount As Long)
Private Sub Form_Load()
Text1.Text = "192.168.1.101"
Text2.Text = ""
Command1.Caption = "Get Remote Mac Address"
End Sub
Private Sub Command1_Click()
Dim sRemoteMacAddress As String
If Len(Text1.Text) > 0 Then
If GetRemoteMACAddress(Text1.Text, sRemoteMacAddress) Then
Text2.Text = sRemoteMacAddress
Else
Text2.Text = "(SendARP call failed)"
End If
End If
End Sub
Private Function GetRemoteMACAddress(ByVal sRemoteIP As String, _
sRemoteMacAddress As String) As Boolean
Dim dwRemoteIP As Long
Dim pMacAddr As Long
Dim bpMacAddr() As Byte
Dim PhyAddrLen As Long
Dim cnt As Long
Dim tmp As String
'convert the string IP into
'an unsigned long value containing
'a suitable binary representation
'of the Internet address given
dwRemoteIP = inet_addr(sRemoteIP)
If dwRemoteIP <> 0 Then
'set PhyAddrLen to 6
PhyAddrLen = 6
'retrieve the remote MAC address
If SendARP(dwRemoteIP, 0&, pMacAddr, PhyAddrLen) = NO_ERROR Then
If pMacAddr <> 0 And PhyAddrLen <> 0 Then
'returned value is a long pointer
'to the mac address, so copy data
'to a byte array
ReDim bpMacAddr(0 To PhyAddrLen - 1)
CopyMemory bpMacAddr(0), pMacAddr, ByVal PhyAddrLen
'loop through array to build string
For cnt = 0 To PhyAddrLen - 1
If bpMacAddr(cnt) = 0 Then
tmp = tmp & "00-"
Else
tmp = tmp & Hex$(bpMacAddr(cnt)) & "-"
End If
Next
'remove the trailing dash
'added above and return True
If Len(tmp) > 0 Then
sRemoteMacAddress = Left$(tmp, Len(tmp) - 1)
GetRemoteMACAddress = True
End If
Exit Function
Else
GetRemoteMACAddress = False
End If
Else
GetRemoteMACAddress = False
End If 'SendARP
Else
GetRemoteMACAddress = False
End If 'dwRemoteIP
End Function
::::::::::VB.NET Code::::::::::
Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class Form1
Inherits System.Windows.Forms.Form
#Region "Windows Form Designer generated code "
Public Sub New()
MyBase.New()
If m_vb6FormDefInstance Is Nothing Then
If m_InitializingDefInstance Then
m_vb6FormDefInstance = Me
Else
Try
'For the start-up form, the first instance created is the default instance.
If System.Reflection.Assembly.GetExecutingAssembly.EntryPoint.DeclaringType Is Me.GetType Then
m_vb6FormDefInstance = Me
End If
Catch
End Try
End If
End If
'This call is required by the Windows Form Designer.
InitializeComponent()
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
Public ToolTip1 As System.Windows.Forms.ToolTip
Public WithEvents Command1 As System.Windows.Forms.Button
Public WithEvents Text2 As System.Windows.Forms.TextBox
Public WithEvents Text1 As System.Windows.Forms.TextBox
'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.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
Me.components = New System.ComponentModel.Container()
Me.ToolTip1 = New System.Windows.Forms.ToolTip(components)
Me.ToolTip1.Active = True
Me.Command1 = New System.Windows.Forms.Button
Me.Text2 = New System.Windows.Forms.TextBox
Me.Text1 = New System.Windows.Forms.TextBox
Me.Text = "Form1"
Me.ClientSize = New System.Drawing.Size(312, 206)
Me.Location = New System.Drawing.Point(4, 30)
Me.StartPosition = System.Windows.Forms.FormStartPosition.WindowsDefaultLocation
Me.Font = New System.Drawing.Font("Arial", 8!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.BackColor = System.Drawing.SystemColors.Control
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable
Me.ControlBox = True
Me.Enabled = True
Me.KeyPreview = False
Me.MaximizeBox = True
Me.MinimizeBox = True
Me.Cursor = System.Windows.Forms.Cursors.Default
Me.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.ShowInTaskbar = True
Me.HelpButton = False
Me.WindowState = System.Windows.Forms.FormWindowState.Normal
Me.Name = "Form1"
Me.Command1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
Me.Command1.Text = "Command1"
Me.Command1.Size = New System.Drawing.Size(129, 57)
Me.Command1.Location = New System.Drawing.Point(144, 128)
Me.Command1.TabIndex = 2
Me.Command1.Font = New System.Drawing.Font("Arial", 8!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Command1.BackColor = System.Drawing.SystemColors.Control
Me.Command1.CausesValidation = True
Me.Command1.Enabled = True
Me.Command1.ForeColor = System.Drawing.SystemColors.ControlText
Me.Command1.Cursor = System.Windows.Forms.Cursors.Default
Me.Command1.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.Command1.TabStop = True
Me.Command1.Name = "Command1"
Me.Text2.AutoSize = False
Me.Text2.Size = New System.Drawing.Size(225, 49)
Me.Text2.Location = New System.Drawing.Point(40, 56)
Me.Text2.TabIndex = 1
Me.Text2.Text = "Text2"
Me.Text2.Font = New System.Drawing.Font("Arial", 8!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Text2.AcceptsReturn = True
Me.Text2.TextAlign = System.Windows.Forms.HorizontalAlignment.Left
Me.Text2.BackColor = System.Drawing.SystemColors.Window
Me.Text2.CausesValidation = True
Me.Text2.Enabled = True
Me.Text2.ForeColor = System.Drawing.SystemColors.WindowText
Me.Text2.HideSelection = True
Me.Text2.ReadOnly = False
Me.Text2.Maxlength = 0
Me.Text2.Cursor = System.Windows.Forms.Cursors.IBeam
Me.Text2.MultiLine = False
Me.Text2.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.Text2.ScrollBars = System.Windows.Forms.ScrollBars.None
Me.Text2.TabStop = True
Me.Text2.Visible = True
Me.Text2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.Text2.Name = "Text2"
Me.Text1.AutoSize = False
Me.Text1.Size = New System.Drawing.Size(225, 41)
Me.Text1.Location = New System.Drawing.Point(40, 8)
Me.Text1.TabIndex = 0
Me.Text1.Text = "Text1"
Me.Text1.Font = New System.Drawing.Font("Arial", 8!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Text1.AcceptsReturn = True
Me.Text1.TextAlign = System.Windows.Forms.HorizontalAlignment.Left
Me.Text1.BackColor = System.Drawing.SystemColors.Window
Me.Text1.CausesValidation = True
Me.Text1.Enabled = True
Me.Text1.ForeColor = System.Drawing.SystemColors.WindowText
Me.Text1.HideSelection = True
Me.Text1.ReadOnly = False
Me.Text1.Maxlength = 0
Me.Text1.Cursor = System.Windows.Forms.Cursors.IBeam
Me.Text1.MultiLine = False
Me.Text1.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.Text1.ScrollBars = System.Windows.Forms.ScrollBars.None
Me.Text1.TabStop = True
Me.Text1.Visible = True
Me.Text1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.Text1.Name = "Text1"
Me.Controls.Add(Command1)
Me.Controls.Add(Text2)
Me.Controls.Add(Text1)
End Sub
#End Region
#Region "Upgrade Support "
Private Shared m_vb6FormDefInstance As Form1
Private Shared m_InitializingDefInstance As Boolean
Public Shared Property DefInstance() As Form1
Get
If m_vb6FormDefInstance Is Nothing OrElse m_vb6FormDefInstance.IsDisposed Then
m_InitializingDefInstance = True
m_vb6FormDefInstance = New Form1()
m_InitializingDefInstance = False
End If
DefInstance = m_vb6FormDefInstance
End Get
Set
m_vb6FormDefInstance = Value
End Set
End Property
#End Region
Private Const NO_ERROR As Short = 0
Private Declare Function inet_addr Lib "wsock32.dll" (ByVal s As String) As Integer
Private Declare Function SendARP Lib "iphlpapi.dll" (ByVal DestIP As Integer, ByVal SrcIP As Integer, ByRef pMacAddr As Integer, ByRef PhyAddrLen As Integer) As Integer
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef dst As Byte, ByRef src As Integer, ByVal bcount As Integer)
Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
Text1.Text = "192.168.0.103"
Text2.Text = ""
Command1.Text = "Get Remote Mac Address"
End Sub
Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click
Dim sRemoteMacAddress As String
If Len(Text1.Text) > 0 Then
If GetRemoteMACAddress(Text1.Text, sRemoteMacAddress) Then
Text2.Text = sRemoteMacAddress
Else
Text2.Text = "(SendARP call failed)"
End If
End If
End Sub
Private Function GetRemoteMACAddress(ByVal sRemoteIP As String, ByRef sRemoteMacAddress As String) As Boolean
Dim dwRemoteIP As Integer
Dim pMacAddr As Integer
Dim bpMacAddr() As Byte
Dim PhyAddrLen As Integer
Dim cnt As Integer
Dim tmp As String
'convert the string IP into
'an unsigned long value containing
'a suitable binary representation
'of the Internet address given
dwRemoteIP = inet_addr(sRemoteIP)
If dwRemoteIP <> 0 Then
'set PhyAddrLen to 6
PhyAddrLen = 6
'retrieve the remote MAC address
If SendARP(dwRemoteIP, 0, pMacAddr, PhyAddrLen) = NO_ERROR Then
If pMacAddr <> 0 And PhyAddrLen <> 0 Then
'returned value is a long pointer
'to the mac address, so copy data
'to a byte array
ReDim bpMacAddr(PhyAddrLen - 1)
CopyMemory(bpMacAddr(0), pMacAddr, PhyAddrLen)
'loop through array to build string
For cnt = 0 To PhyAddrLen - 1
If bpMacAddr(cnt) = 0 Then
tmp = tmp & "00-"
Else
tmp = tmp & Hex(bpMacAddr(cnt)) & "-"
End If
Next
'remove the trailing dash
'added above and return True
If Len(tmp) > 0 Then
sRemoteMacAddress = VB.Left(tmp, Len(tmp) - 1)
GetRemoteMACAddress = True
End If
Exit Function
Else
GetRemoteMACAddress = False
End If
Else
GetRemoteMACAddress = False
End If 'SendARP
Else
GetRemoteMACAddress = False
End If 'dwRemoteIP
End Function
End Class
When I copied the above code, I copeid everything, so if you want to test it, you have to delete all code from the form, including the code that the compiler puts in for you. Sorry, I did not notice this when I copied it.
![]() |
0 |
![]() |
What MAC address are you trying to retrieve? And what version of the .NET Framework? Might check:
http://www.15seconds.com/issue/051215.htm
Jeff
Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.
![]() |
0 |
![]() |
Hi, I am trying to get the computers MAC address. This would be the adapter's address. I need this inorder to use Wake On Lan so I can start up remote computers on the network.
![]() |
0 |
![]() |
The question is whether you wated to get the local system MAC or a remote MAC. You may need permissions for the remote MAC. Did you look at the link?
Jeff
Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.
![]() |
0 |
![]() |
Yes, I need the remote MAC and I am network administrator. I have looked at the link, and System.Net.NyetworkInformation.PhysicalAddress seems to be what I need. This only takes in byte information, so I would have to convert the IP into byte first. Thank you.
![]() |
0 |
![]() |
did you already find the problem? you have only integer datatypes but there should be some long. the mac address is 48 bit but integer is only 32. So when ever you want a variable to contain a macaddress it should be long.
Thomas
![]() |
0 |
![]() |