网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

c语言mac地址解析函数 c语言获取mac地址

用C语言写个程序:先获取本机MAC地址,据此得到Link Local地址(IPv6 Address)

麻烦,不爱动手,上网查一下,就那么两个api,一用就ok了。easy的很。

创新互联公司主营杭州网站建设的网络公司,主营网站建设方案,重庆APP开发公司,杭州h5成都小程序开发搭建,杭州网站营销推广欢迎杭州等地区企业咨询

#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里)

查询MAC地址的C语言代码…要求简单精炼!

#include windows.h

#include wincon.h

#include stdlib.h

#include stdio.h

typedef struct _ASTAT_

{

ADAPTER_STATUS adapt;

NAME_BUFFER NameBuff [30];

}ASTAT, * PASTAT;

ASTAT Adapter;

void main (void)

{

NCB ncb;

UCHAR uRetCode;

char NetName[50];

memset( ncb, 0, sizeof(ncb) );

ncb.ncb_command = NCBRESET;

ncb.ncb_lana_num = 0;

uRetCode = Netbios( ncb );

printf( "The NCBRESET return code is: 0x%x \n ", uRetCode );

memset( ncb, 0, sizeof(ncb) );

ncb.ncb_command = NCBASTAT;

ncb.ncb_lana_num = 0;

strcpy( (char*)ncb.ncb_callname, "* " );

ncb.ncb_buffer = (unsigned char *) Adapter;

ncb.ncb_length = sizeof(Adapter);

uRetCode = Netbios( ncb );

printf( "The NCBASTAT return code is: 0x%x \n ", uRetCode );

if ( uRetCode == 0 )

{

printf( "The Ethernet Number is: %02x%02x%02x%02x%02x%02x\n ",

Adapter.adapt.adapter_address[0],

Adapter.adapt.adapter_address[1],

Adapter.adapt.adapter_address[2],

Adapter.adapt.adapter_address[3],

Adapter.adapt.adapter_address[4],

Adapter.adapt.adapter_address[5] );

}

}

c语言,获取本机mac地址,那位大神解答下。

有个简单的方法,提供给你个思路

system("ipconfig /all tmp.txt");

然后打开tmp.txt

查找本地连接 再找之后的Physical Address字符串,然后找冒号,

取这个冒号后面的值就是mac了

C语言如何获取嵌入式linux网卡上的mac地址

全对应的

getmacaddress.c

#include stdio.h

#if defined(WIN32) || (!defined(__GNUC__)  !defined(__clang__))

#include winsock2.h

#include iphlpapi.h

#include stdlib.h

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

#elif defined(__linux__)

#include string.h //strncpy

#include sys/ioctl.h

#include sys/socket.h

#include net/if.h

#else

//bsd

#include sys/types.h //FreeBSD u_int

#include ifaddrs.h

#include net/if.h

#include net/if_dl.h

#include net/if_types.h

#endif

void getmacaddress(char *mac_address){

#if defined(WIN32) || (!defined(__GNUC__)  !defined(__clang__))

PIP_ADAPTER_INFO pAdapterInfo;

PIP_ADAPTER_INFO pAdapter;

ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);

unsigned char *addr;

mac_address[0]=0;

pAdapterInfo = (IP_ADAPTER_INFO*)malloc(sizeof(IP_ADAPTER_INFO));

if(!pAdapterInfo)return;

if(GetAdaptersInfo(pAdapterInfo, ulOutBufLen) == ERROR_BUFFER_OVERFLOW){

free(pAdapterInfo);

pAdapterInfo = (IP_ADAPTER_INFO*)malloc(ulOutBufLen);

if(!pAdapterInfo)return;

}

if(GetAdaptersInfo(pAdapterInfo, ulOutBufLen) == NO_ERROR){

pAdapter = pAdapterInfo;

if(pAdapter){

addr = pAdapter-Address;

sprintf(mac_address,"%02x:%02x:%02x:%02x:%02x:%02x",

addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);

}

}

free(pAdapterInfo);

#elif defined(__linux__)

mac_address[0]=0;

struct ifreq *ifr, *ifend;

struct ifreq ifreq;

struct ifconf ifc;

struct ifreq ifs[16];

int fd;

unsigned char *addr;

fd = socket(AF_INET, SOCK_DGRAM, 0);

ifc.ifc_len = sizeof(ifs);

ifc.ifc_req = ifs;

if(ioctl(fd, SIOCGIFCONF, ifc)0){close(fd);return;}

ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));

for(ifr = ifc.ifc_req;ifr  ifend;ifr++){

if(ifr-ifr_addr.sa_family == AF_INET){

strncpy(ifreq.ifr_name, ifr-ifr_name, sizeof(ifreq.ifr_name));

if(ioctl(fd, SIOCGIFHWADDR, ifreq)0)continue;

addr = ifreq.ifr_hwaddr.sa_data;

sprintf(mac_address,"%02x:%02x:%02x:%02x:%02x:%02x",

addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);

}

}

close(fd);

#else

//bsd

mac_address[0]=0;

struct ifaddrs *ifa_list, *ifa; 

struct sockaddr_dl *dl; 

unsigned char *addr;

if(getifaddrs(ifa_list)0)return;

for(ifa = ifa_list;ifa;ifa = ifa-ifa_next){ 

dl = (struct sockaddr_dl*)ifa-ifa_addr; 

if (dl-sdl_family == AF_LINK  dl-sdl_type == IFT_ETHER) {

addr = (unsigned char*)LLADDR(dl);

sprintf(mac_address,"%02x:%02x:%02x:%02x:%02x:%02x",

addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);

return;

}

freeifaddrs(ifa_list); 

#endif

}

如何用C语言获取网卡的mac地址

为什么一定要用C语言呢?这个用C语言比较麻烦,需要的知识比较多,完全可以用更简单的办法啊。命令行上这样的命令“ipconfig -all | find "物理地址"”,可以很简单的就得到了。

如果是英文系统, 将“物理地址”换成“Physical Address”即可。

如果非的要用C语言,则可以先调用system函数, system("ipconfig -all | find \"物理地址\" temp.txt" ); 将mac地址信息存入临时文件temp.txt;然后再从中提取。


本文名称:c语言mac地址解析函数 c语言获取mac地址
标题链接:http://bjjierui.cn/article/doopcgd.html

其他资讯