c语言获取MAC地址
来源:互联网 发布:淘宝商家客服电话0571 编辑:程序博客网 时间:2024/05/22 12:57
#include <winsock2.h>
#include <Iphlpapi.h>
#include <stdio.h>
#pragma comment(lib, "Iphlpapi.lib")
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;
}
#include <Iphlpapi.h>
#include <stdio.h>
#pragma comment(lib, "Iphlpapi.lib")
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;
}
0 0
- c语言 获取mac地址
- c语言获取MAC地址
- c语言库函数获取mac地址
- Linux C语言获取设备MAC地址
- linux下获取所有网卡mac地址(C语言)
- linux下C语言获取网卡MAC地址
- C 语言中通过脚本获取mac地址
- C++Builder 获取MAC地址
- linux c获取mac地址
- Windows C 获取mac地址
- linux c获取mac地址
- linux c获取mac地址
- linux系统下C语言获取和设置本地MAC地址的代码
- Linux下C语言获取所有网卡MAC地址的代码
- C语言获取本地所有网卡的ip地址及MAC信息
- Liunx C获取IP、MAC地址
- linux c 获取本地MAC地址
- iOS objective-c 获取mac地址
- C++中类对象所占空间的大小
- 最大间距问题(元素分段/分桶法)
- RailsCasts16 Virtual Attributes 虚拟属性
- Spring+hibernate事务详解
- 智能家居时代谁来承载中国家庭的信息安全?
- c语言获取MAC地址
- The Balance(母函数)
- URAL 1310. ACM Diagnostics
- Orchard的几个概念
- 函数参数传递方式
- Linux下wait, WIFEXITED, WEXITSTATUS的比较
- 谈谈对方法模板设计模式的理解
- KMP模式匹配算法
- 读《大型网站技术架构-核心原理与技术分析》有感之一 架构师领导艺术