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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用C语言写个程序:先获取本机MAC地址,据此得到Link Local地址(IPv6 Address)相关的知识,希望对你有一定的参考价值。

大家帮帮忙。要用C语言编写啊

麻烦,不爱动手,上网查一下,就那么两个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里)
参考技术A 麻烦,不爱动手,上网查一下,就那么两个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里)
参考技术B 用JAVA写的一个

import java.io.*;
import javax.swing.*;
public class Ipv6
public static void main(String[] args)throws Exception
String physicalAddress=Ipv6.getMac();//获取主机的MACA地址
String str[]=physicalAddress.split("-");
String str1=Ipv6.hexString2binaryString(str[0]);
char[] char2=str1.toCharArray();
if(char2[6]=='1')
char2[6]='0';

else
char2[6]='1';

String str3=String.copyValueOf(char2);
String str4=Ipv6.binaryString2hexString(str3);
str[0]=str4;
String str5="FE80::"+str[0]+str[1]+":"+str[2]+"FF"+":"+"FE"+str[3]+":"+str[4]+str[5];
JOptionPane.showMessageDialog(null,"本机的MAC地址是: "+ physicalAddress+";\nLink-Local Ipv6 地址为:"+str5,"Link-Local Ipv6",JOptionPane.INFORMATION_MESSAGE);


public static String getMac()throws Exception//获取主机的MACA地址
String line;
String physicalAddress = "read MAC error!";
try
Process p=Runtime.getRuntime().exec("cmd.exe /c ipconfig /all");
BufferedReader bd =new BufferedReader(new InputStreamReader (p.getInputStream()));
while((line=bd.readLine())!=null)
if(line.indexOf("Physical Address. . . . . . . . . :")!=-1)
if(line.indexOf(":")!=-1)
physicalAddress = line.substring(line.indexOf(":")+2);
break; //找到MAC,推出循环



p.waitFor();
catch (IOException e)
e.printStackTrace();

return physicalAddress;


public static String hexString2binaryString(String hexString) //将16进制字符串转化为2进制字符串
if (hexString == null || hexString.length() % 2 != 0)
return null;
String bString = "", tmp;
for (int i = 0; i < hexString.length(); i++)
tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hexString.substring(i, i + 1), 16));
bString += tmp.substring(tmp.length() - 4);

return bString;


public static String binaryString2hexString(String bString) //将2进制字符串转化为16进制字符串
if (bString == null || bString.equals("") || bString.length() % 8 != 0)
return null;
StringBuffer tmp=new StringBuffer();
int iTmp = 0;
for (int i = 0; i < bString.length(); i += 4)
iTmp = 0;
for (int j = 0; j < 4; j++)
iTmp += Integer.parseInt(bString.substring(i + j, i + j + 1)) << (4 - j - 1);

tmp.append(Integer.toHexString(iTmp));

return tmp.toString();

python-获取本机mac地址

 1 #!/usr/bin/env python
 2 #-*- coding:utf-8 -*-
 3 ############################
 4 #File Name: getmac.py
 5 #Author: frank
 6 #Mail: [email protected]
 7 #Created Time:2017-06-05 17:10:51
 8 ############################
 9 
10 import uuid
11 
12 def get_mac_address():
13     mac=uuid.UUID(int = uuid.getnode()).hex[-12:].upper()
14     #return ‘%s:%s:%s:%s:%s:%s‘ % (mac[0:2],mac[2:4],mac[4:6],mac[6:8],mac[8:10],mac[10:])
15     return ":".join([mac[e:e+2] for e in range(0,11,2)])
16 
17 if __name__ == __main__:
18     ##name = "test_name"
19     #namespace = "test_namespace"
20 
21     print get_mac_address()
22     ‘‘‘
23     u=uuid.uuid1()  # 带参的方法参见Python Doc
24     print u
25     #print uuid.uuid3(namespace, name)
26     #print uuid.uuid4()
27     #print uuid.uuid5(namespace, name)
28     node1=uuid.getnode() #获取mac地址
29     print ("node1:%s"%node1)
30     print ("hex(node1):%s"%hex(node1))
31     print ("uuid.bytes:%s"%repr(u.bytes))
32     print ("uuid.bytes_le:%s"%repr(u.bytes_le))
33     print ("hex:", u.hex)
34     print ‘int     :‘, u.int
35     print ‘urn     :‘, u.urn
36     print ‘variant :‘, u.variant
37     print ‘version :‘, u.version
38     #print get_mac_address()
39     ‘‘‘

 

以上是关于用C语言写个程序:先获取本机MAC地址,据此得到Link Local地址(IPv6 Address)的主要内容,如果未能解决你的问题,请参考以下文章

用java获得机器的唯一号

node.js获取本机IP地址

求1易语言获取MAC地址到编辑框1的方法

用c语言写个函数测试处理器是大端还是小端程序怎么写?

Python - 使用 C 类型和本机 ioctl() 获取 Mac 地址会产生未知结果

获取本机的IP地址和mac地址