Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

AndyGroom

Programmer
May 23, 2001
972
GB
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?
 
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?
 
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
 
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?
 
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?
 
Your function could be a little shorter ...

Code:
[blue]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[/blue]

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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top