indy里面还有个函数
procedure TIdStackWindows.PopulateLocalAddresses;
type
TaPInAddr = Array[0..250] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
i: integer;
AHost: PHostEnt;
PAdrPtr: PaPInAddr;
begin
FLocalAddresses.Clear ;
AHost := GetHostByName(PChar(WSGetHostName));
if AHost = nil then
begin
CheckForSocketError(SOCKET_ERROR);
end
else
begin
PAdrPtr := PAPInAddr(AHost^.h_address_list);
i := 0;
while PAdrPtr^[i] <> nil do
begin
FLocalAddresses.Add(TInAddrToString(PAdrPtr^[I]^));
Inc(I);
end;
end;
end;
这是那个函数的原代码。是不是很简单,你可以直接调用就可以了。
Parameters
pIpAddrTable
[out] Pointer to a buffer that receives the interface–to–IP address mapping table as a MIB_IPADDRTABLE structure.
pdwSize
[in, out] On input, specifies the size of the buffer pointed to by the pIpAddrTable parameter.
On output, if the buffer is not large enough to hold the returned mapping table, the function sets this parameter equal to the required buffer size.
bOrder
[in] Specifies whether the returned mapping table should be sorted in ascending order by IP address. If this parameter is TRUE, the table is sorted.
Return Values
If the function succeeds, the return value is NO_ERROR.
If the function fails, the return value is one of the following error codes.
Return Code Description
ERROR_INSUFFICIENT_BUFFER The buffer pointed to by the pIpAddrTable parameter is not large enough. The required size is returned in the DWORD variable pointed to by the pdwSize parameter.
ERROR_INVALID_PARAMETER The pdwSize parameter is NULL, or GetIpAddrTable is unable to write to the memory pointed to by the pdwSize parameter.
ERROR_NOT_SUPPORTED This function is not supported on the operating system in use on the local system.
Other Use FormatMessage to obtain the message string for the returned error.
function LIP : 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^[I] <> nil do begin
result:=StrPas(inet_ntoa(pptr^[I]^));
Inc(I);
end;
WSACleanup;
end;