[VB code] Get host IP via Hostname

xiaoxiao2021-03-06  55

'Here's Sample Code for getHostByname ()

'Add a Textbox (Text1) And a command button (Command1) To a new form and use the Following Code:

'Usage: Fill in the textbox with the name you want to resolve and click the command button to resolve the name.

Private Const WS_VERSION_REQD = & H101Private Const WS_VERSION_MAJOR = WS_VERSION_REQD / & H100 And & HFF & Private Const WS_VERSION_MINOR = WS_VERSION_REQD And & HFF & Private Const MIN_SOCKETS_REQD = 1Private Const SOCKET_ERROR = -1Private Const WSADescription_Len = 256Private Const WSASYS_Status_Len = 128

Private Type Hostename As Longhaliases As Longhaddrtype As IntegerHength AS INTEGERHADDRLIST AS LONGEND TYPE

Private Type WSADATAwversion As IntegerwHighVersion As IntegerszDescription (0 To WSADescription_Len) As ByteszSystemStatus (0 To WSASYS_Status_Len) As ByteiMaxSockets As IntegeriMaxUdpDg As IntegerlpszVendorInfo As LongEnd Type

Private Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As LongPrivate Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal wVersionRequired &, lpWSAData As WSADATA) As LongPrivate Declare Function WSACleanup Lib "WSOCK32.DLL" () As LongPrivate Declare Function gethostbyname Lib "Wsock32.dll" (Byval Hostname $) As longprivate declare sub rtlmovememory lib "kernel32" (HPVDest As Any, Byval HPVSource &, Byval Cbcopy &)

Function Hibyte (Byval WParam As Integer)

Hibyte = WPARAM / & H100 and & HFF & HFF &

END FUNCTION

Function Lobyte (Byval WPARAM AS Integer)

Lobyte = WParam and & HFF & HFF &

END FUNCTION

Sub socketsinitialize ()

DIM WSAD AS WSADATADIM IRETURN AS INTEGERDIM SLOWBYTE AS STRING, SHIGHBYTE AS STRING, SMSG AS STRINGIRETURN = WSASTARTUP (WS_Version_reqd, WSAD)

IReturn <> 0 THENMSGBOX "Winsock.dll Is Not Responding." Endendiff.

If lobyte (WSAD.wversion)

IF wsad.imaxsockets

End Sub

Sub socketscleanup () DIM LRETURN AS Long

Lreturn = wsacleanup ()

If Lreturn <> 0 THENMSGBOX "SOCKET ERROR" & TRIM $ (Lreturn) & "Occurred In Cleanup" Endend IF

End Sub

SUB FORM_LOAD ()

Socketsinitialize

End Sub

Private Sub Form_Unload (Cancel AS Integer)

SocketScleanup

End Sub

PRIVATE SUB Command1_Click () DIM HOSTENT_ADDR AS LONGDIM HOST AS HOSTENTDIM HOSTIP_ADDR AS Longdim Temp_ip_address () AS BYTEDIM I as IntegerDIM IP_Address As String

Hostent_addr = gethostbyname (Text1)

If hostent_addr = 0 ThenmsgBox "can't resolve name." EXIT SUBEND IF

RTLMOVEMEMORY HOST, HOSTENT_ADDR, LENB (HOST) RTLMovememory Hostip_addr, Host.haddrlist, 4

Redim temp_ip_address (1 to host.hlength) RTLMoveMemory Temp_ip_address (1), Hostip_Addr, Host.hlength

For i = 1 to host.hlengthip_address = ip_address & temp_ip_address (i) & "." NEXTIP_ADDRESS = MID $ (IP_ADDRESS, 1, LEN (IP_ADDRESS) - 1) MSGBOX IP_ADDRESS

End Sub

转载请注明原文地址:https://www.9cbs.com/read-114339.html

New Post(0)