A
aizuodream
Unregistered / Unconfirmed
GUEST, unregistred user!
我想获取机器的MAC,IP,子网掩码,网关等信息.
通过IP Helper 的函数GetAdaptersInfo等来实现的.代码具体如下:
IP,子网掩码,MAC都可以正常获取,但是网关地址则不行.
我通过单步调试,发现一个很奇怪的问题.
就是:虽然IP,网关地址的类型都是一样的,为:16个字节的字符型数组
但是表示的方法不一样,IP地址的字符是从前面开始填,后面用#0补;而gateway则先用#0填,后面才是真正网关的地址.也正是如此,网关总是读不出来,变成空字符串了.这个问题的原因是什么,该怎么来解决,我用了很多字符串处理函数,如trim好像不行.
例如: IP为: ('2','1','0','.','3','8','.','2','1','9','.','2','2','8',#0,#0);
网关地址gateway为#0,#0,#0,#0,'2','1','0','.','3','8','2','1','9','1');
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
const
MAX_ADAPTER_NAME_LENGTH= 256;
MAX_ADAPTER_DESCRIPTION_LENGTH = 128;
MAX_ADAPTER_ADDRESS_LENGTH = 8;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
Type
TIPAddressString = Array[0..4*4-1] of Char;
PIPAddrString = ^TIPAddrString;
TIPAddrString = Record
Next : PIPAddrString;
IPAddress : TIPAddressString;
IPMask : TIPAddressString;
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;
var
Form1: TForm1;
gatewaystr:string;
strlen:integer;
implementation
{$R *.dfm}
Function GetAdaptersInfo(AI : PIPAdapterInfo; Var BufLen : Integer) : Integer;
StdCall; External 'iphlpapi.dll' Name 'GetAdaptersInfo';
Function MACToStr(ByteArr : PByte; Len : Integer) : String;
Begin
Result := '';
While (Len > 0) do Begin
Result := Result+IntToHex(ByteArr^,2)+'-';
ByteArr := Pointer(Integer(ByteArr)+SizeOf(Byte));
Dec(Len);
End;
SetLength(Result,Length(Result)-1); { remove last dash }
end;
Function GetAddrString(Addr : PIPAddrString) : String;
Begin
Result := '';
While (Addr <> nil) do Begin
Result := Result+'A: '+Addr^.IPAddress+' M: '+Addr^.IPMask+#13;
Addr := Addr^.Next;
End;
End;
procedure TForm1.Button1Click(Sender: TObject);
var
AI,Work : PIPAdapterInfo;
Size : Integer;
Res : Integer;
begin
Size := 5120;
GetMem(AI,Size);
work:=ai;
Res := GetAdaptersInfo(AI,Size);
If (Res <> ERROR_SUCCESS) Then Begin
SetLastError(Res);
RaiseLastWin32Error;
End;
memo1.Lines.Add ('Adapter address: '+MACToStr(@Work^.Address,Work^.AddressLength));
repeat
memo1.Lines.add(' IP addresses: '+GetAddrString(@Work^.IPAddressList));
//就是下面这个gatewaystr网关的值总是为空!!!!
gatewaystr:=work^.GatewayList.IPAddress;
memo1.Lines.Add('gateway address:'+gatewaystr);
work:=work^.Next ;
until (work=nil);
end;
end.
通过IP Helper 的函数GetAdaptersInfo等来实现的.代码具体如下:
IP,子网掩码,MAC都可以正常获取,但是网关地址则不行.
我通过单步调试,发现一个很奇怪的问题.
就是:虽然IP,网关地址的类型都是一样的,为:16个字节的字符型数组
但是表示的方法不一样,IP地址的字符是从前面开始填,后面用#0补;而gateway则先用#0填,后面才是真正网关的地址.也正是如此,网关总是读不出来,变成空字符串了.这个问题的原因是什么,该怎么来解决,我用了很多字符串处理函数,如trim好像不行.
例如: IP为: ('2','1','0','.','3','8','.','2','1','9','.','2','2','8',#0,#0);
网关地址gateway为#0,#0,#0,#0,'2','1','0','.','3','8','2','1','9','1');
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
const
MAX_ADAPTER_NAME_LENGTH= 256;
MAX_ADAPTER_DESCRIPTION_LENGTH = 128;
MAX_ADAPTER_ADDRESS_LENGTH = 8;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
Type
TIPAddressString = Array[0..4*4-1] of Char;
PIPAddrString = ^TIPAddrString;
TIPAddrString = Record
Next : PIPAddrString;
IPAddress : TIPAddressString;
IPMask : TIPAddressString;
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;
var
Form1: TForm1;
gatewaystr:string;
strlen:integer;
implementation
{$R *.dfm}
Function GetAdaptersInfo(AI : PIPAdapterInfo; Var BufLen : Integer) : Integer;
StdCall; External 'iphlpapi.dll' Name 'GetAdaptersInfo';
Function MACToStr(ByteArr : PByte; Len : Integer) : String;
Begin
Result := '';
While (Len > 0) do Begin
Result := Result+IntToHex(ByteArr^,2)+'-';
ByteArr := Pointer(Integer(ByteArr)+SizeOf(Byte));
Dec(Len);
End;
SetLength(Result,Length(Result)-1); { remove last dash }
end;
Function GetAddrString(Addr : PIPAddrString) : String;
Begin
Result := '';
While (Addr <> nil) do Begin
Result := Result+'A: '+Addr^.IPAddress+' M: '+Addr^.IPMask+#13;
Addr := Addr^.Next;
End;
End;
procedure TForm1.Button1Click(Sender: TObject);
var
AI,Work : PIPAdapterInfo;
Size : Integer;
Res : Integer;
begin
Size := 5120;
GetMem(AI,Size);
work:=ai;
Res := GetAdaptersInfo(AI,Size);
If (Res <> ERROR_SUCCESS) Then Begin
SetLastError(Res);
RaiseLastWin32Error;
End;
memo1.Lines.Add ('Adapter address: '+MACToStr(@Work^.Address,Work^.AddressLength));
repeat
memo1.Lines.add(' IP addresses: '+GetAddrString(@Work^.IPAddressList));
//就是下面这个gatewaystr网关的值总是为空!!!!
gatewaystr:=work^.GatewayList.IPAddress;
memo1.Lines.Add('gateway address:'+gatewaystr);
work:=work^.Next ;
until (work=nil);
end;
end.