Detect the remote Mac Address

Posted on 2005-04-05
Last Modified: 2008-02-01

I search a sample vb6 code that detect a remote mac address from an remote IP adress.What can I do ?

Question by:Marco_Panza
    LVL 35

    Accepted Solution

    Try looking here:

    To a form add a command button (Command1), and two text boxes (Text1, Text2). Labels are optional. Add the following code, and be sure to set Text1 in Form_Load to point to a valid IP address (local or remote machine) workgroup network or domain:

    Option Explicit
    ' Copyright ©1996-2005 VBnet, Randy Birch, All Rights Reserved.
    ' Some pages may also contain other copyrights by the author.
    ' Distribution: You can freely use this code in your own
    '               applications, but you may not reproduce
    '               or publish this code on any web site,
    '               online service, or distribute as source
    '               on any media without express permission.
    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 = "" 'or address of interest
       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
             Text2.Text = "(SendARP call failed)"
          End If
       End If

    End Sub

    Private Function GetRemoteMACAddress(ByVal sRemoteIP As String, _
                                         sRemoteMacAddress As String, _
                                         sDelimiter As String) As Boolean

       Dim dwRemoteIP As Long
       Dim pMacAddr As Long
       Dim bpMacAddr() As Byte
       Dim PhyAddrLen As Long
      'convert the string IP into
      'an unsigned long value containing
      'a suitable binary representation
      'of the Internet address given
       dwRemoteIP = ConvertIPtoLong(sRemoteIP)

       If dwRemoteIP <> 0 Then
         'must set this up first!
          PhyAddrLen = 6
        'assume failure
         GetRemoteMACAddress = False
         '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
               'convert the byte array to a string
               'and return success
                sRemoteMacAddress = MakeMacAddress(bpMacAddr(), sDelimiter)
                GetRemoteMACAddress = True
             End If 'pMacAddr

          End If  'SendARP
       End If  'dwRemoteIP
    End Function

    Private Function ConvertIPtoLong(sIpAddress) As Long

       ConvertIPtoLong = inet_addr(sIpAddress)

    End Function

    Private Function MakeMacAddress(b() As Byte, sDelim As String) As String

       Dim cnt As Long
       Dim buff As String
       On Local Error GoTo MakeMac_error
      'so far, MAC addresses are
      'exactly 6 segments in size (0-5)
       If UBound(b) = 5 Then
         'concatenate the first five values
         'together and separate with the
         'delimiter char
          For cnt = 0 To 4
             buff = buff & Right$("00" & Hex(b(cnt)), 2) & sDelim
         'and append the last value
          buff = buff & Right$("00" & Hex(b(5)), 2)
       End If  'UBound(b)
       MakeMacAddress = buff
       Exit Function
       MakeMacAddress = "(error building MAC address)"
       Resume MakeMac_exit
    End Function



    Author Comment

    Very Good ...

    LVL 35

    Expert Comment

    by:[ fanpages ]
    You're very welcome.

    Thanks for the points/grading.


    [ ]

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now