procedure TForm1.ButtonIPClick(Sender: TObject);
  type
    TaPInAddr = Array[0..10] of PInAddr;
    PaPInAddr = ^TaPInAddr;
  var
    phe: PHostEnt;
    pptr: PaPInAddr;
    Buffer: Array[0..63] of Char;
    I: Integer;
    GInitData: TWSAData;
    IP: String;
begin
    Screen.Cursor := crHourGlass;
    try
      WSAStartup($101, GInitData);
      IP:='0.0.0.0';
      GetHostName(Buffer, SizeOf(Buffer));
      phe := GetHostByName(buffer);
      if phe = nil then
      begin
        ShowMessage(IP);
        Exit;
      end;
      pPtr := PaPInAddr(phe^.h_addr_list);
      I := 0;
      while pPtr^[I] <> nil do
       begin
        IP := inet_ntoa(pptr^[I]^);
        Inc(I);
       end;
      WSACleanup;
      ShowMessage(IP);
    finally
      Screen.Cursor := crDefault;
    end;
end;

解决方案 »

  1.   

    通过拨号用 PPP 上网,本身有网卡 NIC。在求 ip 地址时总是只得到一个 ip(实际是NIC
    的 IP),无法得到另一个 ip。最近再试了一下,可以得到两个 ip。
    代码为:
    char szHostName[128];
    CString m_IpAdd1, m_IpAdd2;
    if( gethostname(szHostName, 128) == 0 ) //得到本地机名
    {
     struct hostent * pHost;
     pHost = gethostbyname(szHostName);
     m_IpAdd1 = inet_ntoa(*((struct in_addr*)(pHost->h_addr_list[0])));
     //第一个 ip,为 NIC IP
     if (pHost->h_addr_list[1] != NULL) //没有拨号时当然没有PPP IP
      m_IpAdd2 = inet_ntoa(*((struct in_addr*)(pHost->h_addr_list[1])));
     //第二个 ip,为 PPP IP
    }
      

  2.   

             Dim lpraspppip As RASPPPIP
             lpraspppip.dwSize = 40
             If RasGetProjectionInfo(mhConn, RASP_PppIp, lpraspppip, 40) = 0 Then
                 mRemoteIPAddress = TrimNull(StrConv(lpraspppip.szServerAddress, vbUnicode))
             End If