问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

有谁知道在vb中怎样能得到本机的ip地址

发布网友 发布时间:2022-04-27 09:20

我来回答

2个回答

热心网友 时间:2023-10-10 05:02

建议用API函数,以下完整示例:
(其中直接调用GetIPAddress函数即可获取IP)

------------------

Option Explicit

Public Const MIN_SOCKETS_REQD As Long = 1
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 SOCKET_ERROR As Long = -1
Public Const ERROR_SUCCESS As Long = 0
Public Const WSADESCRIPTION_LEN = 257
Public Const WSASYS_STATUS_LEN = 129
Public Const MAX_WSADescription = 256
Public Const MAX_WSASYSStatus = 128
Public 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
Type WSADataInfo
wVersion As Integer
wHighVersion As Integer
szDescription As String * WSADESCRIPTION_LEN
szSystemStatus As String * WSASYS_STATUS_LEN
iMaxSockets As Integer
iMaxUdpDg As Integer
lpVendorInfo As String
End Type
Public Type HOSTENT
hName As Long
hAliases As Long
hAddrType As Integer
hLen As Integer
hAddrList As Long
End Type
Declare Function WSAStartupInfo Lib "WSOCK32" Alias "WSAStartup" (ByVal wVersionRequested As Integer, lpWSADATA As WSADataInfo) As Long
Declare Function WSACleanup Lib "WSOCK32" () As Long
Declare Function WSAGetLastError Lib "WSOCK32" () As Long
Declare Function WSAStartup Lib "WSOCK32" (ByVal wVersionRequired As Long, lpWSADATA As WSAData) As Long
Declare Function gethostname Lib "WSOCK32" (ByVal szHost As String, ByVal dwHostLen As Long) As Long
Declare Function gethostbyname Lib "WSOCK32" (ByVal szHost As String) As Long
Declare Sub CopyMemoryIP Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)

'获取本地IP地址
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
CopyMemoryIP HOST, lpHost, Len(HOST)
CopyMemoryIP dwIPAddr, HOST.hAddrList, 4
ReDim tmpIPAddr(1 To HOST.hLen)
CopyMemoryIP 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 Function
Public Function GetIPHostName() As String
Dim sHostName As String * 256
If Not SocketsInitialize() Then
GetIPHostName = ""
Exit Function
End If
If gethostname(sHostName, 256) = SOCKET_ERROR Then
GetIPHostName = ""
MsgBox "Windows Sockets error " & Str$(WSAGetLastError()) & " has occurred. Unable to successfully get Host Name."
SocketsCleanup
Exit Function
End If
GetIPHostName = Left$(sHostName, InStr(sHostName, Chr(0)) - 1)
SocketsCleanup
End Function
Public 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 Sub
Public 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

热心网友 时间:2023-10-10 05:03

最直观的方法,添加一个winsock,然后MeIp=winsock.LocalIP
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
悲观的意思是什么悲观是什么意思 ...坐立不安的。还总想挠挠手呀、胳膊什么的。这是怎么回事啊?是一种... ...胳膊肘麻,有时会麻到感觉大拇指根部疼,食指也有点... ...入睡感觉左胳膊从手腕到肩膀酥溜溜的酸,睁开眼又好了,什么原因... 胳膊上麻溜溜的 像是有小疙瘩 还很痒 有点硬 少量蔗糖,食盐,食油,食醋分别倒入一定量水中,并用筷子不断搅拌,观察... 将食盐 蔗糖 泥土 植物油与水混合 英文会计的provision对应中文会计的哪个词啊? 双人旁一个直一个心念什么,什么意思 白瓷有隙是成语吗 vb 如何获取 ip? 小老鼠打头来,牛把蹄儿抬,.........十二动物转圈跑的全部歌词 如何在VB中获得用户的IP地址啊,请指教 0 十二生肖歌顺口溜 vb获取当前ip问题 急需《十二生肖歌》(诗) vb6.0 怎样获得IP? 十二生肖歌 老鼠前面走,跟着老黄牛,老虎一声吼,兔子抖三抖,天上龙在... 怎么用VB语句获取本机IP并可直接显示 vb 如何实现自功获取IP地址? 如何用vb编程实现读取本地计算机的IP地址?? 我们该如何优雅的公布自己谈恋爱了? 找男朋友文案 想谈恋爱的简短句子 很想谈恋爱的句子 我想学怎么做酱香饼哪里可以学 想学做酱香饼,哪里有酱香饼学? 一般学做酱香饼学费多少钱 酱香饼怎么做,学做酱香饼,酱香饼制作教程,酱香饼摆摊开店? 自己在家做酱香饼,掌握哪些技巧能把口味做的秒杀路边摊? vb 如何获取外网ip (200分)VB 获取本机IP和重新获取IP问题 数字歌的相关评论 vb能不能获取计算机的ip地址 代码是什么呢 如何用VB6.0获取ip地址并格式化? linux怎么部署springboot 临朐旅游景点 临朐哪个旅游景点好玩 临朐沂山风景区在什么地方,好玩吗? 临朐一日游必去景点 临朐特色美食和景点有哪些? 沂山风景区怎么样?去过的朋友觉得景色如何? 临朐有什么好的旅游景点 潍坊周边旅游景点 维坊有哪些景点 对孩子写新年祝福语 电力实习总结的内容? 电力专业的去找什么工作实习好呢? 地方供电局会收实习生吗? 供电所实习具体干什么?