1. vb中如何获取本机ip地址

定义以下过程即可获取本机地址

SubGetMyIP()
DimstrComputerAsString
DimobjWMIAsObject
DimcolIPAsObject
DimIPAsObject
DimIAsInteger

strComputer="."
SetobjWMI=GetObject("winmgmts://"&strComputer&"/root/cimv2")
SetcolIP=objWMI.ExecQuery_
("Select*fromWin32_=TRUE")
ForEachIPIncolIP
IfNotIsNull(IP.IPAddress)Then
ForI=LBound(IP.IPAddress)ToUBound(IP.IPAddress)
MsgBox"IP地址:"&IP.IPAddress(I)&Chr(10)&_
Next
EndIf
Next
EndSub

2. vb6.0如何获得局域网所有IP地址

Option Explicit
'===================以下定义用于获得本机IP==================
Private Const WSADescription_Len = 256
Private Const WSASYS_Status_Len = 128
Private Type WSA_DATA
wVersion As Integer
wHighVersion As Integer
strDescription(WSADescription_Len + 1) As Byte
strSystemStatus(WSASYS_Status_Len + 1) As Byte
iMaxSockets As Integer
iMaxUdpDg As Integer
lpVendorInfo As Long
End Type
Private Type HOSTENT
hname As Long
hAliases As Long
hAddrType As Integer
hLength As Integer
hAddrList As Long
End Type
Private Declare Function WSAStartup Lib "ws2_32.dll" (ByVal _
wVersionRequired&, lpWSAData As WSA_DATA) As Long
Private Declare Function gethostbyname Lib "ws2_32.dll" (ByVal hostname$) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal length As Long)
Private Declare Function WSACleanup Lib "ws2_32.dll" () As Long
'===================以上定义用于获得本机IP==================

'=====================以下定义用于获得MAC====================
Private Declare Function SendARP Lib "iphlpapi" (ByVal dest As Long, ByVal host As Long, ByRef Mac As Any, ByRef length As Long) As Long
Private Declare Function inet_addr Lib "ws2_32.dll" (ByVal cp As String) As Long
'=====================以上定义用于获得MAC====================

'获得指定IP地址的MAC地址,用到全局变量hostIpStr为本机IP地址
'输入:IP为本网内的IP地址字符串,函数返回MAC地址,若出错返回空字符串
Function GetMac(IP As String) As String
Dim ldest As Long, lhost As Long, Mac(5) As Byte, length As Long
Dim i As Long, lR As Long, hostIpStr As String
hostIpStr = GetMyIp
GetMac = "" ' 若得不到MAC!
If hostIpStr <> "" Then
ldest = inet_addr(IP) '//目的地的IP转换为IP内码形式
lhost = inet_addr(hostIpStr) '//将本机IP转换为IP内码形式
length = 6
lR = SendARP(ldest, lhost, Mac(0), length)
If length > 0 Then
For i = 0 To length - 1
GetMac = GetMac & Right("00" & Hex(Mac(i)), 2)
Next i
End If
End If
End Function

'获得本机IP地址,函数返回值=本机IP地址,若出错返回空字符串
Function GetMyIp() As String
Dim WSAD As WSA_DATA
Dim lR As Long, MyIp As String
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
lR = WSAStartup(&H202, WSAD)
If lR <> 0 Then 'WSANOERROR Then
MsgBox "启动WSAStartup失败!"
GetMyIp = ""
Exit Function
End If
hostent_addr = gethostbyname("")

If hostent_addr = 0 Then
GetMyIp = "" '注释:主机名不能被解释
Exit Function
End If

CopyMemory host, ByVal hostent_addr, LenB(host)
CopyMemory hostip_addr, ByVal host.hAddrList, 4

ReDim temp_ip_address(1 To host.hLength)
CopyMemory temp_ip_address(1), ByVal 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)
GetMyIp = ip_address
WSACleanup
End Function

Private Sub Form_Load()
MsgBox GetMyIp
MsgBox GetMac(GetMyIp)
MsgBox GetMac("192.168.1.1")
End Sub