The following code returns the IP address of the first TCP/IP connection for a machine.
uses WinSock;
// ...
function GetLocalIP() : String;
var
addr: TSockAddrIn;
phe: PHostEnt;
szHostName: array[0..128] of Char;
socketData: TWSADATA;
begin
Result := '127.0.0.1';
// Initialize the socket API
if (WSAStartup($101, socketData) = 0) then
begin
// Get local machine name
if (GetHostName(szHostName, 128) = SOCKET_ERROR) then
Exit;
// Use name to find IP address
phe := GetHostByName(szHostName);
if (phe = nil) then
Exit;
addr.sin_addr.S_addr := Longint(PLongint(phe^.h_addr_list^)^);
// Convert IP address to PChar format
Result := inet_ntoa(addr.sin_addr);
end;
end;
// ...
Label1.Caption := GetLocalIP();
What modifications would I need to make to get the IP address for all TCP/IP network connections (where there was more than one)?
I did stumble across this across this related article: Get information about the installed network adapters which appears to use a different technique using the Windows API "GetAdaptersInfo"...is this the way to go?