C
czhfy
Unregistered / Unconfirmed
GUEST, unregistred user!
查了一晚上资料,终于发现了iphlpapi中的GetIfEntry函数,代码也基本完成,可现在却无法调用成功,返回的值不是NO_ERROR而是ERROR_INVALID_DATA(13),所以判断的结果不正确。请教大家我这儿使用的哪儿有问题?谢谢!程序里其实有些东西我也不太明白,请各位指教。
附我的程序:
unit main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TIPMainForm = class(TForm)
Button1: TButton;
info: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure GetMIBIFRow;
end;
var
IPMainForm: TIPMainForm;
implementation
{$R *.dfm}
Const
MAX_INTERFACE_NAME_LEN = 256; { mrapi.h }
MAXLEN_PHYSADDR = 8; { iprtrmib.h }
MAXLEN_IFDESCR = 256;
MIB_IF_OPER_STATUS_NON_OPERATIONAL = 0;
{$EXTERNALSYM MIB_IF_OPER_STATUS_NON_OPERATIONAL}
MIB_IF_OPER_STATUS_UNREACHABLE = 1;
{$EXTERNALSYM MIB_IF_OPER_STATUS_UNREACHABLE}
MIB_IF_OPER_STATUS_DISCONNECTED = 2;
{$EXTERNALSYM MIB_IF_OPER_STATUS_DISCONNECTED}
MIB_IF_OPER_STATUS_CONNECTING = 3;
{$EXTERNALSYM MIB_IF_OPER_STATUS_CONNECTING}
MIB_IF_OPER_STATUS_CONNECTED = 4;
{$EXTERNALSYM MIB_IF_OPER_STATUS_CONNECTED}
MIB_IF_OPER_STATUS_OPERATIONAL = 5;
{$EXTERNALSYM MIB_IF_OPER_STATUS_OPERATIONAL}
Type
PMIBIFRow = ^TMIBIFRow;
TMIBIFRow = Record {MIB_IFROW}
wszName: array [0..MAX_INTERFACE_NAME_LEN] of WideChar;
dwIndex: Longword;
dwType: Longword;
dwMtu: Longword;
dwSpeed: Longword;
dwPhysAddrLen: Longword;
bPhysAddr: array [0..MAXLEN_PHYSADDR - 1] of byte;
dwAdminStatus: Longword;
dwOperStatus: Longword;
dwLastChange: Longword;
dwInOctets: Longword;
dwInUcastPkts: Longword;
dwInNUcastPkts: Longword;
dwInDiscards: Longword;
dwInErrors: Longword;
dwInUnknownProtos: Longword;
dwOutOctets: Longword;
dwOutUcastPkts: Longword;
dwOutNUcastPkts: Longword;
dwOutDiscards: Longword;
dwOutErrors: Longword;
dwOutQLen: Longword;
dwDescrLen: Longword;
bDescr: array [0..MAXLEN_IFDESCR - 1] of byte;
end;
function GetIfEntry(IE: PMIBIFRow): integer;
StdCall; External 'iphlpapi.dll' Name 'GetIfEntry';
procedure TIPMainForm.Button1Click(Sender: TObject);
begin
GetMIBIFRow;
end;
procedure TIPMainForm.GetMIBIFRow;
var
IR: PMIBIFRow;
Size: integer;
Res: integer;
s: string;
begin
Size := 40960;
GetMem(IR, Size);
Res := GetIfEntry(IR);
if (Res <> NO_ERROR) then
begin
SetLastError(Res);
RaiseLastWin32Error;
end;
with info do
begin
Clear;
case IR^.dwOperStatus of
MIB_IF_OPER_STATUS_NON_OPERATIONAL: s := 'LAN adapter has been disabled, for example because of an address conflict.';
MIB_IF_OPER_STATUS_UNREACHABLE: s := 'WAN adapter that is not connected.';
MIB_IF_OPER_STATUS_DISCONNECTED: s := 'For LAN adapters: network cable disconnected. For WAN adapters: no carrier.';
MIB_IF_OPER_STATUS_CONNECTING: s := 'WAN adapter that is in the process of connecting.';
MIB_IF_OPER_STATUS_CONNECTED: s := 'WAN adapter that is connected to a remote peer.';
MIB_IF_OPER_STATUS_OPERATIONAL: s := 'Default status for LAN adapters';
else s := '';
end;
Lines.Add('本地连接:' + s);
end;
FreeMem(IR);
end;
end.
附我的程序:
unit main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TIPMainForm = class(TForm)
Button1: TButton;
info: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure GetMIBIFRow;
end;
var
IPMainForm: TIPMainForm;
implementation
{$R *.dfm}
Const
MAX_INTERFACE_NAME_LEN = 256; { mrapi.h }
MAXLEN_PHYSADDR = 8; { iprtrmib.h }
MAXLEN_IFDESCR = 256;
MIB_IF_OPER_STATUS_NON_OPERATIONAL = 0;
{$EXTERNALSYM MIB_IF_OPER_STATUS_NON_OPERATIONAL}
MIB_IF_OPER_STATUS_UNREACHABLE = 1;
{$EXTERNALSYM MIB_IF_OPER_STATUS_UNREACHABLE}
MIB_IF_OPER_STATUS_DISCONNECTED = 2;
{$EXTERNALSYM MIB_IF_OPER_STATUS_DISCONNECTED}
MIB_IF_OPER_STATUS_CONNECTING = 3;
{$EXTERNALSYM MIB_IF_OPER_STATUS_CONNECTING}
MIB_IF_OPER_STATUS_CONNECTED = 4;
{$EXTERNALSYM MIB_IF_OPER_STATUS_CONNECTED}
MIB_IF_OPER_STATUS_OPERATIONAL = 5;
{$EXTERNALSYM MIB_IF_OPER_STATUS_OPERATIONAL}
Type
PMIBIFRow = ^TMIBIFRow;
TMIBIFRow = Record {MIB_IFROW}
wszName: array [0..MAX_INTERFACE_NAME_LEN] of WideChar;
dwIndex: Longword;
dwType: Longword;
dwMtu: Longword;
dwSpeed: Longword;
dwPhysAddrLen: Longword;
bPhysAddr: array [0..MAXLEN_PHYSADDR - 1] of byte;
dwAdminStatus: Longword;
dwOperStatus: Longword;
dwLastChange: Longword;
dwInOctets: Longword;
dwInUcastPkts: Longword;
dwInNUcastPkts: Longword;
dwInDiscards: Longword;
dwInErrors: Longword;
dwInUnknownProtos: Longword;
dwOutOctets: Longword;
dwOutUcastPkts: Longword;
dwOutNUcastPkts: Longword;
dwOutDiscards: Longword;
dwOutErrors: Longword;
dwOutQLen: Longword;
dwDescrLen: Longword;
bDescr: array [0..MAXLEN_IFDESCR - 1] of byte;
end;
function GetIfEntry(IE: PMIBIFRow): integer;
StdCall; External 'iphlpapi.dll' Name 'GetIfEntry';
procedure TIPMainForm.Button1Click(Sender: TObject);
begin
GetMIBIFRow;
end;
procedure TIPMainForm.GetMIBIFRow;
var
IR: PMIBIFRow;
Size: integer;
Res: integer;
s: string;
begin
Size := 40960;
GetMem(IR, Size);
Res := GetIfEntry(IR);
if (Res <> NO_ERROR) then
begin
SetLastError(Res);
RaiseLastWin32Error;
end;
with info do
begin
Clear;
case IR^.dwOperStatus of
MIB_IF_OPER_STATUS_NON_OPERATIONAL: s := 'LAN adapter has been disabled, for example because of an address conflict.';
MIB_IF_OPER_STATUS_UNREACHABLE: s := 'WAN adapter that is not connected.';
MIB_IF_OPER_STATUS_DISCONNECTED: s := 'For LAN adapters: network cable disconnected. For WAN adapters: no carrier.';
MIB_IF_OPER_STATUS_CONNECTING: s := 'WAN adapter that is in the process of connecting.';
MIB_IF_OPER_STATUS_CONNECTED: s := 'WAN adapter that is connected to a remote peer.';
MIB_IF_OPER_STATUS_OPERATIONAL: s := 'Default status for LAN adapters';
else s := '';
end;
Lines.Add('本地连接:' + s);
end;
FreeMem(IR);
end;
end.