·您的位置: 首页 » 资源教程 » 编程开发 » ASP.NET » 取得本机IP地址的方法两则

取得本机IP地址的方法两则

类别: ASP.NET教程  评论数:0 总得分:0
方法1:(控件法)

新建工程,在工程中加入Winsock控件和一个按钮控件

代码:

Option Explicit

Private Sub Command1_Click()
MsgBox "当前计算机网卡IP:" & Winsock1.LocalIP
End Sub

方法2:(API法)

新建工程,在工程中加入一个按钮控件

代码:

Option Explicit

Private Type WSAData
wVersion As Integer
wHighVersion As Integer
szDescription(0 To 256)
szSystemStatus(0 To 128)
wMaxSockets As Integer
dwVendorInfo As Long
End Type

Private Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal wVersionRequired As Long, lpWSAData As WSAData) As Long
Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
Private Declare Function gethostname Lib "WSOCK32.DLL" (ByVal szHost As String, ByVal dwHostLen As Long) As Long
Private Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal szHost As String) As Long
Private Declare Sub CopyMemoryIP Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)

Private Function GetIPAddress() As String
On Error GoTo Z
Dim wsa As WSAData
Dim rVal As Long
Dim sHost As String * 256
Dim lp As Long
Dim lpa As Long
Dim IpAdr(0 To 3) As Byte
rVal = WSAStartup((2 + &H100), wsa)
If rVal = 0 Then
gethostname sHost, 256
lp = gethostbyname(sHost)
If lp Then
CopyMemoryIP lpa, lp + 16, 4
CopyMemoryIP IpAdr(0), lpa, 4
GetIPAddress = CStr(IpAdr(0)) & "." & CStr(IpAdr(1)) & "." & CStr(IpAdr(2)) & "." & CStr(IpAdr(3))
End If
WSACleanup
End If
Exit Function
Z:
GetIPAddress = ""
End Function

Private Sub Command1_Click()
Dim ret As String
ret = GetIPAddress
If ret <> "" Then
MsgBox "当前计算机网卡IP:" & ret
End If
End Sub





-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1