VC++ 获取mac地址
2012-07-18 15:11 AlexDotNet 阅读(6199) 评论(0) 编辑 收藏 举报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 }