Fórum Pegar Ip do Servidor da Rede. #218818

06/03/2004

0

ola pessoal estou com o seguinte codigo:

procedure TForm1.Button1Click(Sender: TObject); begin button1.Enabled := false; Label1.Caption := ´ ´ ´+IP+´ ´´; label1.Align:=alBottom; end; Function TForm1.IP:string; //--> Declarar a ´Winsock´ na clausula uses da unit Var WSAData: TWSAData; HostEnt: PHostEnt; Name:string; begin WSAStartup(2, WSAData); SetLength(Name, 255); Gethostname(PChar(Name), 255); SetLength(Name, StrLen(PChar(Name))); HostEnt := gethostbyname(PChar(Name)); with HostEnt^ do begin Result := Format(´¬d.¬d.¬d.¬d´,[Byte(h_addr^[0]),Byte(h_addr^[1]),Byte(h_addr^[2]),Byte(h_addr^[3])]); end; WSACleanup; end;



esse codigo me retorna o numero de ip da minha maquina precisa que retornasse o ip do servidor da minha rede.

aguem pode ajudar...??


Zacca

Zacca

Responder

Posts

06/03/2004

Vinicius2k

Colega,

uma pequena alteração :
function GetIP(Host: String = ´´): String;
var WSAData: TWSAData;
    HostEnt: PHostEnt;
    Name: String;
begin
 try
  WSAStartup(2, WSAData);
  SetLength(Name, 255);
  if Host = ´´ then Gethostname(PChar(Name), 255)
   else Name:= Host;
  SetLength(Name, StrLen(PChar(Name)));
  HostEnt := gethostbyname(PChar(Name));
  with HostEnt^ do
   try
    Result:= Format(´¬d.¬d.¬d.¬d´,[Byte(h_addr^[0]),Byte(h_addr^[1]),Byte(h_addr^[2]),Byte(h_addr^[3])]);
   except
    Result:= ´Host desconhecido.´;
   end;
 finally
  WSACleanup;
 end;
end;


para usar : [b:50f967babc]GetIP;[/b:50f967babc] retorna o seu IP ou [b:50f967babc]GetIP(´www.clubedelphi.com.br´);[/b:50f967babc] retorna 200.154.146.64

espero ter ajudado...

T+


Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar