用户登录  |  用户注册
首 页源码下载网络学院最新源码源码排行屏蔽广告
当前位置:新兴网络 > 网络学院 > 编程开发 > Visual Basic

VB 域名转换IP地址函数

减小字体 增大字体 作者:佚名  来源:本站整理  发布时间:2010-09-06 12:45:24

它可以将一个域名转换成对应的IP地址。
调用示例:

VBScript code复制代码
Private Sub Form_Load()
    MsgBox DomainNameToIP("www.newxing.com")
End Sub

Module:

VBScript code复制代码
Option Explicit
  
Private Type HOSTENT
    hName As Long
    hAliases As Long
    hAddrType As Integer
    hLength As Integer
    hAddrList As Long
End Type
  
Private Type WSADATA
    wversion As Integer
    wHighVersion As Integer
    szDescription(0 To 256) As Byte
    szSystemStatus(0 To 128) As Byte
    iMaxSockets As Integer
    iMaxUdpDg As Integer
    lpszVendorInfo As Long
End Type
  
Private Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Long
Private Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal wVersionRequired As Integer, lpWSAData As WSADATA) As Long
Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
Private Declare Function gethostname Lib "WSOCK32.DLL" (ByVal szHostname As String, ByVal HostLen As Long) As Long
Private Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal szHostname As String) As Long
Private Declare Sub RtlMoveMemory Lib "KERNEL32" (hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)
  
Private Const WS_VERSION_REQD = &H101
  
Public Function DomainNameToIP(URL As String) As String
    InitializeWinSock
    DomainNameToIP = GetAddressByName(URL)
    TerminateWinSock
End Function
  
Private Function GetAddressByName(strHostname As String)
    Dim lngAddr As Long
    Dim udtHost As HOSTENT
    Dim lngIP As Long
    Dim bteTmp() As Byte
    Dim i As Integer
    Dim strIP As String
  
    lngAddr = gethostbyname(strHostname)
  
    If lngAddr = 0 Then
        MsgBox "Kein Host gefunden."
        GetAddressByName = Null
        Exit Function
    End If
  
    RtlMoveMemory udtHost, lngAddr, LenB(udtHost)
    RtlMoveMemory lngIP, udtHost.hAddrList, 4
  
    ReDim bteTmp(1 To udtHost.hLength)
    RtlMoveMemory bteTmp(1), lngIP, udtHost.hLength
    For i = 1 To udtHost.hLength
        strIP = strIP & bteTmp(i) & "."
    Next
    strIP = Mid$(strIP, 1, Len(strIP) - 1)
  
    GetAddressByName = strIP
End Function
  
Private Sub InitializeWinSock()
    Dim udtWSAD As WSADATA
    Dim lngRet As Long
    lngRet = WSAStartup(WS_VERSION_REQD, udtWSAD)
    If lngRet <> 0 Then
        MsgBox "Winsock.dll konnte nicht initialisiert werden."
        End
    End If
End Sub
  
Private Sub TerminateWinSock()
    Dim lngRet As Long
    lngRet = WSACleanup()
    If lngRet <> 0 Then
        MsgBox "Fehler " & lngRet & " beim Beenden von Winsock.dll"
        End
    End If
End Sub

Tags:域名转IP VB

作者:佚名
  • 好的评价 如果您觉得此文章好,就请您
      0%(0)
  • 差的评价 如果您觉得此文章差,就请您
      0%(0)

网络学院评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论