ref: http://www.codeguru.com/cpp/i-n/network/networkinformation/article.php/c5451/Three-ways-to-get-your-MAC-address.htm
Note: this function works for windows 2000/xp or above only... and if there are more than one network cards, this function only get the first one... for example:, if vmare workstation exists, this function will return a mac address for vmnet8 in my machine..
use GetAdaptersInfo instead to retrieve all nic info
#include <string>
#include <windows.h>
#include "boost/format.hpp"
using namespace std;
using boost::format;
using boost::io::str;
string getMac()
{
unsigned char MACData[6];
UUID uuid;
UuidCreateSequential(&uuid);
for(int i = 2; i<8; ++i)
{
MACData[i-2] = uuid.Data4[i];
}
format f("%02X-%02X-%02X-%02X-%02X-%02X");
f % (int)MACData[0] % (int)MACData[1] %(int)MACData[2] %(int)MACData[3] %(int)MACData[4] %(int)MACData[5];
return f.str();
}
link Rpcrt4.lib...
获取MAC地址的方法
本文介绍了一种在Windows 2000/XP及以上系统中获取第一个网络适配器MAC地址的C++实现方法。该方法利用UuidCreateSequential函数生成一个序列化的UUID,并从中提取出MAC地址。需要注意的是,如果存在多个网卡,此方法仅返回第一个网卡的MAC地址。对于需要获取所有网卡信息的情况,建议使用GetAdaptersInfo函数。
360

被折叠的 条评论
为什么被折叠?



