delphi 获取内网所有IP地址


Const
MAX_ADAPTER_NAME_LENGTH    = 256;
MAX_ADAPTER_DESCRIPTION_LENGTH = 128;
MAX_ADAPTER_ADDRESS_LENGTH    = 8;

Type
TIPAddressString = Array[0..4*4-1] of Char;
PIPAddrString = ^TIPAddrString;
TIPAddrString = Record
   Next    : PIPAddrString;
   IPAddress : TIPAddressString;
   IPMask    : TIPAddressString;
   Context   : Integer;
End;

PIPAdapterInfo = ^TIPAdapterInfo;
TIPAdapterInfo = Record { IP_ADAPTER_INFO }
   Next    : PIPAdapterInfo;
   ComboIndex    : Integer;
   AdapterName    : Array[0..MAX_ADAPTER_NAME_LENGTH+3] of Char;
   Description    : Array[0..MAX_ADAPTER_DESCRIPTION_LENGTH+3] of Char;
   AddressLength    : Integer;
   Address    : Array[1..MAX_ADAPTER_ADDRESS_LENGTH] of Byte;
   Index    : Integer;
   _Type    : Integer;
   DHCPEnabled    : Integer;
   CurrentIPAddress    : PIPAddrString;
   IPAddressList    : TIPAddrString;
   GatewayList    : TIPAddrString;
End;

Function GetAdaptersInfo(AI : PIPAdapterInfo; Var BufLen : Integer) : Integer; StdCall; External 'iphlpapi.dll' Name 'GetAdaptersInfo';

function GetLocalIP():string;
  Function GetAddrString(Addr : PIPAddrString) : String;
  Begin
   Result := ''; 
   While (Addr <> nil) do Begin
   Result := Result+Addr^.IPAddress+#13;
   Addr := Addr^.Next;
   End;
  End;
var
  AI,Work : PIPAdapterInfo;
  Size    : Integer;
  Res    : Integer;
  temps   :string;
begin
Size := 5120;
GetMem(AI,Size);
work:=ai;
Res := GetAdaptersInfo(AI,Size);
If (Res <> ERROR_SUCCESS) Then halt;
repeat
   temps:=GetAddrString(@Work^.IPAddressList);
   if temps <> '0.0.0.0'+#13 then result:=result+temps+'/';
   work:=work^.Next ;
until (work=nil);
if copy(result,length(result),length(result)-1) = '/' then delete(result,length(result),length(result)-1);
end;

//showmessage(GetLocalIP);


联系电话:
020-00000000
联系电话:
020-00000000
联系电话:
020-12345678