MELeBlanc
asked on
Obtain hostname from IP address
Anyone have a code snippet that will retrieve a machine name from an IP address?
TIA
-M
TIA
-M
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Or you can read it from netbios, or you can try both, for maximum results !!!
REMOTE REG METHOD ...
Private Function GetNameFromReg(sIP As String) As String
' Return a computer name using remote registry
Dim sResult As String
Dim reg As New RemoteReg
sResult = reg.GetValue(eHKEY_LOCAL_M
GetNameFromReg = sResult
End Function
and this was in the remote reg class...
Public Function GetValue(ByVal PredefinedKey As HKEYs, ByVal KeyName As String, ByVal ValueName As String, Optional ComputerName As String) As Variant
On Error GoTo ErrHand
Dim GetHandle As Long
Dim hKey As Long
Dim lpData As String
Dim lpDataDWORD As Long
Dim lpcbData As Long
Dim lpType As Long
Dim lReturnCode As Long
Dim lhRemoteRegistry As Long
If Left$(KeyName, 1) = "\" Then
KeyName = Right$(KeyName, Len(KeyName) - 1)
End If
If ComputerName = "" Then
GetHandle = RegOpenKeyEx(PredefinedKey
Else
lReturnCode = RegConnectRegistry(Compute
GetHandle = RegOpenKeyEx(lhRemoteRegis
End If
If GetHandle = ERROR_SUCCESS Then
lpcbData = 255
lpData = String(lpcbData, Chr(0))
GetHandle = RegQueryValueEx(hKey, ValueName, 0, lpType, ByVal lpData, lpcbData)
If GetHandle = ERROR_SUCCESS Then
Select Case lpType
Case REG_SZ
GetHandle = RegQueryValueExString(hKey
If GetHandle = 0 Then
GetValue = Left$(lpData, lpcbData - 1)
Else
GetValue = ""
End If
Case REG_DWORD
GetHandle = RegQueryValueEx(hKey, ValueName, 0, lpType, lpDataDWORD, lpcbData)
If GetHandle = 0 Then
GetValue = CLng(lpDataDWORD)
Else
GetValue = 0
End If
Case REG_BINARY
GetHandle = RegQueryValueEx(hKey, ValueName, 0, lpType, lpDataDWORD, lpcbData)
If GetHandle = 0 Then
GetValue = CByte(lpDataDWORD)
Else
GetValue = 0
End If
End Select
End If
RegCloseKey hKey
End If
Exit Function
ErrHand:
Err.Raise "11002", "clsRegistry", "GetValue"
End Function
WINSOCK METHOD ....
Option Explicit
Private Const BIF_BROWSEFORCOMPUTER = &H1000
Const WSADESCRIPTION_LEN = 256 + 1
Const WSASYS_STATUS_LEN = 128 + 1
Const AF_INET = 2
Const INADDR_NONE = &HFFFFFFFF
Private Type WSADATA
wHighVersion As Long
szDescription As String * WSADESCRIPTION_LEN
szSystemStatus As String * WSASYS_STATUS_LEN
iMaxSockets As Long
iMaxUdpDg As Long
lpVendorInfo As Long
End Type
Private Type HOSTENT
h_name As Long 'LPSTR
h_aliases As Long 'LPLPSTR
h_addrtype As Integer
h_length As Integer
h_addr_list As Long 'char FAR * FAR * h_addr_list
End Type
Private Type sockaddr_in
sin_family As Long
sin_port As Integer
in_addr As Long
sin_zero As String * 8
End Type
Private Type in_addr
b1 As Byte
b2 As Byte
b3 As Byte
b4 As Byte
End Type
Private Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal wVersionRequested As Integer, ByRef lpWSADATA As WSADATA) As Long
Private Declare Sub WSACleanup Lib "WSOCK32.DLL" ()
Private Declare Function gethostbyaddr Lib "WSOCK32.DLL" (addr As Any, ByVal iLen As Long, ByVal itype As Long) As Long
Private Declare Function getnameinfo Lib "WSOCK32.DLL" (addr As Any, ByVal iLen As Long, ByVal host As String, ByVal hostlen As Long, ByVal serv As String, ByVal servlen As Long, ByVal flags As Long) As Long
Private Declare Function inet_addr Lib "WSOCK32.DLL" (ByVal cp As String) As Long
Private Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal name As String) As Long 'HOSTENT
Private Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As Long) As Long
Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1 As String, lpString2 As Any) As Long
Public Function GetName(cpAddr As String) As String
Dim Adata As WSADATA
Dim addr As Long
Dim p As Long
Dim host As HOSTENT
Dim buf As String
Dim sHost As String
Dim sServ As String
Dim sArr() As String
Dim sWork As String
sWork = ""
If WSAStartup(&H101, Adata) <> 0 Then Exit Function
addr = inet_addr(cpAddr & Chr$(0))
If addr <> INADDR_NONE Then
p = gethostbyaddr(ByVal VarPtr(addr), 4, AF_INET)
If p Then
CopyMemory ByVal VarPtr(host), ByVal p, LenB(host)
buf = Space(lstrlen(host.h_name)
lstrcpy buf, ByVal host.h_name
sWork = Trim(Left(buf, lstrlen(host.h_name)))
Else
End If
End If
WSACleanup
If InStr(sWork, ".") > 0 Then
sArr = Split(sWork, ".")
If sArr(0) <> "" Then
sWork = sArr(0)
End If
End If
GetName = sWork
End Function