GetAdaptersInfo函数问题

Kalivn 2009-07-20 11:53:34
先看一下MSDN-------->

IP_ADAPTER_INFO Structure

The IP_ADAPTER_INFO structure contains information about a particular network adapter on the local computer.


typedef struct _IP_ADAPTER_INFO { struct _IP_ADAPTER_INFO* Next; DWORD ComboIndex; char AdapterName[MAX_ADAPTER_NAME_LENGTH + 4]; char Description[MAX_ADAPTER_DESCRIPTION_LENGTH + 4]; UINT AddressLength; BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH]; DWORD Index; UINT Type; UINT DhcpEnabled; PIP_ADDR_STRING CurrentIpAddress; IP_ADDR_STRING IpAddressList; IP_ADDR_STRING GatewayList; IP_ADDR_STRING DhcpServer; BOOL HaveWins; IP_ADDR_STRING PrimaryWinsServer; IP_ADDR_STRING SecondaryWinsServer; time_t LeaseObtained; time_t LeaseExpires;
} IP_ADAPTER_INFO, *PIP_ADAPTER_INFO;
Members
Next
A pointer to the next adapter in the list of adapters.

ComboIndex
Reserved.

AdapterName
An ANSI character string of the name of the adapter.

Description
An ANSI character string of the description for the adapter.

AddressLength
The length, in bytes, of the hardware address for the adapter.

Address
The hardware address for the adapter represented as a BYTE array.

Index
The adapter index. The adapter index may change when an adapter is disabled and then enabled, or under other circumstances, and should not be considered persistent.

Type
The adapter type. The type must 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
These values are defined in the header file IPIfCons.h.

DhcpEnabled
An option value that specifies whether the dynamic host configuration protocol (DHCP) is enabled for this adapter.

CurrentIpAddress
Reserved.

IpAddressList
The list of IPv4 addresses associated with this adapter represented as a linked list of IP_ADDR_STRING structures. An adapter can have multiple IPv4 addresses assigned to it.

GatewayList
The IPv4 address of the gateway for this adapter represented as a linked list of IP_ADDR_STRING structures. An adapter can have multiple IPv4 gateway addresses assigned to it. This list usually contains a single entry for IPv4 address of the default gateway for this adapter.

DhcpServer
The IPv4 address of the DHCP server for this adapter represented as a linked list of IP_ADDR_STRING structures. This list contains a single entry for IPv4 address of the DHCP server for this adapter. A value of 255.255.255.255 indicates the DHCP server could not be reached, or is in the process of being reached. This member is only valid when the DhcpEnabled member is non-zero.

HaveWins
An option value that specifies whether this adapter uses the Windows Internet Name Service (WINS).

PrimaryWinsServer
The IPv4 address of the primary WINS server represented as a linked list of IP_ADDR_STRING structures. This list contains a single entry for IPv4 address of the primary WINS server for this adapter. This member is only valid when the HaveWins member is TRUE.

SecondaryWinsServer
The IPv4 address of the secondary WINS server represented as a linked list of IP_ADDR_STRING structures. An adapter can have multiple secondary WINS server addresses assigned to it. This member is only valid when the HaveWins member is TRUE.

LeaseObtained
The time when the current DHCP lease was obtained. This member is only valid when the DhcpEnabled member is non-zero.

LeaseExpires
The time when the current DHCP lease expires. This member is only valid when the DhcpEnabled member is non-zero.

Remarks
The IP_ADAPTER_INFO structure is limited to IPv4 information about a particular network adapter on the local computer. The IP_ADAPTER_INFO structure is retrieved by calling the GetAdaptersInfo function.

When using Visual Studio 2005 and later, the time_t datatype defaults to an 8-byte datatype, not the 4-byte datatype used for the LeaseObtained and LeaseExpires members on a 32-bit platform. To properly use the IP_ADAPTER_INFO structure on a 32-bit platform, define _USE_32BIT_TIME_T (use -D _USE_32BIT_TIME_T as an option, for example) when compiling the application to force the time_t datatype to a 4-byte datatype.

For use on Windows XP and later, the IP_ADAPTER_ADDRESSES structure contains both IPv4 and IPv6 information. The GetAdaptersAddresses function retrieves IPv4 and IPv6 adapter information.

我的问题就是Index,怎么做才能在改变也是就MSDN所说的在关闭打开后得到一个正确的值,请高手指点~
...全文
527 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
tsys2000 2010-08-09
  • 打赏
  • 举报
回复
你这个不能获得已禁用的呀
Kalivn 2009-07-22
  • 打赏
  • 举报
回复
搞定,分析下理路留个提示给大家......

定义一个class,定义好所有GetAdapterInfo的数据放进去

class Cnetwork
{
string m_sName;
string m_sDesc;
........
};

通过HeapAlloc分配好初始化内存,然后通过GetAdapterInfo里面的struct _IP_ADAPTER_INFO* Next;
while (pNext)<--是否有网卡结构指针,总是指向下一个网卡的信息,包含第1块网卡
{
count++;//这里是网卡数
pNext = pNext->Next;//指向下个 _IP_ADAPTER_INFO 结构的指针再循环
}
计算网卡数得到总需要分配的内存大小 lpBuffer = count * sizeof(Cnetwork)动态分配内存,并把所有网卡的信息放进内存~~
在主程序也可以用 动态分配内存大小(lpBuffer) / 初始化内存大小sizeof(Cnetwork)得到网卡数量,也可以得到按选择得到你所需要那卡网卡的信息~~:)
udknight 2009-07-21
  • 打赏
  • 举报
回复
写了一堆废话,问题到是一行小字在最下面。真是晕。lz问问题也要有水平啊
udknight 2009-07-21
  • 打赏
  • 举报
回复
禁用的这个实现不了吧。读注册表吧
ArcRain 2009-07-21
  • 打赏
  • 举报
回复
这个函数实现不了也没办法...换其他方法吧...
Kalivn 2009-07-21
  • 打赏
  • 举报
回复
看清楚我问的问题..我问的就是如何在关闭启用的环境下用GetAdaptersInfo得到正常的网卡数,用ICOM可以枚举出当前的网卡关闭或启用,但我只想用这个函数,ICOM又得另写一个类,太烦了

有朋友可以回答吗..
udknight 2009-07-21
  • 打赏
  • 举报
回复
index 是网卡索引号啊,你取出来的是一个PIP_ADAPTER_INFO的链表。不是就一个网卡。你可以试试在机器上面装一个虚拟机,默认的总会取出虚拟机的网卡
Kalivn 2009-07-21
  • 打赏
  • 举报
回复
顶下,谁来回答下该如何解决
ArcRain 2009-07-21
  • 打赏
  • 举报
回复
这个Index的值与多网卡的启用,禁用有关系.
而且这个GetAdaptersInfo函数好象是不能获取到禁用网卡信息的.
Kalivn 2009-07-21
  • 打赏
  • 举报
回复
那有什么好的办法解决这个问题,COM除外,那玩意太烦了- -!
难道就只能用注册表来枚举了?
greatws 2009-07-21
  • 打赏
  • 举报
回复
index就是网卡编号啊,而且如果有网卡被禁用时,这张网卡是无法用GetAdaptersInfo函数获得的,这时其他网卡的Index可能发生改变。

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧