C ++ hardware information to obtain the MAC address of the NIC
There are many ways to obtain the MAC address of a computer Nic.Command line formatYou can obtain the MAC addresses of the wired and wireless NICs. This is simple and you can directly use the code.
[1] header file
# If! Defined (afx_95644697_c78f_4dd6_885b_3d7c40b2d55c0000ded _) # define configure _ # if _ MSC_VER> 1000 # pragma once # endif // _ MSC_VER> 1000 # include <iostream> # include <string> # include <windows. h> using namespace std; // -------------------------------------------------------------- // Nic MAC address // -------------------------------------------------------------- BOOL GetMacBy Cmd (char * lpszMac, int len = 128); # endif //! Defined (afx_95644697_c78f_4dd6_885b_3d7c40b2d55c0000ded _)
[2] CPP File
# Include "stdafx. h "# include" MacID. h "// Nic 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"; // obtain the MAC command line const string strEnSearch = "Physical Address .........: "; // Nic MAC address leading information con St string strChSearch = "physical address .............: "; BOOL bret = FALSE; HANDLE hReadPipe = NULL; // read the pipeline HANDLE hWritePipe = NULL; // write the pipeline PROCESS_INFORMATION pi; // STARTUPINFOsi process information; // control the command line window information SECURITY_ATTRIBUTES sa; // Security Attribute charszBuffer [MAX_COMMAND_SIZE + 1] = {0}; // place the output buffer of the command line result stringstrBuffer; unsigned longcount = 0; longipos = 0; pi. hProcess = NULL; pi. hThread = NULL; si. cb = sizeof (STARTUPINFO); sa. NLength = sizeof (SECURITY_ATTRIBUTES); sa. lpSecurityDescriptor = NULL; sa. bInheritHandle = TRUE; // 1.0 create a pipe bret = CreatePipe (& hReadPipe, & hWritePipe, & sa, 0); if (! Bret) {goto END;} // 2.0 set the command line window information to GetStartupInfo (& si); si. hStdError = hWritePipe; si. hStdOutput = hWritePipe; si. wShowWindow = SW_HIDE; // hides the command line window si. dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; // 3.0 create a process bret = CreateProcess (NULL, szFetCmd, NULL, NULL, TRUE, 0, NULL, NULL, & si, & pi); if (! Bret) {goto END;} // 4.0 read the returned data WaitForSingleObject (pi. hProcess, 2000/* INFINITE */); bret = ReadFile (hReadPipe, szBuffer, MAX_COMMAND_SIZE, & count, 0); if (! Bret) {goto END;} // 5.0 searches for the MAC address. By default, the first IP address is used, which is generally the MACstrBuffer = szBuffer for Ethernet; ipos = strBuffer. find (strEnSearch); if (ipos <0) // system that distinguishes Chinese and English {ipos = strBuffer. find (strChSearch); if (ipos <1) {goto END;} // extract the MAC address string strBuffer = strBuffer. substr (ipos + strChSearch. length ();} else {// extract the MAC address string strBuffer = strBuffer. substr (ipos + strEnSearch. length ();} ipos = strBuffer. find ("\ n"); strBuffer = strBuffer. substr (0, ipos); memset (sz Buffer, 0x00, sizeof (szBuffer); strcpy_s (szBuffer, strBuffer. c_str (); // remove the '-' in the middle of "00-50-EB-0F-27-82" to 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}; // obtain MACGetMacByCmd (lpszMac); // print out MACcout <lpszMac <endl;