请问如何用delphi 7 实现显示自己的ip地址 最好能提供代码 本人是初学者
请多指教!

解决方案 »

  1.   

    //得到本机机器名、IP地址及其类别// 通过调用Api函数gethostname,gethostbyname,wsastartup
    //uses中加winsock
    //介绍wsadata,phostent msdn
    //另外gethostaddress,
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls,winsock;type
      TForm1 = class(TForm)
        Button1: TButton;
        Edit1: TEdit;
        Edit2: TEdit;
        Edit3: TEdit;
        StaticText1: TStaticText;
        StaticText2: TStaticText;
        StaticText3: TStaticText;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}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.   

    //局域网中通过计算机名得到IP
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls,winsock;type
      TForm1 = class(TForm)
        Button1: TButton;
        Edit1: TEdit;
        Edit2: TEdit;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
    var
      WSAData: TWSAData;
      HostEnt: PHostEnt;
      sComputerName, sIP: string;
    begin
      sComputername:=edit1.text;
      WSAStartup(2, WSAData);
      HostEnt := gethostbyname(PChar(sComputerName));
      if HostEnt <> nil then
      begin
        with HostEnt^ do
          sIP := Format('%d.%d.%d.%d', [Byte(h_addr^[0]), Byte(h_addr^[1]), Byte(h_addr^[2]), Byte(h_addr^[3])]);  end;
      WSACleanup;
      edit2.text:=sIP;
    end;
    end.
    ///////////////////////////////////
      

  3.   

    加上个nmhttp控件showmessage(nmhttp1.LocalIP);
      

  4.   

    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;
      

  5.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, winsock, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
        function GetHostIP(HostName: String): String;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}function Tform1.GetHostIP(HostName: String): String;
    var
       buf:pChar;
       iWsaRet:Integer;
       Data:WSAData;
       hostent:PHostEnt;
    begin
       Result := '';
       iWsaRet := WSAStartup($101,Data);
       if iWsaRet<>0 then
       begin
          ShowMessage('Socket initialize error!');
          Exit;
       end;
       buf := Allocmem(60);
       strcopy(buf,PChar(HostName));
       if Trim(buf)='' then
          gethostname(buf,60);
       hostent := gethostbyname(buf);
       Freemem(buf,60);
       if hostent=nil then
          Exit;
       Result  := inet_ntoa(pinAddr(hostent^.h_addr^)^);
       WSACleanup();
    end;
    end.
    如果hostname为空,则取本机的IP
      

  6.   

    //得到本机的局域网Ip地址function GetLocalIP(var LocalIp: string): Boolean;
    var
        HostEnt: PHostEnt;
        Ip: string;
        addr: pchar;
        Buffer: array [0..63] of char;
        GInitData: TWSADATA;
    begin
      Result := False;
      try
        WSAStartup(2, GInitData);
        GetHostName(Buffer, SizeOf(Buffer));
        HostEnt := GetHostByName(buffer);
        if HostEnt = nil then Exit;
        addr := HostEnt^.h_addr_list^;
        ip := Format('%d.%d.%d.%d', [byte(addr [0]),
              byte (addr [1]), byte (addr [2]), byte (addr [3])]);
        LocalIp := Ip;
        Result := True;
      finally
        WSACleanup;
      end;
    end;