Getting local IP address in Delphi

Possible duplicate:
Delphi, How to get all local IP addresses?

What is the easiest and fastest way to get the local IP address of a machine in Delphi 2009 without using third-party components? Thanks.

+3
source share
1 answer

From: http://www.scalabium.com/faq/dct0037.htm

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Winsock;

Function GetIPAddress():String;
type
  pu_long = ^u_long;
var
  varTWSAData : TWSAData;
  varPHostEnt : PHostEnt;
  varTInAddr : TInAddr;
  namebuf : Array[0..255] of char;
begin
  If WSAStartup($101,varTWSAData) <> 0 Then
  Result := 'No. IP Address'
  Else Begin
    gethostname(namebuf,sizeof(namebuf));
    varPHostEnt := gethostbyname(namebuf);
    varTInAddr.S_addr := u_long(pu_long(varPHostEnt^.h_addr_list^)^);
    Result := 'IP Address: '+inet_ntoa(varTInAddr);
  End;
  WSACleanup;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1.Caption := GetIPAddress;
end;

end.
+10
source

Source: https://habr.com/ru/post/1712857/


All Articles