C++ 获取mac地址

 1 // MacAddress.cpp : Defines the entry point for the console application.
 2 //
 3 
 4 #include "stdafx.h"
 5 #include <windows.h>
 6 #include <wincon.h>
 7 #include <stdlib.h>
 8 #include <stdio.h>
 9 #include <time.h>
10 #include <Nb30.h>
11 #pragma comment(lib,"netapi32.lib")  
12 
13 int GetMac(char * mac)     
14 {     
15     NCB ncb;
16     typedef struct _ASTAT_
17     {
18         ADAPTER_STATUS   adapt;
19         NAME_BUFFER   NameBuff   [30];
20     }ASTAT,*PASTAT;
21 
22     ASTAT Adapter;   
23 
24     typedef struct _LANA_ENUM
25     {
26         UCHAR   length;
27         UCHAR   lana[MAX_LANA];
28     }LANA_ENUM;
29 
30     LANA_ENUM lana_enum;    
31     UCHAR uRetCode;
32     memset(&ncb, 0, sizeof(ncb));
33     memset(&lana_enum, 0, sizeof(lana_enum));    
34     ncb.ncb_command = NCBENUM;
35     ncb.ncb_buffer = (unsigned char *)&lana_enum;
36     ncb.ncb_length = sizeof(LANA_ENUM);
37     uRetCode = Netbios(&ncb);
38 
39     if(uRetCode != NRC_GOODRET)     
40         return uRetCode;     
41 
42     for(int lana=0; lana<lana_enum.length; lana++)     
43     {
44         ncb.ncb_command = NCBRESET;
45         ncb.ncb_lana_num = lana_enum.lana[lana];
46         uRetCode = Netbios(&ncb); 
47         if(uRetCode == NRC_GOODRET)
48             break; 
49     }
50 
51     if(uRetCode != NRC_GOODRET)
52         return uRetCode;     
53 
54     memset(&ncb, 0, sizeof(ncb));
55     ncb.ncb_command = NCBASTAT;
56     ncb.ncb_lana_num = lana_enum.lana[0];
57     strcpy((char*)ncb.ncb_callname, "*");
58     ncb.ncb_buffer = (unsigned char *)&Adapter;
59     ncb.ncb_length = sizeof(Adapter);
60     uRetCode = Netbios(&ncb);
61 
62     if(uRetCode != NRC_GOODRET)
63         return uRetCode;
64 
65     sprintf(mac,"%02X-%02X-%02X-%02X-%02X-%02X",
66         Adapter.adapt.adapter_address[0],
67         Adapter.adapt.adapter_address[1],
68         Adapter.adapt.adapter_address[2],
69         Adapter.adapt.adapter_address[3],
70         Adapter.adapt.adapter_address[4],
71         Adapter.adapt.adapter_address[5]);
72 
73     return 0;   
74 } 
75 
76 
77 int main(int argc, char* argv[])
78 {
79     char   mac[200]; 
80     GetMac(mac);   
81     printf("The Mac Address is : %s   \n",mac);        
82 
83     system("pause");
84     return 0;
85 }

 转载:http://www.cnblogs.com/SteveCheung/archive/2012/07/18/2597449.html

转载于:https://www.cnblogs.com/chechen/p/5946717.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值