本机IP怎么获得,特别是多网卡的情况下。多个IP都要查出来。

解决方案 »

  1.   

    //uses中加winsock
    procedure TForm1.Button1Click(Sender: TObject);
    var
     Ip:string;
     Ipstr:string;
     buffer:array[1..32] of char;
     i:integer;
     WSData:TWSAData;
     Host:PHostEnt;
    begin
    if WSAstartup(2,WSData)<>0 then  //为程序使用WS2_32.DLL初始化
      begin
        showmessage('WS2_32.DLL初始化失败!');
        halt;
      end;
    try
    if gethostname(@buffer[1],32)<>0 then
      begin
        showmessage('没有得到主机名!');
        halt;
      end;except
      showmessage('没有成功返回主机名');
      halt;
    end;
      Host:=gethostbyname(@buffer[1]);
      if Host=nil then
       begin
        showmessage('IP地址为空!');
        halt;
       end
        else
          begin
            edit2.text:=host.h_name;
            edit3.text:=chr(host.h_addrtype+64);
            for i:=1 to 4 do
              begin
               Ip:=inttostr(Ord(Host.h_addr^[i-1]));
               showmessage('分段Ip地址为:'+Ip);
               Ipstr:=Ipstr+Ip;
               if i<4 then
                 Ipstr:=Ipstr+'.'
                 else
                   edit1.text:=Ipstr;
              end;
          end;
    end;end.
    //WSAstartup在使用gethostname,gethostbyname前
    //一定不要忘了初始化WS2_32.DLL
      

  2.   

    use winsock;
    function LocalIP1 : string;
    type
        TaPInAddr = array [0..10] of PInAddr;
        PaPInAddr = ^TaPInAddr;
    var
        phe  : PHostEnt;
        pptr : PaPInAddr;
        Buffer : array [0..63] of char;
        I    : Integer;
        GInitData      : TWSADATA;begin
        WSAStartup($101, GInitData);
        Result := '';
        GetHostName(Buffer, SizeOf(Buffer));
        phe :=GetHostByName(buffer);
        if phe = nil then Exit;
        pptr := PaPInAddr(Phe^.h_addr_list);
        I := 0;
        while pptr^[I] <> nil do begin
          result:=StrPas(inet_ntoa(pptr^[I]^));
          Inc(I);
        end;
        WSACleanup;
    end;
      

  3.   

    没那么多复杂吧
    function getRemoteIP:string;//获取远程IP地址
    var
      wsdata:Twsadata;
      PCName : array[0..128] of char;
    begin
      Wsastartup($101,wsdata);
      GetHostName(@PCName,128);
      result := iNet_ntoa(PInAddr(GetHostByName(@PCName)^.h_addr_list^)^);
      WSACleanup;
    end;