使用Winsock控件即可获得,方法如下:
Private Sub Command1_Click()
MsgBox Winsock1.LocalIP
End Sub
注意:Winsock控件不是标准工具箱中的控件,使用前要先在菜单"工程-部件"中选择,将"Microsoft Winsock Control 6.0"选中就可以了
Private Sub Command1_Click()
Dim aa As String
Dim strLocalIP As String
Dim winIP As Object
aa = aa & "本机电脑名称:" & Environ("computername") & vbCrLf
aa = aa & "本机用户名称:" & Environ("username") & vbCrLf
Set winIP = CreateObject("MSWinsock.Winsock")
strLocalIP = winIP.localip
MsgBox aa & "本机IP:" & strLocalIP
End Sub
Private Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Long
Private Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal wVersionRequired As Integer, lpWSAData As WSADATA) As Long
Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
Private Declare Function gethostname Lib "WSOCK32.DLL" (ByVal hostname$, ByVal HostLen As Long) As Long
Private Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal _
hostname$) As Long
Private Declare Sub RtlMoveMemory Lib "KERNEL32" (hpvDest As Any, ByVal hpvSource&, ByVal cbCopy&)
Private Const WS_VERSION_REQD = &H101
Private 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 = 1
Private Const SOCKET_ERROR = -1
Private Const WSADescription_Len = 256
Private Const WSASYS_Status_Len = 128
On Error Resume Next
lbsname.Caption = " --------"
lbslip.Caption = " --------"
lbship.Caption = " --------"
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
Dim judge As Integer
judge = 0
If gethostname(hostname, 256) = SOCKET_ERROR Then
MsgBox "Windows Sockets error " & str(WSAGetLastError())
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
RtlMoveMemory host, hostent_addr, LenB(host)
RtlMoveMemory hostip_addr, host.hAddrList, 4
lbsname.Caption = " " & hostname
'get all of the IP address if machine is multi-homed
Do
ReDim temp_ip_address(1 To host.hLength)
RtlMoveMemory temp_ip_address(1), hostip_addr, host.hLength
For i = 1 To host.hLength
ip_address = ip_address & temp_ip_address(i) & "."
Next
ip_address = Mid$(ip_address, 1, Len(ip_address) - 1)
If judge = 0 Then
lbslip.Caption = " " & ip_address
judge = 1
Else
lbship.Caption = " " & ip_address
End If
ip_address = ""
host.hAddrList = host.hAddrList + LenB(host.hAddrList)
RtlMoveMemory hostip_addr, host.hAddrList, 4
Loop While (hostip_addr <> 0)
参考:http://www.code365.com/programe/vb/code/200512142114072162.htm
工程->部件->Windows Winsock Control
在窗体上添加一个winsock控件,和一个CommandButton
Private Sub Command1_Click()
MsgBox Winsock1.LocalIP
End Sub
支持2楼那用这么复杂啊~~~控件就是用来用的~~~如果什么都用代码自己完成的话编程还有什么集体可言啊