阅读:4653回复:14
多块网卡,程序中如何识别各自的网络连接状态?
计算机上有多块网卡,如何在程序中识别判断哪些网络连接是成功建立的,哪些是断开的。就象2000任务栏里出现的网络连接小图标一样,不正常是显示红叉,正常就显示两台电脑的样子。
查询了一下IP Helper API函数,发现象GetAdapterInfo()之类的函数都是得到网卡的属性信息,至于连接状态却没有反映。 不知哪位高手能指点迷津。 |
|
沙发#
发布于:2002-10-08 10:02
我也想知道,哪位高手来解答一下?
|
|
板凳#
发布于:2002-10-08 14:26
有没有其他API函数,不一定是IP Helper里面的
|
|
地板#
发布于:2002-10-08 14:36
在驱动里面可以用OID Query查询网卡的状态。
|
|
地下室#
发布于:2002-10-08 14:40
你所说的是tcp或者udp之类的连接吗?
那与网卡有什么关系? 如果是socket建的连接,用tdi就可以得到! |
|
5楼#
发布于:2002-10-08 16:33
其实DDK中有套API专门用于检测设备驱动的状态的,他们以SetupDi-开头,你慢慢研究研究吧,还带了例子呢~~
|
|
6楼#
发布于:2002-10-10 19:51
GetIfEntry,看看这个函数
|
|
7楼#
发布于:2002-10-10 20:08
GetIfEntry
是你想要的吗? |
|
8楼#
发布于:2002-10-16 09:18
GetIfEntry()怎么用呢,查了一下MSDN,发现没有详细的例子,只有使用说明,能否给个例子。
|
|
9楼#
发布于:2002-10-16 10:07
MSDN可能着不到,你到PlatForm SDK找吧。
|
|
10楼#
发布于:2002-10-16 10:08
GetIfEntry
The GetIfEntry function retrieves information for the specified interface on the local computer. DWORD GetIfEntry( PMIB_IFROW pIfRow // pointer to interface entry ); Parameters pIfRow [in, out] Pointer to a MIB_IFROW structure that, on successful return, receives information for an interface on the local computer. On input, set the dwIndex member of MIB_IFROW to the index of the interface for which to retrieve information. Return Values If the function succeeds, the return value is NO_ERROR. If the function fails, use FormatMessage to obtain the message string for the returned error. Requirements Windows NT/2000/XP: Included in Windows NT 4.0 SP4 and later. Windows 95/98/Me: Included in Windows 98 and later. Header: Declared in Iphlpapi.h. Library: Use Iphlpapi.lib. See Also IP Helper Start Page, IP Helper Function Reference, GetNumberOfInterfaces, MIB_IFROW Platform SDK Release: November 2001 What did you think of this topic? Let us know. Order a Platform SDK CD Online (U.S/Canada) (International) Requirements Windows NT/2000/XP: Included in Windows NT 4.0 SP4 and later. Windows 95/98/Me: Included in Windows 98 and later. Header: Declared in Iphlpapi.h. Library: Use Iphlpapi.lib. See Also IP Helper Start Page, IP Helper Function Reference, GetNumberOfInterfaces, MIB_IFROW |
|
11楼#
发布于:2002-10-16 10:08
MIB_IFROW
The MIB_IFROW structure stores information about a particular interface. typedef struct _MIB_IFROW { WCHAR wszName[MAX_INTERFACE_NAME_LEN]; DWORD dwIndex; // index of the interface DWORD dwType; // type of interface DWORD dwMtu; // max transmission unit DWORD dwSpeed; // speed of the interface DWORD dwPhysAddrLen; // length of physical address BYTE bPhysAddr[MAXLEN_PHYSADDR]; // physical address of adapter DWORD dwAdminStatus; // administrative status DWORD dwOperStatus; // operational status DWORD dwLastChange; // last time operational status changed DWORD dwInOctets; // octets received DWORD dwInUcastPkts; // unicast packets received DWORD dwInNUcastPkts; // non-unicast packets received DWORD dwInDiscards; // received packets discarded DWORD dwInErrors; // erroneous packets received DWORD dwInUnknownProtos; // unknown protocol packets received DWORD dwOutOctets; // octets sent DWORD dwOutUcastPkts; // unicast packets sent DWORD dwOutNUcastPkts; // non-unicast packets sent DWORD dwOutDiscards; // outgoing packets discarded DWORD dwOutErrors; // erroneous packets sent DWORD dwOutQLen; // output queue length DWORD dwDescrLen; // length of bDescr member BYTE bDescr[MAXLEN_IFDESCR]; // interface description } MIB_IFROW,*PMIB_IFROW; Members wszName[MAX_INTERFACE_NAME_LEN] Pointer to a Unicode string that contains the name of the interface. dwIndex Specifies the index that identifies the interface. dwType Specifies the type of interface. This member can be one of the following values. MIB_IF_TYPE_OTHER MIB_IF_TYPE_ETHERNET MIB_IF_TYPE_TOKENRING MIB_IF_TYPE_FDDI MIB_IF_TYPE_PPP MIB_IF_TYPE_LOOPBACK MIB_IF_TYPE_SLIP dwMtu Specifies the Maximum Transmission Unit (MTU). dwSpeed Specifies the speed of the interface in bits per second. dwPhysAddrLen Specifies the length of the physical address specified by the bPhysAddr member. bPhysAddr[MAXLEN_PHYSADDR] Specifies the physical address of the adapter for this interface. dwAdminStatus Specifies the interface is administratively enabled or disabled. dwOperStatus Specifies the operational status of the interface. This member can be one of the following values. Value Meaning MIB_IF_OPER_STATUS_NON_OPERATIONAL LAN adapter has been disabled, for example because of an address conflict. MIB_IF_OPER_STATUS_UNREACHABLE WAN adapter that is not connected. MIB_IF_OPER_STATUS_DISCONNECTED For LAN adapters: network cable disconnected. For WAN adapters: no carrier. MIB_IF_OPER_STATUS_CONNECTING WAN adapter that is in the process of connecting. MIB_IF_OPER_STATUS_CONNECTED WAN adapter that is connected to a remote peer. MIB_IF_OPER_STATUS_OPERATIONAL Default status for LAN adapters dwLastChange Specifies the last time the operational status changed. dwInOctets Specifies the number of octets of data received through this interface. dwInUcastPkts Specifies the number of unicast packets received through this interface. dwInNUcastPkts Specifies the number of non-unicast packets received through this interface. Broadcast and multicast packets are included. dwInDiscards Specifies the number of incoming packets that were discarded even though they did not have errors. dwInErrors Specifies the number of incoming packets that were discarded because of errors. dwInUnknownProtos Specifies the number of incoming packets that were discarded because the protocol was unknown. dwOutOctets Specifies the number of octets of data sent through this interface. dwOutUcastPkts Specifies the number of unicast packets sent through this interface. dwOutNUcastPkts Specifies the number of non-unicast packets sent through this interface. Broadcast and multicast packets are included. dwOutDiscards Specifies the number of outgoing packets that were discarded even though they did not have errors. dwOutErrors Specifies the number of outgoing packets that were discarded because of errors. dwOutQLen Specifies the output queue length. dwDescrLen Specifies the length of the bDescr member. bDescr[MAXLEN_IFDESCR] Contains a description of the interface. Requirements Windows NT/2000/XP: Included in Windows NT 4.0 SP4 and later. Windows 95/98/Me: Included in Windows 98 and later. Header: Declared in Iprtrmib.h. See Also GetIfEntry, MIB_IFSTATUS, MIB_IFTABLE, MPR_INTERFACE_0 Platform SDK Release: November 2001 What did you think of this topic? Let us know. Order a Platform SDK CD Online (U.S/Canada) (International) |
|
12楼#
发布于:2002-10-16 11:29
可用以下三
|
|
|
13楼#
发布于:2002-10-16 17:19
网卡状态改变的时候,会传输相应的oid,与status关键字有关,呵呵,xp就是这样!2000也应该是!
|
|
14楼#
发布于:2002-10-17 17:10
我编程试验了一下GetIfEntry()函数,返回的网卡名称和类型都对,就是反映状态的两个变量始终不变,不管网线拔掉还是插上,dwOperStatus值一直都是1(MIB_IF_OPER_STATUS_UNREACHABLE),
dwAdminStatus也一直是都是1。反过来用SetIfEntry()重新设置这些值好象也不起作用。我用GetAdapterInfo()函数根据返回的IP如果都为“0.0.0.0”,就判断网络没有连接成功,这样在设置IP的情况下好象是对的,但对于自动分配IP地址来说,系统任务栏里明显是未断开,可得到的IP也是“0.0.0.0”。 |
|