// Get host address
char Buffer[64];
HOSTENT FAR *pHost;
CString sIPAddress;
gethostname(Buffer, 64);
pHost = gethostbyname(Buffer);
sIPAddress = inet_ntoa(*(struct in_addr*)pHost->h_addr_list[0]);
// If have many ip address, then
// look h_addr_list[1], etc, until encounter a NULL pointer
取得本地计算机的名字:
Windows 95 API: GetComputerName
题外话:取得本地登录用户名:GetUserName
取得本地ip地址(转贴):
function LocalIP : string;
type
TaPInAddr = array [0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
phe : PHostEnt;
pptr : PaPInAddr;
Buffer : array [0..63] of char;
I : Integer;
GInitData : TWSADATA;
begin
WSAStartup($101, GInitData);
Result := '';
GetHostName(Buffer, SizeOf(Buffer));
phe :=GetHostByName(buffer);
if phe = nil then Exit;
pptr := PaPInAddr(Phe^.h_addr_list);
I := 0;
while pptr^ <> nil do begin
result:=StrPas(inet_ntoa(pptr^^));
Inc(I);
end;
WSACleanup;
end;