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

已举报 回答
查询MAC地址的C语言代码…要求简单精炼!
问在线客服
扫码问在线客服
  • 回答数

    7

  • 浏览数

    8,205

7个回答 默认排序
  • 默认排序
  • 按时间排序

已采纳
#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] );
}
}
取消 评论
/usr/src/linux/kernel vi fork.c

说实话,我看不懂。有兴趣你可以把内核源代码下载下来看这个fork.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里)
取消 评论
按字符串输入。
取消 评论
可以试一下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

#include stdio.h

int checkIP(const char* p)
{
int n[4];
char c[4];
if (sscanf(p, %d%c%d%c%d%c%d%c,
&n[0], &c[0], &n[1], &c[1],
&n[2], &c[2], &n[3], &c[3])
== 7)
{
int i;
for(i = 0; i 3; ++i)
if (c[i] != '.')
return 0;
for(i = 0; i 4; ++i)
if (n[i] 255 || n[i] 0)
return 0;
return 1;
} else
return 0;
}

int main()
{
const char* x[] =
{
192.168.1.1,
10.0.0.1.,
127.256.0.1,
iugerjiogjioe,
172.16,0.1,
0
};
const char* m[] =
{
不是合法的IP地址,
是合法的IP地址
};
int i = 0;

while(x[i] != 0) {
printf(%s %s\n, x[i], m[checkIP(x[i])]);
++i;
}
return 0;
}
取消 评论
用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();
}
}
取消 评论
0123456789ABCDEF,
只允许输入其中的一个是吗?
你查一下窗口子类化,在回调函数中拦截一下WM_CHAR这个消息
如果是上述值之一,则返回默认回调函数,否则返回其他值,或者执行你感兴趣的工作
取消 评论
ZOL问答 > 查询MAC地址的C语言代码…要求简单精炼!

举报

感谢您为社区的和谐贡献力量请选择举报类型

举报成功

经过核实后将会做出处理
感谢您为社区和谐做出贡献

扫码参与新品0元试用
晒单、顶楼豪礼等你拿

扫一扫,关注我们
提示

确定要取消此次报名,退出该活动?