如何获取本机IP。。。?

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

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

谢谢。
...全文
22 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
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

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧