c语言获取MAC地址
2014-09-01 21:17
288 查看
#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;
}
相关文章推荐
- Linux系统下用C语言获取MAC地址
- C语言实现Windows下获取IP和MAC地址。
- Linux下C语言获取所有网卡MAC地址的代码
- c语言 获取mac地址
- Linux系统下用C语言获取MAC地址
- ASP获取客户端MAC地址
- 获取本地主机的MAC地址
- 网卡MAC地址相关信息大全-4 获取远程网卡MAC地址
- [译]获取系统网络MAC地址的三种方法
- C#获取本地计算机名IP,Mac地址
- C#获取本地计算机名IP,Mac地址
- java简单的获取windows系统网卡mac地址
- 获取同网段其它机器的Mac地址
- Linux 下获取LAN中指定IP的网卡的MAC(物理地址)
- 用C#获取硬盘序列号,CPU序列号,网卡MAC地址
- 获取Mac地址
- C#获取本地计算机名,IP,MAC(物理)地址
- 获取MAC地址
- 如何获取本机所有MAC地址
- java简单的获取windows系统网卡mac地址