哈哈 那还不简单。你放个socket控件,里面就有直接获取地址的属性。

解决方案 »

  1.   

    ' Script to retrieve the current IP Address on the first network card.
    '
    ' (c) 2002 A.J. Elsinga
    '      [email protected]
    '
    '      version 1.0
     ' ************************************************************
    ' ***           Start of functions and procedures          ***
    ' ************************************************************Function GetIPAddress
    ' This function retrieves the IP Address from the registry
    ' It gets it from the CurrentControlSet, so even when using DHCP 
    ' it returns the correct IP Address' Declare variables   Dim key
       Dim cTempIPAddress
       Dim cIPAddress
       dim cIPAddressKey
       Set oSh = CreateObject("WScript.Shell")   cInterfacesKey="HKLM\SYSTEM\CurrentControlSet\Services\TCPIP\Parameters\Interfaces\"
       cNICSearch="HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\1\ServiceName"
    ' First check which network card to use
       cNicServiceName=oSh.RegRead(cNICSearch)' Now read the IP Address from that card
       cIPAddressKey=cInterfaceskey + cNicServiceName+"\IPAddress"
       cTempIPAddress=oSh.RegRead (cIPAddresskey)
     
    ' Split the items in the var tempIPAddress to the octets in array IPAddress
       cIPAddress=split (cTempIPAddress(0),".",4)
     
    ' the IP addresss is now readable from ipaddress
    ' for example, to read the first octet, use: FirstOctet=IPAddress(0)
     
       GetIPAddress=cIPAddress
    End FunctionFunction GetIPOctet (nOctet)
    ' This function retrieves a given octet out of the IP Address
        Dim IPAddress
       
        IPAddress=GetIPAddress
        GetIPOctet=IPAddress(nOctet-1)
    End Function
      

  2.   

    实例如下:
    '窗体需要以下控件:一个textbox (Text1, Multiline=True),一个command button (Command1),一个名为Module1模块
    '将以下代码放在窗体代码中
    Private Sub Command1_Click()
        Module1.Start
    End Sub'将以下代码放在模块中Const MAX_IP = 5   '假设机器中最多有五个网卡Type IPINFO
         dwAddr As Long   ' IP address
        dwIndex As Long '  interface index
        dwMask As Long ' subnet mask
        dwBCastAddr As Long ' broadcast address
        dwReasmSize  As Long ' assembly size
        unused1 As Integer ' not currently used
        unused2 As Integer '; not currently used
    End TypeType MIB_IPADDRTABLE
        dEntrys As Long   'number of entries in the table
        mIPInfo(MAX_IP) As IPINFO  'array of IP address entries
    End TypeType IP_Array
        mBuffer As MIB_IPADDRTABLE
        BufferLen As Long
    End TypePublic Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    Public Declare Function GetIpAddrTable Lib "IPHlpApi" (pIPAdrTable As Byte, pdwSize As Long, ByVal Sort As Long) As Long
    Sub main()
    Form1.Show
    End Sub'converts a Long  to a string
    Public 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 FunctionPublic Sub Start()
    Dim Ret As Long, Tel As Long
    Dim bBytes() As Byte
    Dim Listing As MIB_IPADDRTABLEForm1.Text1 = ""On Error GoTo END1
        GetIpAddrTable ByVal 0&, Ret, True    If Ret <= 0 Then Exit Sub
        ReDim bBytes(0 To Ret - 1) As Byte
        'retrieve the data
        GetIpAddrTable bBytes(0), Ret, False
          
        'Get the first 4 bytes to get the entry's.. ip installed
        CopyMemory Listing.dEntrys, bBytes(0), 4
        'MsgBox "IP's found : " & Listing.dEntrys    => Founded ip installed on your PC..
        Form1.Text1 = Listing.dEntrys & "   IP addresses found on your PC !!" & vbCrLf
        Form1.Text1 = Form1.Text1 & "----------------------------------------" & vbCrLf
        For Tel = 0 To Listing.dEntrys - 1
            'Copy whole structure to Listing..
           ' MsgBox bBytes(tel) & "."
            CopyMemory Listing.mIPInfo(Tel), bBytes(4 + (Tel * Len(Listing.mIPInfo(0)))), Len(Listing.mIPInfo(Tel))
             Form1.Text1 = Form1.Text1 & "IP address                   : " & ConvertAddressToString(Listing.mIPInfo(Tel).dwAddr) & vbCrLf
             Form1.Text1 = Form1.Text1 & "IP Subnetmask            : " & ConvertAddressToString(Listing.mIPInfo(Tel).dwMask) & vbCrLf
             Form1.Text1 = Form1.Text1 & "BroadCast IP address  : " & ConvertAddressToString(Listing.mIPInfo(Tel).dwBCastAddr) & vbCrLf
             Form1.Text1 = Form1.Text1 & "**************************************" & vbCrLf
        Next'MsgBox ConvertAddressToString(Listing.mIPInfo(1).dwAddr)
    Exit Sub
    END1:
    MsgBox "ERROR"
    see it?
      

  3.   

    列出本级所有ip'This project requires the following components:
    ' - a form (Form1) with a textbox (Text1, Multiline=True)
    '   and a command button (Command1)
    ' - a module (Module1)'in Form1:
    Private Sub Command1_Click()
        Module1.Start
    End Sub'In Module1:'******************************************************************
    'Created By Verburgh Peter.
    ' 07-23-2001
    [email protected]
    '-------------------------------------
    'With this small application , you can detect the IP's installed on your computer,
    'including subnet mask , BroadcastAddr..
    '
    'I've wrote this because i've a programm that uses the winsock control, but,
    'if you have multiple ip's  installed on your pc , you could get by using the Listen
    ' method the wrong ip ...
    'Because Winsock.Localip => detects the default ip installed on your PC ,
    ' and in most of the cases it could be the LAN (nic) not the WAN (nic)
    'So then you have to use the Bind function ,to bind to your right ip..
    'but how do you know & find that ip ?
    'you can find it now by this appl.. it check's in the api.. IP Table..
    '******************************************************************
    Const MAX_IP = 5   'To make a buffer... i dont think you have more than 5 ip on your pc..Type IPINFO
         dwAddr As Long   ' IP address
        dwIndex As Long '  interface index
        dwMask As Long ' subnet mask
        dwBCastAddr As Long ' broadcast address
        dwReasmSize  As Long ' assembly size
        unused1 As Integer ' not currently used
        unused2 As Integer '; not currently used
    End TypeType MIB_IPADDRTABLE
        dEntrys As Long   'number of entries in the table
        mIPInfo(MAX_IP) As IPINFO  'array of IP address entries
    End TypeType IP_Array
        mBuffer As MIB_IPADDRTABLE
        BufferLen As Long
    End TypePublic Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    Public Declare Function GetIpAddrTable Lib "IPHlpApi" (pIPAdrTable As Byte, pdwSize As Long, ByVal Sort As Long) As Long
    Sub main()
    Form1.Show
    End Sub'converts a Long  to a string
    Public 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 FunctionPublic Sub Start()
    Dim Ret As Long, Tel As Long
    Dim bBytes() As Byte
    Dim Listing As MIB_IPADDRTABLEForm1.Text1 = ""On Error GoTo END1
        GetIpAddrTable ByVal 0&, Ret, True    If Ret <= 0 Then Exit Sub
        ReDim bBytes(0 To Ret - 1) As Byte
        'retrieve the data
        GetIpAddrTable bBytes(0), Ret, False
          
        'Get the first 4 bytes to get the entry's.. ip installed
        CopyMemory Listing.dEntrys, bBytes(0), 4
        'MsgBox "IP's found : " & Listing.dEntrys    => Founded ip installed on your PC..
        Form1.Text1 = Listing.dEntrys & "   IP addresses found on your PC !!" & vbCrLf
        Form1.Text1 = Form1.Text1 & "----------------------------------------" & vbCrLf
        For Tel = 0 To Listing.dEntrys - 1
            'Copy whole structure to Listing..
           ' MsgBox bBytes(tel) & "."
            CopyMemory Listing.mIPInfo(Tel), bBytes(4 + (Tel * Len(Listing.mIPInfo(0)))), Len(Listing.mIPInfo(Tel))
             Form1.Text1 = Form1.Text1 & "IP address                   : " & ConvertAddressToString(Listing.mIPInfo(Tel).dwAddr) & vbCrLf
             Form1.Text1 = Form1.Text1 & "IP Subnetmask            : " & ConvertAddressToString(Listing.mIPInfo(Tel).dwMask) & vbCrLf
             Form1.Text1 = Form1.Text1 & "BroadCast IP address  : " & ConvertAddressToString(Listing.mIPInfo(Tel).dwBCastAddr) & vbCrLf
             Form1.Text1 = Form1.Text1 & "**************************************" & vbCrLf
        Next'MsgBox ConvertAddressToString(Listing.mIPInfo(1).dwAddr)
    Exit Sub
    END1:
    MsgBox "ERROR"
    End Sub
      

  4.   

    上面老兄太麻烦了吧加入microsoft winsock control 6.0Private Sub Command1_Click()
    MsgBox Winsock1.LocalIP
    End Sub这不就好了吗
      

  5.   

    microsoft winsock 
    MsgBox Winsock1.LocalIP
      

  6.   

    上面的,您要先看过人家文章里说的,当机器上有二块以上网卡时,一块LAN一块WAN,通常WINSOCK取得的是LAN中的,而这不是我们所想要的。
    所以WINSOCK.LOCALIP返回来的是局网上的,非我们想要的。
      

  7.   

    两个RAS API函数,如何转换,就看各位的VB功底了(这里不仅仅可以得到静态IP,还可以得到动态IP),本人有一段VC实现的获取动态拨号IP的代码,如有需要可以索取.
    RasEnumConnections
    The RasEnumConnections function lists all active RAS connections. It returns each connection's handle and phone-book entry name. DWORD RasEnumConnections(
      LPRASCONN lprasconn,   // buffer to receive connections data
      LPDWORD lpcb,          // size in bytes of buffer
      LPDWORD lpcConnections // number of connections written to buffer
    );
    Parameters
    lprasconn 
    [in, out] Pointer to a buffer that receives, on output, an array of RASCONN structures, one for each RAS connection. 
    On input, an application must set the dwSize member of the first RASCONN structure in the buffer to sizeof(RASCONN) in order to identify the version of the structure being passed. lpcb 
    [in, out] Pointer to a variable that, on input, contains the size, in bytes, of the buffer specified by lprasconn. 
    On output, the function sets this variable to the number of bytes required to enumerate the RAS connections. lpcConnections 
    [out] Pointer to a variable that receives the number of RASCONN structures written to the buffer specified by lprasconn. 
    Return Values
    If the function succeeds, the return value is zero.If the function fails, the return value is either a nonzero error value listed in the RAS header file or ERROR_BUFFER_TOO_SMALL or ERROR_NOT_ENOUGH_MEMORY. Res
    If a connection was made without specifying a phone-book entry name, the information returned for that connection gives the connection phone number preceded by ".".The following sample code enumerates the current RAS connection. This code assumes that, at most, only one connection is currently active. Note that the code sets the dwSize member of the RASCONN structure to specify the version of the structure.RASCONN * lpRasConn;
    DWORD     lpcb;
    DWORD     lpcConnections;lpRasConn = (LPRASCONN) GlobalAlloc(GPTR, sizeof(RASCONN));
    lpRasConn->dwSize = sizeof(RASCONN);
    lpcb = sizeof(RASCONN);
     
    nRet = RasEnumConnections(lpRasConn, &lpcb, &lpcConnections);
    if (nRet != 0)
    {
        printf("RasEnumConnections failed: Error = %d", nRet);
    }
    else
    {
        printf("The following RAS connections are currently active\n\n");
        for (i = 0; i < lpcConnections; i++)
        {
            printf("Entry name: %s\n", lpRasConn->szEntryName);
            lpRasConn++;
        }
    }Windows 95/98/Me: RasEnumConnectionsW is supported by the Microsoft Layer for Unicode. To use this, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.Requirements 
      Windows NT/2000/XP: Included in Windows NT 3.1 and later.
      Windows 95/98/Me: Included in Windows 95 and later.
      Header: Declared in Ras.h.
      Library: Use Rasapi32.lib.
      Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000/XP. Also supported by Microsoft Layer for Unicode.RasGetProjectionInfo
    The RasGetProjectionInfo function obtains information about a remote access projection operation for a specified remote access component protocol.DWORD RasGetProjectionInfo(
      HRASCONN hrasconn,            // handle that specifies
                                    //  remote access connection of interest
      RASPROJECTION rasprojection,  // specifies type of
                                    //  projection information to obtain
      LPVOID lpprojection,          // points to buffer that
                                    //  receives projection information
      LPDWORD lpcb                  // points to variable that
                                    //  specifies buffer size
    );
    Parameters
    hrasconn 
    [in] Handle to the remote access connection of interest. An application obtains a RAS connection handle from the RasDial or RasEnumConnections function. 
    rasprojection 
    [in] Specifies the RASPROJECTION enumerated type value that identifies the protocol of interest. 
    Windows XP or later: The RASP_Amb and RASP_PppNbf values are not supported. Windows 2000 64-bit Edition: The RASP_PppIpx value is not supported. lpprojection 
    [out] Pointer to a buffer that receives the information specified by the rasprojection parameter. The information will be in a structure appropriate to the rasprojection value. rasprojection value Data structure 
    RASP_Amb RASAMB 
    RASP_PppCcp RASPPPCCP 
    RASP_PppIp RASPPPIP  
    RASP_PppIpx RASPPPIPX 
    RASP_PppLcp RASPPPLCP 
    RASP_PppNbf RASPPPNBF 
    RASP_Slip RASPSLIP  
    Windows XP or later: The RASAMB and RASPPPNBF structures are not supported. Windows 2000 64-bit Edition: The RASPPPIPX structure is not supported. lpcb 
    [in, out] Pointer to a variable that, on input, specifies the size in bytes of the buffer pointed to by lpprojection. 
    On output, this variable receives the size of the buffer needed to contain the specified projection information. Return Values
    If the function succeeds, the return value is zero.If the function fails, the return value is an error code. The function may return a nonzero RAS error code, or one of the following error codes.Value Meaning 
    ERROR_BUFFER_TOO_SMALL The buffer pointed to by lpprojection is not large enough to contain the requested information. 
    ERROR_INVALID_HANDLE The hrasconn parameter is not a valid handle. 
    ERROR_INVALID_PARAMETER One of the parameters is invalid. 
    ERROR_INVALID_SIZE The dwSize member of the structure pointed to by lpprojection specifies an invalid size. 
    ERROR_PROTOCOL_NOT_CONFIGURED The control protocol for which information was requested neither succeeded nor failed, because the connection's phone-book entry did not require that an attempt to negotiate the protocol be made. This is a RAS error code. 
    Res
    Remote access projection is the process whereby a remote access server and a remote client negotiate network protocol-specific information. A remote access server uses this network protocol-specific information to represent a remote client on the network. Windows NT/2000/XP: Remote access projection information is not available until the operating system has executed the RasDial RASCS_Projected state on the remote access connection. If RasGetProjectionInfo is called prior to the RASCS_Projected state, it returns ERROR_PROJECTION_NOT_COMPLETE. Windows 95/98/ME: Windows 95 Dial-Up Networking does not support the RASCS_Projected state. The projection phase may be done during the RASCS_Authenticate state. If the authentication is successful, the connection operation proceeds to the RASCS_Authenticated state, and projection information is available for successfully configured protocols. If RasGetProjectionInfo is called prior to the RASCS_Authenticated state, it returns ERROR_PROTOCOL_NOT_CONFIGURED.Windows XP or later: Windows XP and later operating systems do not support the NetBEUI protocol or authentication message blocks (AMB).64-bit Windows 2000/XP: The 64-bit edition of Windows 2000 does not support the IPX protocol.Requirements 
      Windows NT/2000/XP: Included in Windows NT 3.1 and later.
      Windows 95/98/Me: Included in Windows 95 and later.
      Header: Declared in Ras.h.
      Library: Use Rasapi32.lib.
      Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000/XP.
      

  8.   

    你计算机的注册表中就有.调用API函数,直接读取就可以了.
      

  9.   

    放个winsock控件 winsock.localip返回的值就是本地IP