以下代码返回计算机的第一个TCP/IP连接的IP地址。
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();
要获得所有TCP/IP网络连接的IP地址(如果有多个),我需要进行哪些修改?
我确实在这篇相关文章中偶然发现了这一点:
Get information about the installed network adapters
它似乎使用了一种不同的技术,使用Windows API“GetAdaptersInfo”。。。这是走的路吗?