如何获取本机IP。。。?

senvy 2003-12-12 06:46:15
请教如何获取本机IP呢?

除了用Winsock.LocalIP外,还有什么方法么?
要求是不使用控件的,比如用函数,API等。

谢谢。
...全文
3 点赞 收藏 6
写回复
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
senvy 2003-12-12
谢谢各位大哥的帮忙。

只要是要溶入其它开发中去,用控件不方便。

特别感谢 SoHo_Andy(冰) 及版主。
回复
踏平扶桑 2003-12-12
可是用控件很方便
楼主取舍一下吧:)
回复
coffeesky0131 2003-12-12
用控件的话,会耗太多的资源,如果只是单单的去本机的ip地址。
回复
chutianqi 2003-12-12
除了用Winsock.LocalIP外,还有什么方法么?

有简单的不用干吗用那么复杂的呀~~~~~~~~~~~~~~~``想不通 ~~~~~~~~~~~~~~~~``
回复
coffeesky0131 2003-12-12
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
不推荐使用byref 。。。。。 As Any
回复
SoHo_Andy 2003-12-12
引用自版主
API方式

Private Const MAX_IP = 255
Private Type IPINFO
dwAddr As Long
dwIndex As Long
dwMask As Long
dwBCastAddr As Long
dwReasmSize As Long
unused1 As Integer
unused2 As Integer
End Type
Private Type MIB_IPADDRTABLE
dEntrys As Long
mIPInfo(MAX_IP) As IPINFO
End Type
Private Type IP_Array
mBuffer As MIB_IPADDRTABLE
BufferLen As Long
End Type
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function GetIpAddrTable Lib "IPHlpApi" (pIPAdrTable As Byte, pdwSize As Long, ByVal Sort As Long) As Long
Dim strIP As String


Private Function ConvertAddressToString(longAddr As Long) As String
Dim myByte(3) As Byte
Dim Cnt As Long
CopyMemory myByte(0), longAddr, 4
For Cnt = 0 To 3
ConvertAddressToString = ConvertAddressToString + CStr(myByte(Cnt)) + "."
Next Cnt
ConvertAddressToString = Left$(ConvertAddressToString, Len(ConvertAddressToString) - 1)
End Function

Public Sub Start()
Dim Ret As Long, Tel As Long
Dim bBytes() As Byte
Dim Listing As MIB_IPADDRTABLE
On Error GoTo END1
GetIpAddrTable ByVal 0&, Ret, True
If Ret <= 0 Then Exit Sub
ReDim bBytes(0 To Ret - 1) As Byte
GetIpAddrTable bBytes(0), Ret, False
CopyMemory Listing.dEntrys, bBytes(0), 4
strIP = "你机子上有 " & Listing.dEntrys & " 个 IP 地址。" & vbCrLf
strIP = strIP & "------------------------------------------------" & vbCrLf & vbCrLf
For Tel = 0 To Listing.dEntrys - 1
CopyMemory Listing.mIPInfo(Tel), bBytes(4 + (Tel * Len(Listing.mIPInfo(0)))), Len(Listing.mIPInfo(Tel))
strIP = strIP & "IP 地址 : " & ConvertAddressToString(Listing.mIPInfo(Tel).dwAddr) & vbCrLf
strIP = strIP & "子网掩码 : " & ConvertAddressToString(Listing.mIPInfo(Tel).dwMask) & vbCrLf
strIP = strIP & "广播地址 : " & ConvertAddressToString(Listing.mIPInfo(Tel).dwBCastAddr) & vbCrLf
strIP = strIP & "------------------------------------------------" & vbCrLf
Next
Exit Sub
END1:
MsgBox "ERROR"
End Sub

Private Sub Form_Load()
Start
MsgBox strIP

End Sub
回复
发动态
发帖子
VB基础类
创建于2007-09-28

7451

社区成员

VB 基础类
申请成为版主
社区公告
暂无公告