INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

How to get the MAC address of the first network card on the SERVER

How to get the MAC address of the first network card on the SERVER

(OP)
I've got some code which returns the MAC addresses of each network card. My problem is finding a way to return that information to a workstation, since it will be the workstation which wants to know the MAC address of the server (for use in an Activation process to test whether the activation code is associated with the hardware - since each workstation will have a different MAC address, the only 'constant' on the network will be the MAC address of the server).

At the moment I've got an EXE on the server which the workstation executes, the EXE writes the MAC address of the server's network card into a file in the same folder as the EXE and the workstation then reads that file. However this fails when, as is typical, the user doesn't have file-write access to the server. I want to avoid system administrators having to grant read-write access to the folder for every user.

What mechanism can I use to pull information from the server without writing it to a file on the server? Can I pass something to the EXE on the server which would allow it to write back to a file on the workstation, or into the registry on the workstation?

- Andy
___________________________________________________________________
If a man speaks in a forest and there are no women around to hear him - will he still be wrong?

RE: How to get the MAC address of the first network card on the SERVER

(OP)
The code, if it helps. Can it be altered to examine the cards on the server rather than the local machine?:

CODE

Dim oWMIService As Object
  Dim oColAdapters As Object
  Dim oObjAdapter As Object
  
  Set oWMIService = GetObject("winmgmts:" & "!\\.\root\cimv2")
  Set oColAdapters = oWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")
  
  For Each oObjAdapter In oColAdapters
    N$ = N$ & oObjAdapter.MACAddress
    If (N$ <> "") Then Exit For
  Next
  
  Set oObjAdapter = Nothing
  Set oColAdapters = Nothing
  Set oWMIService = Nothing 

- Andy
___________________________________________________________________
If a man speaks in a forest and there are no women around to hear him - will he still be wrong?

RE: How to get the MAC address of the first network card on the SERVER

"winmgmts:" & "!\\.\root\cimv2"

The . represents the local host name, but you can replace it with the name of any accessible host, eg

"winmgmts:" & "!\\ServerIAmInterestedIn\root\cimv2"

RE: How to get the MAC address of the first network card on the SERVER

(OP)
Cool, so if we assume that the main application is running on the server, is there a way to extrapolate the server name based on App.Path? If so that would be the perfect solution.

- Andy
___________________________________________________________________
If a man speaks in a forest and there are no women around to hear him - will he still be wrong?

RE: How to get the MAC address of the first network card on the SERVER

(OP)
Actually I already have some code to do that so I've answered my own question. For anyone who needs the code it's below.

CODE

If (Left$(App.Path, 2) <> "\\") Then
  ServerName$ = func_LetterToUNC(Left$(App.Path, 2))
  ServerName$ = Mid$(App.Path, 3, Instr(3, App.Path, "\") - 3)
End If

Public Function func_LetterToUNC(DriveLetter As String) As String

  On Error Resume Next
  Dim hEnum As Long
  Dim NetInfo(1023) As NETRESOURCE
  Dim Entries As Long
  Dim nStatus As Long
  Dim LocalName As String
  Dim UNCName As String
  Dim I As Long
  Dim r As Long

  nStatus = WNetOpenEnum(RESOURCE_CONNECTED, RESOURCETYPE_ANY, 0&, ByVal 0&, hEnum)
  func_LetterToUNC = DriveLetter

  If ((nStatus = 0) And (hEnum <> 0)) Then
    Entries = 1024
    nStatus = WNetEnumResource(hEnum, Entries, NetInfo(0), CLng(Len(NetInfo(0))) * 1024)
    If (nStatus = 0) Then
      For I = 0 To Entries - 1
        LocalName = ""
        If (NetInfo(I).lpLocalName <> 0) Then
          LocalName = Space(lstrlen(NetInfo(I).lpLocalName) + 1)
          r = lstrcpy(LocalName, NetInfo(I).lpLocalName)
        End If
        If (Len(LocalName) <> 0) Then LocalName = Left(LocalName, (Len(LocalName) - 1))
        If (UCase$(LocalName) = UCase$(DriveLetter)) Then
          UNCName = ""
          If (NetInfo(I).lpRemoteName <> 0) Then
            UNCName = Space(lstrlen(NetInfo(I).lpRemoteName) + 1)
            r = lstrcpy(UNCName, NetInfo(I).lpRemoteName)
          End If
          If (Len(UNCName) <> 0) Then UNCName = Left(UNCName, (Len(UNCName) - 1))
          func_LetterToUNC = UNCName
          Exit For
        End If
      Next I
    End If
  End If
  nStatus = WNetCloseEnum(hEnum)

End Function 

- Andy
___________________________________________________________________
If a man speaks in a forest and there are no women around to hear him - will he still be wrong?

RE: How to get the MAC address of the first network card on the SERVER

Your function could be a little shorter ...

CODE

Public Function func_LetterToUNC(DriveLetter As String) As String
    Dim NetDrives As Object
    Dim lp As Long
    
    Set NetDrives = CreateObject("WScript.Network").EnumNetworkDrives
    
    For lp = 0 To NetDrives.Length - 1 Step 2
        If UCase(DriveLetter) = UCase(NetDrives(lp)) Then
            func_LetterToUNC = NetDrives(lp + 1)
            Exit For
        End If
    Next
End Function 

And you can always early bind (instead of the late binding I'm using for the example), by setting a reference to Windows Script Host Object Library

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close