用C语言写个程序:先获取本机MAC地址,据此得到Link Local地址(IPv6 Address)
麻烦,不爱动手,上网查一下,就那么两个api,一用就ok了。easy的很。
#include winsock2.h
#include Iphlpapi.h
#include stdio.h
void byte2Hex(unsigned char bData,unsigned char hex[])
{
int high=bData/16,low =bData %16;
hex[0] = (high 10)?(‘0’+high):(‘A’+high-10);
hex[1] = (low 10)?(‘0’+low):(‘A’+low-10);
}
int getLocalMac(unsigned char *mac) //获取本机MAC地址
{
ULONG ulSize=0;
PIP_ADAPTER_INFO pInfo=NULL;
int temp=0;
temp = GetAdaptersInfo(pInfo,ulSize);//第一次调用,获取缓冲区大小
pInfo=(PIP_ADAPTER_INFO)malloc(ulSize);
temp = GetAdaptersInfo(pInfo,ulSize);
int iCount=0;
while(pInfo)//遍历每一张网卡
{
// pInfo-Address 是MAC地址
for(int i=0;i(int)pInfo-AddressLength;i++)
{
byte2Hex(pInfo-Address[i],mac[iCount]);
iCount+=2;
if(i(int)pInfo-AddressLength-1)
{
mac[iCount++] = ‘:’;
}else
{
mac[iCount++] = ‘#’;
}
}
pInfo = pInfo-Next;
}
if(iCount 0)
{
mac[–iCount]=’\0′;
return iCount;
}
else return -1;
}
int main(int argc, char* argv[])
{
unsigned char address[1024];
if(getLocalMac(address)0)
{
printf(“mac-%s\n”,address);
}else
{
printf(“invoke getMAC error!\n”);
}
return 0;
}
需要这两个:iphlpapi.lib , ws2_32.lib 静态库(VC添加到工程LINK里)
c语言,获取本机mac地址,那位大神解答下。
有个简单的方法,提供给你个思路
system(“ipconfig /all tmp.txt”);
然后打开tmp.txt
查找本地连接 再找之后的Physical Address字符串,然后找冒号,
取这个冒号后面的值就是mac了
如何通过C语言自动生成MAC地址
mac地址其实就是6个无符号的字符型数据,范围在0~255
所以 生成6个unsigned char类型的随机数就好。
#include stdio.h
#include stdlib.h
#include time.h
int main()
{
unsigned char a[6];
int i;
srand(time(NULL));
for(i = 0; i 6;i++)
a[i]=rand()0xFF;
for(i = 0; i 5;i++)
printf(“%02X:”,a[i]);
printf(“%02X\n”,a[i]);
return 0;
}