热情软件屋

 

怎样读取本机在Internet上的IP


编号:QA004637
建立日期: 2002年11月25日 最后修改日期:2003年6月22日
所属类别:

小华:
    操作系统:9x/ME/2k/XP
    编程工具:VB
    问题:我在写一个VB的远程数据库的软件,其中涉及到一个问题:怎样读取本机在Internet上的IP,用传统的方法如SockDLL或WinSock控件只能得到当前本机的第一个IP,如计算机上有两块网卡(如ADSL+LAN)的情况下则只能读取第一个网络设备的当前IP,请指教用那个API及其在VB的用法。谢谢!
    水平: 中级

回答:

    关键是 GetIpAddrTable (注意:Windows 95 没有 IPHlpApi 连接库,可以从 98 复制过去,NT 4 SP4 以前没有,可以从 SP6a 包里复制过去)。
    C 原型:
    DWORD GetIpAddrTable(
     PMIB_IPADDRTABLE pIpAddrTable,
     PULONG pdwSize,
     BOOL bOrder
    );
    参数:
    pIpAddrTable
    [out] Pointer to a buffer that receives the interface–to–IP address mapping table as a MIB_IPADDRTABLE structure.
    pdwSize
    [in, out] On input, specifies the size of the buffer pointed to by the pIpAddrTable parameter.
    On output, if the buffer is not large enough to hold the returned mapping table, the function sets this parameter equal to the required buffer size.
    
    bOrder
    [in] Specifies whether the returned mapping table should be sorted in ascending order by IP address. If this parameter is TRUE, the table is sorted.
    
    VB 声明:
    Declare Function GetIpAddrTable Lib "IPHlpApi" (pIPAdrTable As Byte, pdwSize As Long, ByVal Sort As Long) As Long
    
    

    
    返回值:
    ERROR_INVALID_PARAMETER The pdwSize parameter is NULL, or GetIpAddrTable is unable to write to the memory pointed to by the pdwSize parameter.
    ERROR_INSUFFICIENT_BUFFER The buffer pointed to by the pIpAddrTable parameter is not large enough. The required size is returned in the DWORD variable pointed to by the pdwSize parameter.
    ERROR_NOT_SUPPORTED This function is not supported on the operating system in use on the local system.
    Other Use FormatMessage to obtain the message string for the returned error.
    
    给一段示范程序:
    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 Sub main()
     Start
     MsgBox strIP
    End Sub
    
    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
    
    

    做好了程序别忘了给我看看哦:icediy(at)163.com。
    
    qg163的意见:
    我是用 shell "winipcfg /batch ipcfg.txt",然后分析ipcfg.txt的内容来得到IP的。
    
    dragonIphlpapi.h 和Iphlpapi.lib在哪能下载啊?没这两个文件。还是什么也做不了。
    李海答:如果安装了Visual Studio.NET或更高版本,这两个文件已经包含了。如果使用VC6,可以在Platform SDK中获得(QA002580 "Win32 SDK是怎样一个软件")。

此问题由ipdown.com回答。

 
把这个问题推荐给朋友
   
   
您的意见类别
您的名字
您的电子邮件
您的建议(请尽可能详细)
 
 

版权所有 1997-2008 热情软件屋
如果您有任何建议和意见, 请给我发个电子邮件 askpro@china-askpro.com
Web Designed by ZebraStudio