I know it's possible to retrieve the MAC address of a local computer using Visual BASIC; however, is it possible to query the MAC address of a remote computer using VB?
Printable View
I know it's possible to retrieve the MAC address of a local computer using Visual BASIC; however, is it possible to query the MAC address of a remote computer using VB?
try this, you may not need it all................
Code:'Name Flags
Public Const NAME_FLAGS_MASK = &H87
Public Const GROUP_NAME = &H80
Public Const UNIQUE_NAME = &H0
Public Const REGISTERING = &H0
Public Const REGISTERED = &H4
Public Const DEREGISTERED = &H5
Public Const DUPLICATE = &H6
Public Const DUPLICATE_DEREG = &H7
Public Const NCBNAMSZ = 16
Public Const HEAP_ZERO_MEMORY = &H8
Public Const HEAP_GENERATE_EXCEPTIONS = &H4
Public Const NCBENUM = &H37
Public Const NCBRESET = &H32
Public Const NCBASTAT = &H33
Public Type NTWRKCNTRLBLCK
ncb_command As Byte
ncb_retcode As Byte
ncb_lsn As Byte
ncb_num As Byte
ncb_buffer As Long
ncb_length As Integer
ncb_callname(0 To 15) As Byte
ncb_name(0 To 15) As Byte
ncb_rto As Byte
ncb_sto As Byte
lpFunc As Long
ncb_lana_num As Byte
ncb_cmd_cplt As Byte
ncb_reserve(0 To 9) As Byte
ncb_event As Long
End Type
Public Type LANA_ENUM
Length As Byte
lana(0 To 256) As Byte
End Type
Public Type ADAPTER_STATUS
adapter_address(0 To 5) As Byte
rev_major As Byte
reserved0 As Byte
adapter_type As Byte
rev_minor As Byte
duration As Integer
frmr_recv As Integer
frmr_xmit As Integer
iframe_recv_err As Integer
xmit_aborts As Integer
xmit_success As Long
recv_success As Long
iframe_xmit_err As Integer
recv_buff_unavail As Integer
t1_timeouts As Integer
ti_timeouts As Integer
reserved1 As Long
free_ncbs As Integer
max_cfg_ncbs As Integer
max_ncbs As Integer
xmit_buf_unavail As Integer
max_dgram_size As Integer
pending_sess As Integer
max_cfg_sess As Integer
max_sess As Integer
max_sess_pkt_size As Integer
name_count As Integer
End Type
Public Type NAME_BUFFER
name_(0 To 15) As Byte
name_num As Byte
name_flags As Byte
End Type
Public Type NET_STATUS
Adapter As ADAPTER_STATUS
NameBuffer(30) As NAME_BUFFER
End Type
Public Type DiskInformation
lpSectorsPerCluster As Long
lpBytesPerSector As Long
lpNumberOfFreeClusters As Long
lpTotalNumberOfClusters As Long
End Type
Public Declare Function NetBios Lib "netapi32.dll" Alias "Netbios" _
(ByRef pncb As NTWRKCNTRLBLCK) As Byte
Public Declare Function VarPtr Lib "MSVBVM50.DLL" (pVoid As Any) As Long
Public Declare Function GetDiskFreeSpace Lib "kernel32" Alias "GetDiskFreeSpaceA" _
(ByVal lpRootPathName As String, _
lpSectorsPerCluster As Long, _
lpBytesPerSector As Long, _
lpNumberOfFreeClusters As Long, _
lpTotalNumberOfClusters As Long) As Long
Public Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" _
(ByVal nDrive As String) As Long
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)
Public Declare Function GetProcessHeap Lib "kernel32" () As Long
Public Declare Function HeapAlloc Lib "kernel32" _
(ByVal hHeap As Long, ByVal dwFlags As Long, ByVal dwBytes As Long) As Long
Public Declare Function HeapFree Lib "kernel32" (ByVal hHeap As Long, _
ByVal dwFlags As Long, lpMem As Any) As Long
Public Sub GetMacAddr()
'
'
Dim NCB As NTWRKCNTRLBLCK, Status As NET_STATUS, LanEnum As LANA_ENUM
Dim bReturn As Byte, sMacAddress As String, i As Integer, sHex As String, l%
Dim k%, iNumNames%, j%, m%, sName$, iPos%, nFlags%
NCB.ncb_command = NCBENUM
NCB.ncb_buffer = VarPtr(LanEnum)
NCB.ncb_length = LenB(LanEnum)
bReturn = NetBios(NCB)
l = LanEnum.Length
If l > 0 Then
For k = 0 To l
NCB.ncb_command = NCBRESET
NCB.ncb_lana_num = LanEnum.lana(k)
bReturn = NetBios(NCB)
NCB.ncb_command = NCBASTAT
NCB.ncb_lana_num = LanEnum.lana(k)
NCB.ncb_callname(0) = 42 'Max number of sessions 42
For i = 1 To 14
NCB.ncb_callname(i) = 32 'Max number of names 32, Use NAME_NUMBER_1
Next i
NCB.ncb_callname(15) = 0
NCB.ncb_buffer = VarPtr(Status)
NCB.ncb_length = LenB(Status)
bReturn = NetBios(NCB)
strNet = strNet & "Network #" & Hex$(k) & vbCrLf & Chr$(9)
sMacAddress = ""
For i = 0 To 5
sHex = Hex(Status.Adapter.adapter_address(i))
If Len(sHex) = 1 Then sHex = "0" & sHex
sMacAddress = sMacAddress & sHex
If i <> 5 Then sMacAddress = sMacAddress + "-"
Next i
strNet = strNet & "Mac Address = " & UCase(sMacAddress) & vbCrLf & Chr$(9)
strNet = strNet & "Adapter Type = " & Hex$(Status.Adapter.adapter_type) & vbCrLf & Chr$(9)
strNet = strNet & vbCrLf
Next k
End If
End Sub
Thanks. How do I implement the code to retrieve the MAC address of a remote computer? I don't see anything that specifies the IP address of the remote computer.
Create a new project. Put one command button and one listbox and one text box into the form. Don't change the names of the controls. If you have a remote machine with Windows NT on it and your machine is also NT you will need only the hostname from the remote machine. Put that into the text box and run the program.
Hope it helps!
Option Explicit
Private Declare Function NetWkstaTransportEnum Lib "netapi32" ( _
Servername As Any, _
ByVal Level As Long, _
Bufptr As Long, _
ByVal Prefmaxlen As Long, _
Entriesread As Long, _
Totalentries As Long, _
Resumehandle As Long) As Long
Private Declare Function lstrlenW Lib "kernel32" ( _
ByVal lpString As Long) As Long
Private Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" ( _
Destination As Any, _
Source As Any, _
ByVal Length As Long)
Private Declare Function NetApiBufferFree Lib "Netapi32.dll" ( _
ByVal lpBuffer As Long) As Long
Private Type WKSTA_TRANSPORT_INFO_0
wkti0_quality_of_service As Long
wkti0_number_of_vcs As Long
wkti0_transport_name As Long
wkti0_transport_address As Long
wkti0_wan_ish As Long
End Type
Private Sub Command1_Click()
Dim Servername() As Byte
Dim Level As Long
Dim Prefmaxlen As Long
Dim Result As Variant
Dim sResult As Variant
Dim sBufptr As WKSTA_TRANSPORT_INFO_0
Dim Bufptr As Long
Dim Entriesread As Long
Dim Totalentries As Long
Dim Resumehandle As Long
Dim Buffer() As Byte
Dim nLen As Long
Dim A As Long
Dim L As Long
List1.Clear
Prefmaxlen = -1
Level = 0
Servername = "\\" & Text1.Text & vbNullChar
Result = NetWkstaTransportEnum(Servername(0), Level, Bufptr, Prefmaxlen, Entriesread, Totalentries, Resumehandle)
If L = 0 Or L = 234& Then
For A = 0 To Entriesread - 1
CopyMem sBufptr, ByVal Bufptr, Len(sBufptr)
Debug.Print PtrToString(sBufptr.wkti0_transport_address)
Bufptr = Bufptr + Len(sBufptr)
Next A
End If
NetApiBufferFree Bufptr
End Sub
Private Function PtrToString(lpwString As Long) As String
Dim Buffer() As Byte
Dim nLen As Long
If lpwString Then
nLen = lstrlenW(lpwString) * 2
If nLen Then
ReDim Buffer(0 To (nLen - 1)) As Byte
CopyMem Buffer(0), ByVal lpwString, nLen
PtrToString = Buffer
List1.AddItem (Buffer)
End If
End If
End Function