1. Delphi 4 has a TNMSMTP control on the Internet Pallette that returns
your I.P in a string with one call.
2. use Winsock:
a. Start a new application
b. Add the Winsock unit to the uses clause.
c. Replace the form create procedure with the following:
procedure TForm1.FormCreate(Sender: TObject);
var
  WSAData: TWSAData;
  FUnit, Scratch: String;
  HostEnt: PHostEnt;
begin  // Initialise the sockets interface
  WSAStartup($0101, WSAData);  // Set FUnit to the name of the PC you want the IP address of.
  // An empty string means the local machine.
  FUnit := '';  // Get IP address of FUnit
  HostEnt := gethostbyname(PChar(FUnit));  // Was call OK?
  if HostEnt <> nil then begin
    Scratch := IntToStr(ord(HostEnt^.h_addr^[0]));
    Scratch := Scratch + '.' + IntToStr(ord(HostEnt^.h_addr^[1]));
    Scratch := Scratch + '.' + IntToStr(ord(HostEnt^.h_addr^[2]));
    Scratch := Scratch + '.' + IntToStr(ord(HostEnt^.h_addr^[3]));
  end;  //Clean up sockets interface
  WSACleanUp;  // Assign IP address to forms caption
  Caption := Scratch;end;

解决方案 »

  1.   

    function LocalIP : 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; 
    注意,需要use WinSock Unit. 
      

  2.   

    unit MyFastNet;interfaceuses
      Windows, Sysutils, WinSock;function NameToIP(Name: string): string;
    function IPToName(IP: string): string;
    function DllStartUp: Boolean;
    function DllCleanUp: Boolean;implementationfunction DllStartUp: Boolean;
    var
      wVersionRequested: WORD;
      WSADATA: TWSAData;
      err: Integer;
    begin
      Result := True;
      wVersionRequested := MAKEWORD( 2, 0 );
      err := WSAStartup( wVersionRequested, WSADATA );
      if (err<>0)or(LOBYTE(WSADATA.wVersion)<>2)or(HIBYTE(WSADATA.wVersion)<>0) then
      begin
        WSACleanup;
        Result := False;
      end;
    end;function DllCleanUp: Boolean;
    begin
      WSACleanup;
      Result := True;
    end;function IPToName(IP: string): string;
    var
      p: PHostEnt;
      INetAddr: dword;
    begin
      Result := '';
      INetAddr := INet_Addr(PChar(IP));
      p := GetHostByAddr(@InetAddr, Length(IP), PF_Inet);
      if p <> nil then
        Result := p^.h_name;
    end;function NameToIP(Name: string): string;
    var
      HostEnt: PHostEnt;
    begin
      Result := '';
      HostEnt := GetHostByName(PChar(Name));
      if HostEnt <> nil then
      begin
        with HostEnt^ do
          Result := Format('%.3d.%.3d.%.3d.%.3d', [Byte(h_addr^[0]),
            Byte(h_addr^[1]), Byte(h_addr^[2]), Byte(h_addr^[3])]);
      end;
    end;end.uses MyFastNet;TForm1.OnCreate:
    DllStartUp;
    ...TForm1.OnDestroy:
    DllCleanUp;
    ...TForm1.Button1Click:
    NameToIP(Name);
    IPToName(Ip);
    ...
      

  3.   

    如果是Win2k,配置了2个或多个IP,该怎么办呢?