C ++ hardware information to obtain the MAC address of the NIC, mac address of the NIC

Source: Internet
Author: User

C ++ hardware information to obtain the MAC address of the NIC, mac address of the NIC

There are many ways to obtain the MAC address of a computer Nic.Command line formatObtain the MAC address of the wired and wireless network cards.

On 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;


[Solved] share a piece of code: AIR reads the MAC address and IP address of the hardware NIC (a term)
Use c # To obtain the MAC address of the NIC hardware used by the computer

Hey, I start directly every time. | in the running, I get all the MAC addresses.
I wonder if there is any virtual network card you mentioned.

Start | run | enter "cmd" | enter "ipconfig-all" and press Enter.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.