码迷,mamicode.com
首页 > 系统相关 > 详细

获取MAC地址

时间:2017-01-09 18:05:54      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:length   实例代码   mac   main   ddr   com   lis   std   ace   

实例代码:

#include <Windows.h>
#include <iphlpapi.h>
#include <list>
#include <string>
#pragma comment(lib, "Iphlpapi.lib")

std::list<std::string> GetMACs()
{
	std::list<std::string> lst;

	ULONG ulSize = 0;
	::GetAdaptersInfo(nullptr, &ulSize);
	
	if (ulSize == 0) return lst;

	PIP_ADAPTER_INFO pInfo = (PIP_ADAPTER_INFO)malloc(ulSize);
	if (::GetAdaptersInfo(pInfo, &ulSize) != ERROR_SUCCESS)
	{
		free(pInfo);
		return lst;
	}

	PIP_ADAPTER_INFO pNext = pInfo;
	while (pNext != nullptr)
	{
		std::string mac;
		char tmp[10];
		for (int i = 0; i < (int)pNext->AddressLength; i++)
		{
			sprintf_s(tmp, "%02X-", pNext->Address[i]);
			mac += tmp;
		}
		if (!mac.empty())
		{
			mac.pop_back();
			lst.emplace_back(std::move(mac));
		}
		pNext = pNext->Next;
	}

	free(pInfo);

	return lst;
}

int main()
{
	GetMACs();
    return 0;
}

  

获取MAC地址

标签:length   实例代码   mac   main   ddr   com   lis   std   ace   

原文地址:http://www.cnblogs.com/kindly/p/6265909.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!