GetAdapterInfo得到MAC地址精简版C++源码

Iphlpapi.h;ipHlpApi.lib;IPEXPORT.H;IPRTRMIB.H;IPTYPES.H

IP Helper 是一套用于管理本地网络设置的API。使用这一套API可以方便的改变计算机的网络设置或者提取有关的信息。而且它还提供了一种消息机制,能够在本地计算 机的网络设置发生改变时通知应用程序。实际上,它不仅仅能够提取本机的网络设置信息,还能够获得网络上其它计算机的IP使用情况和MAC地址。 -IP Helper is a set of API for the management of the local network settings. Using this set of APIs can easily change the computer' s network settings or extract the relevant information. It also provides a messaging mechanism to notify the application when the change in the local computer' s network settings. In fact, it is not only able to extract the information of the network settings, and also be able to get the IP use other computers on the network and MAC address.

本案例用到的资源,源码:http://download.csdn.net/detail/you_xian/6443819

#include<winsock2.h>
#include<Iphlpapi.h>
#include<stdio.h>

#pragma comment(lib,"Iphlpapi.lib")

int main()
{
	PIP_ADAPTER_INFO pAdapterInfo;
	PIP_ADAPTER_INFO pAdapter = NULL;
	DWORD dwRetVal = 0;
	ULONG ulOutBufLen;
	pAdapterInfo=(PIP_ADAPTER_INFO)malloc(sizeof(IP_ADAPTER_INFO));
	ulOutBufLen = sizeof(IP_ADAPTER_INFO);

	
// 第一次调用GetAdapterInfo获取ulOutBufLen大小
	if (GetAdaptersInfo( pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW)
	{
		free(pAdapterInfo);
		pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen); 
	}

	if ((dwRetVal = GetAdaptersInfo( pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
		pAdapter = pAdapterInfo;
		while (pAdapter) 
		{
			printf("Adapter Name: \t%s\n", pAdapter->AdapterName);
			printf("Adapter Desc: \t%s\n", pAdapter->Description);
			printf("MAC Addr: \t%02x-%02x-%02x-%02x-%02x-%02x\n", 
				pAdapter->Address[0],
				pAdapter->Address[1],
				pAdapter->Address[2],
				pAdapter->Address[3],
				pAdapter->Address[4],
				pAdapter->Address[5]);
			printf("IP Address: \t%s\n", pAdapter->IpAddressList.IpAddress.String);
			printf("IP Mask: \t%s\n", pAdapter->IpAddressList.IpMask.String); 
			printf("Gateway: \t%s\n", pAdapter->GatewayList.IpAddress.String);
			pAdapter = pAdapter->Next;
		}
	}
	else
	{
		printf("Call to GetAdaptersInfo failed.\n");
	}
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

百事悠弦

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

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

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

打赏作者

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

抵扣说明:

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

余额充值