Mastodon
Programmierung

Getting the Local IP Address

> How can you get the IP address for the local computer?
> Your help is appreciated…

The following function will give you the IP address. You will have to
include Winsock.pas

[code lang=”delphi”]function GetIPAddress : string;
var
Sdata : TWSAData;
ErrorCode: Integer;
HEnt: PHostEnt;
IPAddr : TInAddr;
CName : PChar;
iSize : longword;
begin
isize := MAX_COMPUTERNAME_LENGTH;

GetMem(CName, iSize);

Try
GetComputerName(CName, iSize);

ErrorCode := WSAStartup($0101, SData);

if ErrorCode = 0 then
begin
Try
HEnt := GetHostbyName(CName);

if HEnt <> nil then
begin
with IPAddr, HEnt^ do
begin
S_un_b.s_b1 := h_addr^[0];
S_un_b.s_b2 := h_addr^[1];
S_un_b.s_b3 := h_addr^[2];
S_un_b.s_b4 := h_addr^[3];
end;
Result := inet_ntoa(IPAddr);
end;
Finally
ErrorCode := WSACleanup;
end;
end;
Finally
FreeMem(CName);
end;
end;
[/code]
regards
Stephan Marais

[tags]Delphi, Network[/tags]

0 Kommentare zu “Getting the Local IP Address

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht.