怎么急干什么,我要帮你照到东西啊!
现在公布:
窗体:加两个TEXTBOX,一个BUTTON,一个StatusBar
代码为:
Dim HiByte As ByteDim WSData As WSADATAPrivate Sub Command1_Click()
Dim StartupStatus As Integer StartupStatus = SocketStartup() If (StartupStatus <> ERROR_SUCCESS) Then
StatusBar1.Panels(1).Text = "Windows Sockets " & CStr(LoByte) & "." & CStr(HiByte) & " is not available."
Else
StatusBar1.Panels(1).Text = "Searching for Host Name ..."
Text1.Text = ResolveHostName StatusBar1.Panels(1).Text = "Searching for IP Address ..." Text2.Text = ResolveIP
SocketClose
End If
End Sub
Private Sub SocketClose()
Dim iReturn As Integer If WSAIsBlocking Then
WSACancelBlockingCall
End If iReturn = WSACleanup()
If iReturn <> ERROR_SUCCESS Then
StatusBar1.Panels(1).Text = "Windows Sockets " & CStr(LoByte) & "." & CStr(HiByte) & " can not be closed"
Else
StatusBar1.Panels(1).Text = "Windows Sockets " & CStr(LoByte) & "." & CStr(HiByte) & " is closed."
End If
End SubPrivate Function SocketStartup() As Integer
Dim iReturn As Integer iReturn = WSAStartup(wVersionRequired, WSData)
If iReturn <> ERROR_SUCCESS Then
MsgBox "Windows Socket can not be started.", vbCritical + vbOKOnly
SocketStartup = iReturn
Exit Function
End If
HiByte = (WSData.wVersion And &HFF00&) \ (&H100)
LoByte = WSData.wVersion And &HFF&
If LoByte < wMajorVersion Or _
(LoByte = wMajorVersion And _
HiByte < wMinorVersion) Then
MsgBox "Sockets version " & CStr(LoByte) & "." & CStr(HiByte) & " is not supported.", vbCritical + vbOKOnly
SocketStartup = -1
Exit Function
End If
SocketStartup = iReturn
End FunctionPrivate Function ResolveHostName() As String
Dim HostName As String
Dim dwLength As Integer
dwLength = 256 ' 建立HostName字符串buffer
HostName = String(dwLength, Chr(0)) ' 传回本地主机的名称(host name)
gethostname HostName, Len(HostName) ResolveHostName = Left(HostName, (Len(HostName) - 1))
End FunctionPrivate Function ResolveIP() As String
Dim HostName As String
Dim dwLength As Integer
Dim RemoteHost As Long
Dim lHostEnt As HOSTENT
Dim InAddress As Long
Dim IPv4(0 To 3) As Byte
dwLength = 256 ' 建立HostName字符串buffer
HostName = String(dwLength, Chr(0)) ' 传回本地主机的名称(host name)
gethostname HostName, Len(HostName) RemoteHost = gethostbyname(Trim(HostName))
If RemoteHost = 0 Then
ResolveIP = "127.0.0.1"
Exit Function
Else
MoveMemory lHostEnt, RemoteHost, LenB(lHostEnt)
If lHostEnt.h_addr_list <> 0 Then
MoveMemory InAddress, lHostEnt.h_addr_list, lHostEnt.h_length
i = 0
Do While InAddress <> 0
MoveMemory IPv4(i), InAddress, lHostEnt.h_length
lHostEnt.h_addr_list = lHostEnt.h_addr_list + lHostEnt.h_length
MoveMemory InAddress, lHostEnt.h_addr_list, lHostEnt.h_length
i = i + 1
Loop
' 传回IPV4类型的主机IP address
ResolveIP = IPv4(0) & "." & IPv4(1) & "." & IPv4(2) & "." & IPv4(3)
Else
ResolveIP = "127.0.0.1"
End If
End If
End Function模块:
Public Const WSADESCRIPTION_LEN = 256
Public Const WSASYS_STATUS_LEN = 128Public Type HOSTENT
h_name As Long
h_aliases As Long
h_addrtype As Integer
h_length As Integer
h_addr_list As Long
End TypePublic Type WSADATA
wVersion As Long
wHighVersion As Long
szDescription(0 To WSADESCRIPTION_LEN) As Byte
szSystemStatus(0 To WSASYS_STATUS_LEN) As Byte
iMaxSockets As Long
iMaxUdpDg As Long
lpVendorInfo As Long
End TypePublic Declare Function WSAStartup Lib "WSOCK32.DLL" _
(ByVal wVersionRequested As Long, _
lpWSAData As WSADATA) As LongPublic Declare Function WSACleanup Lib "WSOCK32.DLL" _
() As IntegerPublic Declare Function WSAIsBlocking Lib "WSOCK32.DLL" _
() As BooleanPublic Declare Function WSACancelBlockingCall Lib "WSOCK32.DLL" _
() As IntegerPublic Declare Function gethostname Lib "WSOCK32.DLL" _
(ByVal name As String, _
ByVal namelen As Integer) As IntegerPublic Declare Function gethostbyname Lib "WSOCK32.DLL" _
(ByVal name As String) As Long
Public Const wVersionRequired = &H101
Public Const wMajorVersion = wVersionRequired \ &H100 And &HFF&
Public Const wMinorVersion = wVersionRequired And &HFF&Public Const ERROR_SUCCESS = 0Declare Sub MoveMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(pDest As Any, _
ByVal pSource As Any, _
ByVal dwLength As Long)
现在公布:
窗体:加两个TEXTBOX,一个BUTTON,一个StatusBar
代码为:
Dim HiByte As ByteDim WSData As WSADATAPrivate Sub Command1_Click()
Dim StartupStatus As Integer StartupStatus = SocketStartup() If (StartupStatus <> ERROR_SUCCESS) Then
StatusBar1.Panels(1).Text = "Windows Sockets " & CStr(LoByte) & "." & CStr(HiByte) & " is not available."
Else
StatusBar1.Panels(1).Text = "Searching for Host Name ..."
Text1.Text = ResolveHostName StatusBar1.Panels(1).Text = "Searching for IP Address ..." Text2.Text = ResolveIP
SocketClose
End If
End Sub
Private Sub SocketClose()
Dim iReturn As Integer If WSAIsBlocking Then
WSACancelBlockingCall
End If iReturn = WSACleanup()
If iReturn <> ERROR_SUCCESS Then
StatusBar1.Panels(1).Text = "Windows Sockets " & CStr(LoByte) & "." & CStr(HiByte) & " can not be closed"
Else
StatusBar1.Panels(1).Text = "Windows Sockets " & CStr(LoByte) & "." & CStr(HiByte) & " is closed."
End If
End SubPrivate Function SocketStartup() As Integer
Dim iReturn As Integer iReturn = WSAStartup(wVersionRequired, WSData)
If iReturn <> ERROR_SUCCESS Then
MsgBox "Windows Socket can not be started.", vbCritical + vbOKOnly
SocketStartup = iReturn
Exit Function
End If
HiByte = (WSData.wVersion And &HFF00&) \ (&H100)
LoByte = WSData.wVersion And &HFF&
If LoByte < wMajorVersion Or _
(LoByte = wMajorVersion And _
HiByte < wMinorVersion) Then
MsgBox "Sockets version " & CStr(LoByte) & "." & CStr(HiByte) & " is not supported.", vbCritical + vbOKOnly
SocketStartup = -1
Exit Function
End If
SocketStartup = iReturn
End FunctionPrivate Function ResolveHostName() As String
Dim HostName As String
Dim dwLength As Integer
dwLength = 256 ' 建立HostName字符串buffer
HostName = String(dwLength, Chr(0)) ' 传回本地主机的名称(host name)
gethostname HostName, Len(HostName) ResolveHostName = Left(HostName, (Len(HostName) - 1))
End FunctionPrivate Function ResolveIP() As String
Dim HostName As String
Dim dwLength As Integer
Dim RemoteHost As Long
Dim lHostEnt As HOSTENT
Dim InAddress As Long
Dim IPv4(0 To 3) As Byte
dwLength = 256 ' 建立HostName字符串buffer
HostName = String(dwLength, Chr(0)) ' 传回本地主机的名称(host name)
gethostname HostName, Len(HostName) RemoteHost = gethostbyname(Trim(HostName))
If RemoteHost = 0 Then
ResolveIP = "127.0.0.1"
Exit Function
Else
MoveMemory lHostEnt, RemoteHost, LenB(lHostEnt)
If lHostEnt.h_addr_list <> 0 Then
MoveMemory InAddress, lHostEnt.h_addr_list, lHostEnt.h_length
i = 0
Do While InAddress <> 0
MoveMemory IPv4(i), InAddress, lHostEnt.h_length
lHostEnt.h_addr_list = lHostEnt.h_addr_list + lHostEnt.h_length
MoveMemory InAddress, lHostEnt.h_addr_list, lHostEnt.h_length
i = i + 1
Loop
' 传回IPV4类型的主机IP address
ResolveIP = IPv4(0) & "." & IPv4(1) & "." & IPv4(2) & "." & IPv4(3)
Else
ResolveIP = "127.0.0.1"
End If
End If
End Function模块:
Public Const WSADESCRIPTION_LEN = 256
Public Const WSASYS_STATUS_LEN = 128Public Type HOSTENT
h_name As Long
h_aliases As Long
h_addrtype As Integer
h_length As Integer
h_addr_list As Long
End TypePublic Type WSADATA
wVersion As Long
wHighVersion As Long
szDescription(0 To WSADESCRIPTION_LEN) As Byte
szSystemStatus(0 To WSASYS_STATUS_LEN) As Byte
iMaxSockets As Long
iMaxUdpDg As Long
lpVendorInfo As Long
End TypePublic Declare Function WSAStartup Lib "WSOCK32.DLL" _
(ByVal wVersionRequested As Long, _
lpWSAData As WSADATA) As LongPublic Declare Function WSACleanup Lib "WSOCK32.DLL" _
() As IntegerPublic Declare Function WSAIsBlocking Lib "WSOCK32.DLL" _
() As BooleanPublic Declare Function WSACancelBlockingCall Lib "WSOCK32.DLL" _
() As IntegerPublic Declare Function gethostname Lib "WSOCK32.DLL" _
(ByVal name As String, _
ByVal namelen As Integer) As IntegerPublic Declare Function gethostbyname Lib "WSOCK32.DLL" _
(ByVal name As String) As Long
Public Const wVersionRequired = &H101
Public Const wMajorVersion = wVersionRequired \ &H100 And &HFF&
Public Const wMinorVersion = wVersionRequired And &HFF&Public Const ERROR_SUCCESS = 0Declare Sub MoveMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(pDest As Any, _
ByVal pSource As Any, _
ByVal dwLength As Long)
Public Const MAX_WSASYSStatus = 128
Public Const ERROR_SUCCESS As Long = 0
Public Const WS_VERSION_REQD As Long = &H101
Public Const WS_VERSION_MAJOR As Long = WS_VERSION_REQD \ &H100 And &HFF&
Public Const WS_VERSION_MINOR As Long = WS_VERSION_REQD And &HFF&
Public Const MIN_SOCKETS_REQD As Long = 1
Public Const SOCKET_ERROR As Long = -1Public Type HOSTENT
hName As Long
hAliases As Long
hAddrType As Integer
hLen As Integer
hAddrList As Long
End TypePublic Type WSADATA
wVersion As Integer
wHighVersion As Integer
szDescription(0 To MAX_WSADescription) As Byte
szSystemStatus(0 To MAX_WSASYSStatus) As Byte
wMaxSockets As Integer
wMaxUDPDG As Integer
dwVendorInfo As Long
End Type
Public Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As LongPublic Declare Function WSAStartup Lib "WSOCK32.DLL" _
(ByVal wVersionRequired As Long, lpWSADATA As WSADATA) As Long
Public Declare Function WSACleanup Lib "WSOCK32.DLL" () As LongPublic Declare Function gethostname Lib "WSOCK32.DLL" _
(ByVal szHost As String, ByVal dwHostLen As Long) As Long
Public Declare Function gethostbyname Lib "WSOCK32.DLL" _
(ByVal szHost As String) As Long
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)
Public Function GetIPAddress() As String Dim sHostName As String * 256
Dim lpHost As Long
Dim HOST As HOSTENT
Dim dwIPAddr As Long
Dim tmpIPAddr() As Byte
Dim i As Integer
Dim sIPAddr As String
If Not SocketsInitialize() Then
GetIPAddress = ""
Exit Function
End If
If gethostname(sHostName, 256) = SOCKET_ERROR Then
GetIPAddress = ""
MsgBox "Windows Sockets error " & Str$(WSAGetLastError()) & _
" has occurred. Unable to successfully get Host Name."
SocketsCleanup
Exit Function
End If
sHostName = Trim$(sHostName)
lpHost = gethostbyname(sHostName)
If lpHost = 0 Then
GetIPAddress = ""
MsgBox "Windows Sockets are not responding. " & _
"Unable to successfully get Host Name."
SocketsCleanup
Exit Function
End If
CopyMemory HOST, lpHost, Len(HOST)
CopyMemory dwIPAddr, HOST.hAddrList, 4
ReDim tmpIPAddr(1 To HOST.hLen)
CopyMemory tmpIPAddr(1), dwIPAddr, HOST.hLen
For i = 1 To HOST.hLen
sIPAddr = sIPAddr & tmpIPAddr(i) & "."
Next
GetIPAddress = Mid$(sIPAddr, 1, Len(sIPAddr) - 1)
SocketsCleanup
End FunctionPublic Function HiByte(ByVal wParam As Integer) HiByte = wParam \ &H100 And &HFF&
End Function
Public Function LoByte(ByVal wParam As Integer) LoByte = wParam And &HFF&End Function
Public Sub SocketsCleanup() If WSACleanup() <> ERROR_SUCCESS Then
MsgBox "Socket error occurred in Cleanup."
End If
End SubPublic Function SocketsInitialize() As Boolean Dim WSAD As WSADATA
Dim sLoByte As String
Dim sHiByte As String
If WSAStartup(WS_VERSION_REQD, WSAD) <> ERROR_SUCCESS Then
MsgBox "The 32-bit Windows Socket is not responding."
SocketsInitialize = False
Exit Function
End If
If WSAD.wMaxSockets < MIN_SOCKETS_REQD Then
MsgBox "This application requires a minimum of " & _
CStr(MIN_SOCKETS_REQD) & " supported sockets."
SocketsInitialize = False
Exit Function
End If
If LoByte(WSAD.wVersion) < WS_VERSION_MAJOR Or _
(LoByte(WSAD.wVersion) = WS_VERSION_MAJOR And _
HiByte(WSAD.wVersion) < WS_VERSION_MINOR) Then
sHiByte = CStr(HiByte(WSAD.wVersion))
sLoByte = CStr(LoByte(WSAD.wVersion))
MsgBox "Sockets version " & sLoByte & "." & sHiByte & _
" is not supported by 32-bit Windows Sockets."
SocketsInitialize = False
Exit Function
End If
SocketsInitialize = True
End Function代码:Private Sub Form_Load()
Text1.Text = GetIPAddress()
If Text1.Text = "127.0.0.1" Then
Label1.Caption = "You are of Line"
Else
Label1.Caption = "You are on Line"
End If
End Sub
很简单
加一个winsock控件
winsock控件中的LocalIP属性就能解决这个问题
辛苦两位大侠了(heeh() ,fraser01(王晓栋))
要么就象前两位一样贴给你调用winsock.dll
api函数的代码
要么就用我的方法
使用winsock控件
你要简单的话
那么只有后者了
甚至不用写代码