win获取网卡列表信息的三种方式

一、仅适用于xp以上系统,xp不可用。

        结构体:

typedef struct _MIB_IF_TABLE2 {
    ULONG NumEntries;
    MIB_IF_ROW2 Table[ANY_SIZE];
} MIB_IF_TABLE2, *PMIB_IF_TABLE2;

typedef struct _MIB_IF_ROW2 {
    //
    // Key structure.  Sorted by preference.
    //
    NET_LUID InterfaceLuid;
    NET_IFINDEX InterfaceIndex;

    //
    // Read-Only fields.
    //
    GUID InterfaceGuid;
    WCHAR Alias[IF_MAX_STRING_SIZE + 1];
    WCHAR Description[IF_MAX_STRING_SIZE + 1];
    ULONG PhysicalAddressLength;
    UCHAR PhysicalAddress[IF_MAX_PHYS_ADDRESS_LENGTH];
    UCHAR PermanentPhysicalAddress[IF_MAX_PHYS_ADDRESS_LENGTH];

    ULONG Mtu;
    IFTYPE Type;                // Interface Type.
    TUNNEL_TYPE TunnelType;     // Tunnel Type, if Type = IF_TUNNEL.
    NDIS_MEDIUM MediaType;
    NDIS_PHYSICAL_MEDIUM PhysicalMediumType;
    NET_IF_ACCESS_TYPE AccessType;
    NET_IF_DIRECTION_TYPE DirectionType;
    struct {
        BOOLEAN HardwareInterface : 1;
        BOOLEAN FilterInterface : 1;
        BOOLEAN ConnectorPresent : 1;
        BOOLEAN NotAuthenticated : 1;
        BOOLEAN NotMediaConnected : 1;
        BOOLEAN Paused : 1;
        BOOLEAN LowPower : 1;
        BOOLEAN EndPointInterface : 1;
    } InterfaceAndOperStatusFlags;

    IF_OPER_STATUS OperStatus;
    NET_IF_ADMIN_STATUS AdminStatus;
    NET_IF_MEDIA_CONNECT_STATE MediaConnectState;
    NET_IF_NETWORK_GUID NetworkGuid;
    NET_IF_CONNECTION_TYPE ConnectionType;

    //
    // Statistics.
    //
    ULONG64 TransmitLinkSpeed;
    ULONG64 ReceiveLinkSpeed;

    ULONG64 InOctets;
    ULONG64 InUcastPkts;
    ULONG64 InNUcastPkts;
    ULONG64 InDiscards;
    ULONG64 InErrors;
    ULONG64 InUnknownProtos;
    ULONG64 InUcastOctets;
    ULONG64 InMulticastOctets;
    ULONG64 InBroadcastOctets;
    ULONG64 OutOctets;
    ULONG64 OutUcastPkts;
    ULONG64 OutNUcastPkts;
    ULONG64 OutDiscards;
    ULONG64 OutErrors;
    ULONG64 OutUcastOctets;
    ULONG64 OutMulticastOctets;
    ULONG64 OutBroadcastOctets;
    ULONG64 OutQLen;
} MIB_IF_ROW2, *PMIB_IF_ROW2;

        具体方法:

        PMIB_IF_TABLE2 pMIT2 = NULL;
		HINSTANCE hInst = ::LoadLibrary(TEXT("Iphlpapi.dll"));
		if (hInst)
		{
			//g_log.WriteLogW(__FILE__, __LINE__, L"");
			typedef DWORD(__stdcall* PGetIfTable2)(PMIB_IF_TABLE2*);
			PGetIfTable2 pFunc = (PGetIfTable2)GetProcAddress(hInst, "GetIfTable2");
			if (pFunc(&pMIT2) != NO_ERROR && pMIT2->NumEntries <= 1)
				return ;
		}
		else
			return ;

		for (int i = 0; i < pMIT2->NumEntries; i++)
        {
            // 处理获取到的网卡列表信息
        }

        typedef DWORD(__stdcall* PFreeMibTable)(PMIB_IF_TABLE2);
		PFreeMibTable pFreeFunc = (PFreeMibTable)GetProcAddress(hInst, "FreeMibTable");

		pFreeFunc(pMIT2);
		pMIT2 = NULL;

        实际执行效果:

         

二、适用于xp及以上系统

        结构体:

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;

        具体方法:

        deque<IP_ADAPTER_INFO> theIpAdapterList;
		GetIpAdapterInfoList(theIpAdapterList);
		for (deque<IP_ADAPTER_INFO>::iterator itr = theIpAdapterList.begin(); itr != theIpAdapterList.end(); itr++)
        {
            // 处理获取到的网卡列表信息

        }

        实际执行效果:

        

三、通过gethostbyname获取,只能获取已连接的网卡ip

    WORD w = MAKEWORD(1, 1);
	WSADATA wsaData;
	WSAStartup(w, &wsaData); // 加载套接字库

	struct hostent *host_info = gethostbyname("");
	in_addr PcAddr;
	for (int i=0;;i++)
	{
		char *p = host_info->h_addr_list[i];
		if (NULL==p)
		{
			break;
		}

		memcpy(&(PcAddr.S_un.S_addr),p,host_info->h_length);
		char*szIP = ::inet_ntoa(PcAddr);
		printf("ip地址:%s\n",szIP);

	}

	WSACleanup();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

古道青阳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值