Solved

Get Client  Mac Address

Posted on 2003-12-09
11
5,909 Views
Last Modified: 2012-08-13
Hi

My problem is that I need to get the NIC mac address off of the client's laptop for successful registration across our network.

I am using .NET to write the application, but assume that I need to host the script client-side or else I invariably get the mac address off of my server!

Is there a way to do this using Javascript?  If not, how could I detect this info using server-side scripting?

Thanks,

Tom

0
Comment
Question by:trman
11 Comments
 
LVL 17

Accepted Solution

by:
Tacobell777 earned 250 total points
ID: 9909482
Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2003 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 = "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
'--end block--'
0
 
LVL 10

Expert Comment

by:Nushi
ID: 9910875
why not using a server side code to do it?
the client is connected to your side so simply run a few lines of code
(Java,PHP etc).

Nushi.
0
 

Author Comment

by:trman
ID: 9932870
Thx for your posts.

Tacobell, will this code return the remote user's NIC mac address or does it resolve the ip address only.

Nushi, what vb.net code do you suggest?

Tom
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 17

Expert Comment

by:Tacobell777
ID: 9932889
it should return the remote mac address

There are numerous ways of doing it, what server-side language are you using?
0
 

Author Comment

by:trman
ID: 9933023
HI Tacobell,

I am using vb.net
0
 
LVL 17

Expert Comment

by:Tacobell777
ID: 9933068
then the above code should be familiar to you, correct?
0
 

Author Comment

by:trman
ID: 9933083
Yes, it is.  I just didn't see a reference to the 'Win32_NetworkAdapterConfiguration' Management Class which is what I am familiar with.

By the way, what part of the above code queries the calling machine's info?
0
 

Author Comment

by:trman
ID: 9939069
The above Function has build errors in VB.net and does not work.  Specifically, this line:   ReDim bpMacAddr(0 To PhyAddrLen - 1)
returns errors...
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 10246846
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: Tacobell777 {http:#9909482}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TheLearnedOne
EE Cleanup Volunteer
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 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