There are many ways to get the MAC address of the computer network card, which is obtained by command line , can get the MAC address of the Wired NIC and the wireless card, very simple, direct
On the code.
"1" header file
#if!defined (afx_95644697_c78f_4dd6_885b_3d7c40b2d55c__included_) #define Afx_95644697_c78f_4dd6_885b_ 3d7c40b2d55c__included_#if _msc_ver > 1000#pragma once#endif//_msc_ver > 1000#include <iostream> #include & Lt;string> #include <windows.h>using namespace std;//---------------------------------------------------- ----------//Network card MAC address//--------------------------------------------------------------BOOL getmacbycmd (char * Lpszmac, int len=128); #endif//!defined (AFX_95644697_C78F_4DD6_885B_3D7C40B2D55C__INCLUDED_)
"2" CPP file
#include "stdafx.h" #include "MacID.h"//--------------------------------------------------------------//network card MAC address// --------------------------------------------------------------BOOL Getmacbycmd (char *lpszmac, int len/*=128*/) { Const long max_command_size = 10000; command line output buffer size WCHAR szfetcmd[]= L "Ipconfig/all"; Get MAC command line const string strensearch = "Physical Address. ........:"; The leading information for the NIC MAC address is const string strchsearch = "Physical Address .............:"; BOOL bret = FALSE; HANDLE hreadpipe = NULL; Read pipe handle hwritepipe = NULL; Write Pipeline process_information Pi; Process information Startupinfosi; Control command Line window information security_attributes sa; Security attribute Charszbuffer[max_command_size+1] = {0}; The output buffer for placing command-line results stringstrbuffer;unsigned longcount = 0;longipos = 0;pi.hprocess = Null;pi.hthread = null;si.cb= sizeof (ST artupinfo); sa.nlength= sizeof (security_attributes); sa.lpsecuritydescriptor = null;sa.binherithandle= TRUE;//1.0 Create Pipeline bret = CreatePipe (&hreadpipe, &hwritepipe, &sa, 0); if (!bret) {goto END;} 2.0 Set command-line window information for the specified read-write pipeline getstartupinfo (&si); si.hstderror= hwritepipe;si.hstdoutput= hwritepipe;si.wshowwindow= SW _hide; Hide command-line window si.dwflags= Startf_useshowwindow | STARTF_USESTDHANDLES;//3.0 Create a process that gets the command line bret = CreateProcess (null, szfetcmd, NULL, NULL, TRUE, 0, NULL, NULL, &SI, &p i); if (!bret) {goto END;} 4.0 read the returned data WaitForSingleObject (pi.hprocess, 2000/*infinite*/); bret = ReadFile (Hreadpipe, Szbuffer, Max_command_siz E, &count, 0); if (!bret) {goto END;} 5.0 Find MAC address, default find first, generally ethernet Macstrbuffer = Szbuffer;ipos = Strbuffer.find (Strensearch), if (IPOs < 0)//distinguish between Chinese and English system { IPOs = Strbuffer.find (Strchsearch); if (IPOs < 1) {goto END;} Extract MAC address String strbuffer = Strbuffer.substr (Ipos+strchsearch.length ());} else{//extract MAC address string strbuffer = Strbuffer.substr (Ipos+strensearch.length ());} IPOs = Strbuffer.find ("\ n"); strbuffer = strbuffer.substr (0, IPOs); memset (Szbuffer, 0x00, sizeof (szbuffer)); strcpy_s ( Szbuffer, Strbuffer.c_str ());//Remove the middle "00-50-eb-0f-27-82" in the middle of '-' get 0050eb0f2782int j = 0;for (int i=0; I<strlen (Szbuffer); i++) {if (szbuffer[i]! = '-') {lpszmac[j] = szbuffer[i];j++;}} bret = TRUE; end://Close all handles CloseHandle (hwritepipe); CloseHandle (Hreadpipe); CloseHandle (pi.hprocess); CloseHandle (Pi.hthread); return (Bret);}
"3" Test
Char lpszmac[128] = {0};//get Macgetmacbycmd (LPSZMAC);//Print out maccout << lpszmac << Endl;
C + + hardware information get NIC MAC address