If you ever needed your own IP address then here it is the code. Please note that in certain situations you may have more than 1 IP address.
uses
Winsock;
...
function ReadIPs: TStrings;
type
TaPInAddr = array[0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
Buffer: array[0..63] of Char;
iI: Integer;
PPtr: PaPInAddr;
pHE: PHostEnt;
GInitData: TWSAData;
begin
WSAStartup($101, GInitData);
Result := TStringList.Create;
Result.Clear;
GetHostName(Buffer, SizeOf(Buffer));
pHEe := GetHostByName(buffer);
if pHE = nil then
Exit;
PPtr := PaPInAddr(pHE^.H_Addr_List);
iI := 0;
while pPtr^[iI] <> nil do begin
Result.Add(Inet_NToA(PPtr^[iI]^));
Inc(iI);
end;
WSACleanup;
end;
...
Memo1.Lines := ReadIPs;
Tags: borland, code, Delphi, gethostbyname, inet, internet, lan, my, network, nil, own ip ess, source, source code, tcp/ip, tip, trick, wan, winsock, wlan, wsastartup
Merlin’s Delphi Forge
Leave a comment