原贴
http://topic.csdn.net/u/20090603/15/a1967885-e1da-44cc-8c1f-705d507ab22a.html
63楼.
利用这个就可以吧域名转换成ip,用楼主方法判断是否可以ping通,这样就完美了Private Sub Command5_Click()
  Dim k As Integer
  Dim ceshi As String
  Dim baidu As String
  
  baidu = "www.baidu.com"
  ceshi = GetIPByName("www.baidu.com")
  Text2.Text = ceshi
End Sub各种试 ceshi = GetIPByName("baidu.com")
....
ceshi = GetIPByName(baidu)
...........
   0 0    怎么都不行.
运行到 
hostent_addr = gethostbyname(name)
的时候
hostent_addr  都等于 0
那括号里到底该是啥.?
要有 http:// ? 要有 www. ?  都不用 ? 可以用表达式么 ? 等等   ........

解决方案 »

  1.   

    gethostbyname用于域名解析,比如:xxx.xxx.com
      

  2.   

    哦,想起来了,应该是查询DNS什么的,把域名翻译为IP地址
      

  3.   


    可我试过一些我用浏览器可以打开的网页 
    hostent_addr 
    还是等于 0 
      

  4.   

    附代码Private Type HOSTENT
    hname As Long
    hAliases As Long
    hAddrType As Integer
    hLength As Integer
    hAddrList As Long
    End Type
    Private Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal _
    hostname$) As Long
    Private Declare Sub RtlMoveMemory Lib "kernel32" (hpvDest As Any, _
    ByVal hpvSource&, ByVal cbCopy&)
    Public Function GetIPByName(name As String) As String
    Dim hostent_addr As Long
    Dim host As HOSTENT
    Dim hostip_addr As Long
    Dim temp_ip_address() As Byte
    Dim i As Integer
    Dim ip_address As String
    GetIPByName = ""
    hostent_addr = gethostbyname(name)If hostent_addr = 0 Then
    Exit Function
    End IfRtlMoveMemory host, hostent_addr, LenB(host)
    RtlMoveMemory hostip_addr, host.hAddrList, 4ReDim temp_ip_address(1 To host.hLength)
    RtlMoveMemory temp_ip_address(1), hostip_addr, host.hLengthFor i = 1 To host.hLength
    ip_address = ip_address & temp_ip_address(i) & "."
    Next
    ip_address = Mid$(ip_address, 1, Len(ip_address) - 1)GetIPByName = ip_addressEnd Function
      

  5.   

    gethostbyname返回一个HOSTENT结构的指针,通过Copymemory得到IP
      

  6.   

    没有初始化WINSOCK。
    Option ExplicitPrivate Type WSADATA
        wVersion As Integer
        wHighVersion As Integer
        szDescription(255) As Byte
        szSystemstatus(127) As Byte
        wMaxSockets As Long
        wMaxUDPDG As Long
        dwVendorInfo As Long
    End Type
    Private Type hostent
        hName As Long
        hAliases As Long
        hAddrType As Integer
        hLength As Integer
        hAddrList As Long
    End Type
    Private Declare Function WSAStartup Lib "ws2_32.dll" (ByVal wVersion As Long, lpWSAD As WSADATA) As Long
    Private Declare Function WSACleanup Lib "ws2_32.dll" () As LongPrivate Const INADDR_NONE = -1
    Private Declare Function gethostname Lib "ws2_32.dll" (ByVal szHost As String, ByVal dwHostLen As Long) As Long
    Private Declare Function gethostbyname Lib "ws2_32.dll" (ByVal hostname As String) As Long
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    Private Declare Function inet_addr Lib "ws2_32.dll" (ByVal cp As String) As LongSub main()
        Dim WSAD As WSADATA
        Dim lngVersionRequested As Long
        
        '初始化SOCKET函数库
        lngVersionRequested = &H202
        WSAStartup lngVersionRequested, WSAD
        
        Debug.Print DomainToIp("www.163.com")
        
        WSACleanup
    End SubPrivate Function DomainToIp(ByVal strHost As String) As String
        Dim lpHostent As Long
        Dim lpIPList As Long
        Dim lngIP As Long
        Dim udtHostent As hostent
        Dim bytIP(3) As Byte
        
        lngIP = INADDR_NONE
        strHost = Trim(strHost)
        If Len(strHost) = 0 Then Exit Function    '判断参数strHost是否为IP地址
        lngIP = inet_addr(strHost)
        If lngIP = INADDR_NONE Then
            '判断参数strHost是否为域名
            lpHostent = gethostbyname(strHost & vbNullChar) '根据域名获得IP
            If lpHostent = 0 Then Exit Function
            CopyMemory udtHostent, ByVal lpHostent, LenB(udtHostent)
            CopyMemory lpIPList, ByVal udtHostent.hAddrList, 4
            CopyMemory lngIP, ByVal lpIPList, 4 '当域名有多个IP地址时,只取第一个IP
        End If
        
        '转换IP地址为字符串
        CopyMemory bytIP(0), lngIP, 4
        DomainToIp = bytIP(0) & "." & bytIP(1) & "." & bytIP(2) & "." & bytIP(3)
    End Function
      

  7.   

    lz的hostent_addr=0我想很可能是socket没有初始化造成的,socket使用前必须初始化,程序退出时必须清除。参考以下代码:窗体的代码:Option ExplicitPrivate Sub Command1_Click()
    Dim sip As Stringsip = getip("www.google.com")
    End SubPrivate Sub Form_Load()
    SocketsInitialize
    End SubPrivate Sub Form_Unload(Cancel As Integer)
    SocketsCleanup
    End SubPrivate Sub SocketsCleanup()
      Dim lReturn As Long
        
      lReturn = WSACleanup()
        
      If lReturn <> 0 Then
          MsgBox "Socket错误 " & Trim$(Str$(lReturn)) & " occurred in Cleanup "
          End
      End If
    End SubPrivate Function hibyte(ByVal wParam As Integer)    '注释:获得整数的高位
      hibyte = wParam And &H100 And &HFF&
    End FunctionPrivate Function lobyte(ByVal wParam As Integer)    '注释:获得整数的低位
      lobyte = wParam And &HFF&
    End Function
    Private Function SocketsInitialize()
      Dim WSAD As WSADATA
      Dim iReturn As Integer
      Dim sLowByte As String, sHighByte As String, sMsg As String
        
      iReturn = WSAStartup(WS_VERSION_REQD, WSAD)
        
      If iReturn <> 0 Then
          MsgBox "Winsock.dll 没有反应."
          End
      End If
        
      If lobyte(WSAD.wversion) < WS_VERSION_MAJOR Or (lobyte(WSAD.wversion) = WS_VERSION_MAJOR And hibyte(WSAD.wversion) < WS_VERSION_MINOR) Then
          sHighByte = Trim$(Str$(hibyte(WSAD.wversion)))
          sLowByte = Trim$(Str$(lobyte(WSAD.wversion)))
          sMsg = "Windows Sockets版本 " & sLowByte & "." & sHighByte
          sMsg = sMsg & " 不被winsock.dll支持 "
          MsgBox sMsg
          End
      End If
        
      If WSAD.iMaxSockets < MIN_SOCKETS_REQD Then
          sMsg = "这个系统需要的最少Sockets数为 "
          sMsg = sMsg & Trim$(Str$(MIN_SOCKETS_REQD))
          MsgBox sMsg
          End
      End If
        
    End Function
    Private Function getip(name As String) As String
      Dim hostent_addr As Long
      Dim host As HOSTENT
      Dim hostip_addr As Long
      Dim temp_ip_address() As Byte
      Dim i As Integer
      Dim ip_address As String
        
      hostent_addr = gethostbyname(name)
        
      If hostent_addr = 0 Then
          getip = ""                    '注释:主机名不能被解释
          Exit Function
      End If
        
      RtlMoveMemory host, hostent_addr, LenB(host)
      RtlMoveMemory hostip_addr, host.hAddrList, 4
        
      ReDim temp_ip_address(1 To host.hLength)
      RtlMoveMemory temp_ip_address(1), hostip_addr, host.hLength
        
      For i = 1 To host.hLength
          ip_address = ip_address & temp_ip_address(i) & "."
      Next
      ip_address = Mid$(ip_address, 1, Len(ip_address) - 1)
        
      getip = ip_addressEnd Function
    模块的代码:Option ExplicitPublic Const WS_VERSION_REQD = &H101
    Public Const WS_VERSION_MAJOR = WS_VERSION_REQD And &H100& And &HFF&
    Public Const WS_VERSION_MINOR = WS_VERSION_REQD And &HFF&
    Public Const MIN_SOCKETS_REQD = 1
      
    Public Const SOCKET_ERROR = -1
    Public Const WSADescription_Len = 256
    Public Const WSASYS_Status_Len = 128Public Type HOSTENT
      hname As Long
      hAliases As Long
      hAddrType As Integer
      hLength As Integer
      hAddrList As Long
    End TypePublic Type WSADATA
      wversion As Integer
      wHighVersion As Integer
      szDescription(0 To WSADescription_Len) As Byte
      szSystemStatus(0 To WSASYS_Status_Len) As Byte
      iMaxSockets As Integer
      iMaxUdpDg As Integer
      lpszVendorInfo As Long
    End Type
    Public Declare Function gethostbyaddr Lib "WSOCK32.DLL" (addr As Any, ByVal _
    byteslen As Integer, addrtype As Integer) As Long
    Public Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Long
    Public Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal _
            wVersionRequired&, lpWSAData As WSADATA) As Long
    Public Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
    Public Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal _
            hostname$) As Long
    Public Declare Sub RtlMoveMemory Lib "kernel32" (hpvDest As Any, _
            ByVal hpvSource&, ByVal cbCopy&)
      

  8.   

    gethostbyname()传入的参数为主机名或者说网址/.刚刚理解了.呵呵.