|
|
|
This example will show you how to get IP address from host name and also How to retrive all associated IPs to your local machine. |
Click here to copy the following block |
Private Type HOSTENT h_name As Long h_aliases As Long h_addrtype As Integer h_length As Integer h_addr_list As Long End Type Private Const AF_INET = 2 Private Declare Function gethostbyname _ Lib "WSOCK32.DLL" (ByVal name As String) As Long Private Declare Function gethostname _ Lib "WSOCK32.DLL" (ByVal hostname$, ByVal HostLen As Long) As Long Private Declare Function inet_ntoa _ Lib "WSOCK32.DLL" (ByVal inaddr As Long) As Long Private Declare Sub CopyMemory _ Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any _ , Source As Any, ByVal length As Long) Private Declare Function lstrlen _ Lib "kernel32.dll" Alias "lstrlenA" (ByVal lpString As Any) As Long Private Declare Function lstrcpy _ Lib "kernel32.dll" Alias "lstrcpyA" (ByVal lpString1 As Any, ByVal _ lpString2 As Any) As Long
Private Sub Form_Load() MsgBox GetIpFromName("WWW.google.com") MsgBox GetAllLocalIP End Sub
Function GetIpFromName(hostname As String) As String Dim hostinfo As HOSTENT Dim pHostinfo As Long Dim pIPAddress As Long Dim ipAddress As Long Dim pIPString As Long Dim ipString As String Dim retval As Long
pHostinfo = gethostbyname(hostname) If pHostinfo = 0 Then Else CopyMemory hostinfo, ByVal pHostinfo, Len(hostinfo) If hostinfo.h_addrtype <> AF_INET Then Debug.Print "A non-IP address was returned." Else CopyMemory pIPAddress, ByVal hostinfo.h_addr_list, 4
CopyMemory ipAddress, ByVal pIPAddress, 4 pIPString = inet_ntoa(ipAddress) ipString = Space(lstrlen(pIPString)) retval = lstrcpy(ipString, pIPString) GetIpFromName = ipString End If End If
End Function
Private Sub GetAllLocalIP() Dim hostname As String * 256 Dim hostent_addr As Long Dim host As HOSTENT Dim hostip_addr As Long Dim temp_ip_address() As Byte Dim i As Integer Dim ip_address As String
If gethostname(hostname, 256) = -1 Then MsgBox "Windows Sockets error " Exit Sub Else hostname = Trim$(hostname) End If
hostent_addr = gethostbyname(hostname)
If hostent_addr = 0 Then MsgBox "Winsock.dll is not responding." Exit Sub End If
CopyMemory host, ByVal hostent_addr, LenB(host) CopyMemory hostip_addr, ByVal host.h_addr_list, 4
MsgBox hostname
Do ReDim temp_ip_address(1 To host.h_length) CopyMemory temp_ip_address(1), ByVal hostip_addr, host.h_length
For i = 1 To host.h_length ip_address = ip_address & temp_ip_address(i) & "." Next ip_address = Mid$(ip_address, 1, Len(ip_address) - 1)
MsgBox ip_address
ip_address = "" host.h_addr_list = host.h_addr_list + LenB(host.h_addr_list) CopyMemory hostip_addr, ByVal host.h_addr_list, 4 Loop While (hostip_addr <> 0)
End Sub |
|
|
|
Submitted By :
Nayan Patel
(Member Since : 5/26/2004 12:23:06 PM)
|
|
|
Job Description :
He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |
View all (893) submissions by this author
(Birth Date : 7/14/1981 ) |
|
|