判断当前网络状态
const
MAX_INTERFACE_NAME_LEN = 256;
MAXLEN_PHYSADDR = 8;
MAXLEN_IFDESCR = 256;
MAX_ADAPTER_NAME = 128;
MAX_ADAPTER_NAME_LENGTH = 256;
MAX_ADAPTER_DESCRIPTION_LENGTH = 128;
MAX_ADAPTER_ADDRESS_LENGTH = 8;
MIB_IF_OPER_STATUS_NON_OPERATIONAL = 0; // 本地网卡被禁用了
MIB_IF_OPER_STATUS_UNREACHABLE = 1; // 拨号连接适配器没有连接
MIB_IF_OPER_STATUS_DISCONNECTED = 2; // 本地网卡适配器没有连接(比如拔了网线就是返回这个值了)
MIB_IF_OPER_STATUS_CONNECTING = 3; // 拨号适配器正在拨号连接...
MIB_IF_OPER_STATUS_CONNECTED = 4; // 拨号适配器已经连接到WAN...
MIB_IF_OPER_STATUS_OPERATIONAL = 5; // 本地网卡适配器的默认状态
MIB_IF_TYPE_OTHER = 1;
MIB_IF_TYPE_ETHERNET = 6;
MIB_IF_TYPE_TOKENRING = 9;
MIB_IF_TYPE_FDDI = 15;
MIB_IF_TYPE_PPP = 23;
MIB_IF_TYPE_LOOPBACK = 24;
MIB_IF_TYPE_SLIP = 28;
type
_MIB_IFROW = record
wszName: array [0..MAX_INTERFACE_NAME_LEN - 1] of WideChar;
dwIndex: Cardinal;
dwType: Cardinal;
dwMtu: Cardinal;
dwSpeed: Cardinal;
dwPhysAddrLen: Cardinal;
bPhysAddr: array [0..MAXLEN_PHYSADDR - 1] of Byte;
dwAdminStatus: Cardinal;
dwOperStatus: Cardinal;
dwLastChange: Cardinal;
dwInOctets: Cardinal;
dwInUcastPkts: Cardinal;
dwInNUcastPkts: Cardinal;
dwInDiscards: Cardinal;
dwInErrors: Cardinal;
dwInUnknownProtos: Cardinal;
dwOutOctets: Cardinal;
dwOutUcastPkts: Cardinal;
dwOutNUcastPkts: Cardinal;
dwOutDiscards: Cardinal;
dwOutErrors: Cardinal;
dwOutQLen: Cardinal;
dwDescrLen: Cardinal;
bDescr: array [0..MAXLEN_IFDESCR - 1] of Byte;
end;
PMibIfBow = ^TMibIfBow;
TMibIfBow = _MIB_IFROW;
_IP_ADAPTER_INDEX_MAP = record
Index: Cardinal;
Name: array [0..MAX_ADAPTER_NAME - 1] of WideChar;
end;
PIPAdapterIndexMap = ^TIPAdapterIndexMap;
TIPAdapterIndexMap = _IP_ADAPTER_INDEX_MAP;
_IP_INTERFACE_INFO = record
NumAdapters: Cardinal;
Adapter: array [0..0] of TIPAdapterIndexMap;
end;
PIPInterfaceInfo = ^TIPInterfaceInfo;
TIPInterfaceInfo = _IP_INTERFACE_INFO;
PIPAddressString = ^TIPAddressString;
TIPAddressString = record
value: array [0..15] of Char;
end;
PIPMaskString = ^TIPMaskString;
TIPMaskString = TIPAddressString;
PIPAddrString = ^TIPAddrString;
_IP_ADDR_STRING = record
Next: PIPAddrString;
IpAddress: TIPAddressString;
IpMask: TIPMaskString;
Context: Cardinal;
end;
TIPAddrString = _IP_ADDR_STRING;
PIPAdapterInfo = ^TIPAdapterInfo;
_IP_ADAPTER_INFO = record
Next: PIPAdapterInfo;
ComboIndex: Cardinal;
AdapterName: array [0..MAX_ADAPTER_NAME_LENGTH + 3] of Char;
Description: array [0..MAX_ADAPTER_DESCRIPTION_LENGTH + 3] of Char;
AddressLength: UINT;
Address: array [0..MAX_ADAPTER_ADDRESS_LENGTH - 1] of Byte;
Index: Cardinal;
dwType: UINT;
DhcpEnabled: UINT;
CurrentIpAddress: PIPAddrString;
IpAddressList: TIPAddrString;
GatewayList: TIPAddrString;
DhcpServer: TIPAddrString;
HaveWins: Integer;
PrimaryWinsServer: TIPAddrString;
SecondaryWinsServer: TIPAddrString;
LeaseObtained: Cardinal; { time_t }
LeaseExpires: Cardinal; { time_t }
end;
TIPAdapterInfo = _IP_ADAPTER_INFO;
function GetIfEntry(pifRow: PMibIfBow): Cardinal; stdcall; external 'Iphlpapi.dll';
function GetNumberOfInterfaces(var pdwNumIf: Cardinal): Cardinal; stdcall; external 'Iphlpapi.dll';
function GetInterfaceInfo(pIfTable: PIPInterfaceInfo;
var dwOutBufLen: Cardinal): Cardinal; stdcall; external 'Iphlpapi.dll';
function GetAdaptersInfo(pAdapterInfo: PIPAdapterInfo;
var dwOutBufLen: Cardinal): Cardinal; stdcall; external 'Iphlpapi.dll';
procedure CheckResult(RetVal: Integer);
begin
if RetVal <> NO_ERROR then
raise Exception.Create(SysErrorMessage(RetVal));
end;
procedure TForm1.Button2Click(Sender: TObject);
var
Ret, OutBufLen: Cardinal;
P, Buffer: PIPAdapterInfo;
begin
OutBufLen := SizeOf(TIPAdapterInfo);
GetMem(Buffer, OutBufLen);
try
FillChar(Buffer^, OutBufLen, 0);
Ret := GetAdaptersInfo(Buffer, OutBufLen);
Memo1.Lines.Add(SysErrorMessage(Ret));
if Ret <> NO_ERROR then
begin
ReallocMem(Buffer, OutBufLen);
CheckResult(GetAdaptersInfo(Buffer, OutBufLen));
end;
P := Buffer;
while Assigned(P) do
begin
Memo1.Lines.Add(Format('ComboIndex: %d, ' +
'AdapterIndex: %d, dwType: %d, DhcpEnabled: %d, IpAddress: %s, GateWay Address: %s, ' +
'DhcpServer: %s, HaveWins: %d, PrimaryWinServer: %s, SecondaryWinServer: %s',
[P.ComboIndex, P.Index, P.dwType,
P.DhcpEnabled, P^.IpAddressList.IpAddress.value, P^.GatewayList.IpAddress.value,
P.DhcpServer.IpAddress.value, P.HaveWins,
P.PrimaryWinsServer.IpAddress.value, P.SecondaryWinsServer.IpAddress.value]));
P := P^.Next;
end;
finally
FreeMem(Buffer);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
I: Integer;
P: PIPInterfaceInfo;
NumOfInterfaces, InfoSize: Cardinal;
Status: TMibIfBow;
begin
CheckResult(GetNumberOfInterfaces(NumOfInterfaces));
GetMem(P, SizeOf(TIPInterfaceInfo));
try
if GetInterfaceInfo(P, InfoSize) <> NO_ERROR then
begin
ReallocMem(P, InfoSize);
CheckResult(GetInterfaceInfo(P, InfoSize));
end;
for I := 0 to P^.NumAdapters - 1 do
begin
Memo1.Lines.Add(Format('Adapter Index :%d, Name: %s', [
P^.Adapter[I].Index, P^.Adapter[I].Name]));
FillChar(Status, SizeOf(Status), 0);
Status.dwIndex := P^.Adapter[I].Index;
CheckResult(GetIfEntry(@Status));
case Status.dwType of
MIB_IF_TYPE_OTHER: Memo1.Lines.Add('网络类型: Other');
MIB_IF_TYPE_ETHERNET: Memo1.Lines.Add('网络类型: Enternet');
MIB_IF_TYPE_TOKENRING: Memo1.Lines.Add('网络类型: TokenRing');
MIB_IF_TYPE_FDDI: Memo1.Lines.Add('网络类型: FDDI');
MIB_IF_TYPE_PPP: Memo1.Lines.Add('网络类型: PPP');
MIB_IF_TYPE_LOOPBACK: Memo1.Lines.Add('网络类型: LoopBack');
MIB_IF_TYPE_SLIP: Memo1.Lines.Add('网络类型: SLIP');
end;
case Status.dwOperStatus of
MIB_IF_OPER_STATUS_NON_OPERATIONAL: Memo1.Lines.Add('当前网卡适配器被禁用了');
MIB_IF_OPER_STATUS_UNREACHABLE: Memo1.Lines.Add('拨号连接适配器没有连接');
MIB_IF_OPER_STATUS_DISCONNECTED: Memo1.Lines.Add('本地网卡适配器没有连接(比如拔了网线就是返回这个值了');
MIB_IF_OPER_STATUS_CONNECTING: Memo1.Lines.Add('拨号适配器正在拨号连接...');
MIB_IF_OPER_STATUS_CONNECTED: Memo1.Lines.Add('拨号适配器已经连接到WAN...');
MIB_IF_OPER_STATUS_OPERATIONAL: Memo1.Lines.Add('本地网卡适配器的默认状态');
end;
end;
finally
FreeMem(P);
end;
end;