Home > Software engineering >  How will this code in VB Dll
How will this code in VB Dll

Time:04-19



'the code by lichmama from cnblogs.com
Private Type IPAddr
Ip1 As Byte
Ip2 As Byte
Ip3 As Byte
Ip4 As Byte
End Type

Private Type IP_OPTION_INFORMATION
Ttl As Byte
Tos As Byte
Flags As Byte
OptionsSize As Byte
OptionsData As Long
End Type

Private Type ICMP_ECHO_REPLY
The Address As IPAddr
The Status As Long
RoundTripTime As Long
DataSize As Integer
Reserved As Integer
PtrData As Long
The Options As IP_OPTION_INFORMATION
The Data As String * 250
End Type

Private Const REQUEST_TIMEOUT=11010

Private Declare Sub RtlZeroMemory Lib "KERNEL32" (dest As Any, ByVal numBytes As Long)
Private Declare Function IcmpCreateFile Lib "icmp. DLL () As" Long
Private Declare Function IcmpCloseHandle Lib "icmp. DLL" (ByVal IcmpHandle As Long) As Long
Private Declare Function IcmpSendEcho Lib "icmp. DLL" (ByVal IcmpHandle As Long, _
ByVal DestinationAddress As Long, _
ByVal RequestData As String, _
ByVal RequestSize As Long, _
ByVal RequestOptions As Long, _
ReplyBuffer As ICMP_ECHO_REPLY, _
ByVal ReplySize As Long, _
ByVal a timeout) As Long As Long

Private Const WS_VERSION_REQD=& amp; H101
Private Const WS_VERSION_MAJOR=WS_VERSION_REQD \ & amp; H100 And & amp; HFF&
Private Const WS_VERSION_MINOR=WS_VERSION_REQD And & amp; HFF&
Private Const MIN_SOCKETS_REQD=1
Private Const SOCKET_ERROR=1
Private Const WSADescription_Len=256
Private Const WSASYS_Status_Len=128

Private Type HOSTENT
HName As Long
HAliases As Long
HAddrType As Integer
HLength As Integer
HAddrList As Long
End Type

Private Type WSADATA
Wversion As Integer
WHighVersion As Integer
SzDescription As Byte (0 To WSADescription_Len)
SzSystemStatus As Byte (0 To WSASYS_Status_Len)
IMaxSockets As Integer
IMaxUdpDg As Integer
LpszVendorInfo As Long
End Type

Private Declare Function WSAGetLastError Lib "WSOCK32. DLL () As" Long
Private Declare Function WSAStartup Lib "WSOCK32. DLL" (ByVal wVersionRequired As an Integer, _
Lpwsadata As Long As WSADATA)
Private Declare Function WSACleanup Lib "WSOCK32. DLL () As" Long
Private Declare Function gethostname Lib "WSOCK32. DLL" (ByVal hostname As String, _
ByVal HostLen As Long As Long)
Private Declare Function gethostbyname Lib "WSOCK32. DLL" (ByVal hostname As String) As Long
Private Declare Sub RtlMoveMemory Lib "KERNEL32" (hpvDest As Any, _
ByVal hpvSource As Long, _
ByVal cbCopy As Long)

Private Function IPString2Long (ByVal IP As String) As Long
For Each Item In the Split (IP, ". ")
V=Hex (Item)
If Len (v)=1 Then v="0" & amp; V
Hex_=v & amp; Hex_
Next
IPString2Long=CLng (" & amp; H "& amp; Hex_)
End the Function

Private Function GetIpAddressByHostName (ByVal hostname As String) As String
Dim lpwsadata As WSADATA
Call WSAStartup (WS_VERSION_REQD lpwsadata)

Dim hostent_addr As Long
Dim the host As HOSTENT
Dim hostip_addr As Long
Dim temp_ip_addr () As Byte
Dim As Integer I
Dim ip_address As String
Hostent_addr=gethostbyname (hostname)
If hostent_addr=0 Then
The Exit Function
End the If
Call RtlMoveMemory (host, hostent_addr LenB (host))
Call RtlMoveMemory (hostip_addr, host. HAddrList, 4 & amp;)
Do
ReDim temp_ip_address (1 To the host. HLength) As Byte
Call RtlMoveMemory (temp_ip_address (1), hostip_addr, host. HLength)

For I=1 To the host. HLength
Ip_address=ip_address & amp; Temp_ip_address (I) & amp; ". "
Next
Ip_address=Mid $(ip_address, 1, Len (ip_address) - 1)

GetIpAddressByHostName=ip_address
GoTo EXIT__

The Debug. Print ip_address


ip_address=""Host. HAddrList=host. HAddrList + LenB (host. HAddrList)
Call RtlMoveMemory (hostip_addr, host. HAddrList, 4 & amp;)
Loop While (hostip_addr & lt;> 0)

EXIT__ :
Erase temp_ip_address
Call WSACleanup
End the Function

Private Function Ping (IP As String, ByVal ReplyBuff As ICMP_ECHO_REPLY) As Long
Dim IcmpHandle As Long

IcmpHandle=IcmpCreateFile ()
If IcmpHandle Then
Dim addr As Long
Dim sendbuff As String
Dim the timeout As Long

Timeout=1000 'set the timeout 1000 ms
Sendbuff=String (32, & amp; HFF)
Addr=IPString2Long (IP)
Call RtlZeroMemory (ByVal VarPtr (ReplyBuff), Len (ReplyBuff))
Call IcmpSendEcho (IcmpHandle, addr, sendbuff, Len (sendbuff), 0 & amp; , ReplyBuff, Len (ReplyBuff), a timeout)
Call IcmpCloseHandle (IcmpHandle)
Ping=ReplyBuff. Status
The Else
'the icmp initailize fail
Ping=1
End the If
End the Function

Private Sub Command1_Click ()
Dim IP As String
Dim ier As ICMP_ECHO_REPLY
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related