如题

解决方案 »

  1.   

    uses Winsock;{$R *.DFM}procedure TLogInFrm.GetHostInfo(var Name, Address: string);
    var
      WSAData: TWSAData;
      HostEnt: PHostEnt;
    begin
      { no error checking...}
      WSAStartup(2, WSAData);
      SetLength(Name, 255);
      Gethostname(PChar(Name), 255);
      SetLength(Name, StrLen(PChar(Name)));
      HostEnt := gethostbyname(PChar(Name));
      with HostEnt^  do
        Address := Format('%d.%d.%d.%d',[
          Byte(h_addr^[0]),
          Byte(h_addr^[1]),
          Byte(h_addr^[2]),
          Byte(h_addr^[3])]);
      WSACleanup;
    end;
      

  2.   

    给你一段程序:unit UnitDNS;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls,Winsock;type
      TFormMain = class(TForm)
        Label1: TLabel;
        Label2: TLabel;
        Label3: TLabel;
        EditPresentation: TEdit;
        EditResult: TEdit;
        edtHost: TEdit;
        Button1: TButton;
        procedure FormCreate(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      FormMain: TFormMain;implementation{$R *.DFM}procedure TFormMain.FormCreate(Sender: TObject);
    Var
       WSAData:TWSAData;
    begin
         //initiates use of WS2_32.DLL
         if (WSAStartup(MAKEWORD(2,0),WSAData)<>0) then
            raise Exception.Create('Winsock Version Error');
    end;procedure TFormMain.FormDestroy(Sender: TObject);
    begin
         WSACleanUP;
    end;
    procedure TFormMain.Button1Click(Sender: TObject);
    var
       Host: pHostent;
       ConsultResult:String;
       Err:Integer;
    begin
         Try
            Host := GetHostByName(PChar(EditPresentation.Text));
         Except
            Err:=WSAGetLastError();
            EditResult.Text:='Error Code:'+InttoStr(Err);
            Exit;
         end;
         if Host=NIL then
         begin
            EditResult.Text:='Host does not exist';
            exit;
         end;
         ConsultResult:=Copy(Host.h_addr^,0,4);
         EditResult.Text:=Format('%d.%d.%d.%d',
                                  [ord(ConsultResult[1]),
                                   ord(ConsultResult[2]),
                                   ord(ConsultResult[3]),
                                   ord(ConsultResult[4])]);
         edtHost.Text:=Host.h_name;
    end;end.
      

  3.   

    参考下面的代码:uses Winsock; ... function getIPs: TStrings; 
    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:=TStringList.Create; 
    Result.Clear; 
    GetHostName(Buffer, SizeOf(Buffer)); 
    phe := GetHostByName(buffer); 
    if phe = nil then 
    begin 
    Exit; 
    end; 
    pPtr := PaPInAddr(phe^.h_addr_list); 
    I := 0; 
    while pPtr^[I] <> nil do 
    begin 
    Result.Add(inet_ntoa(pptr^[I]^)); 
    Inc(I); 
    end; 
    WSACleanup; 
    end; 
    procedure TForm1.Button1Click(Sender: TObject); 
    begin 
    memo1.Lines:=GetIps; 
    end;
      

  4.   

    更简单的,Delphi6中的控件IPWatch的属性LocalIP
      

  5.   

    function GetIP: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;