Get the system network card MAC address method

xiaoxiao2021-03-06  75

The first method uses Microsoft's NetBIOS API. This is a set of commands that provide underlying networks through Winsock. The biggest disadvantage using NetBIOS is that you must have a NetBIOS service in your system (if you enable file sharing in your Windows network, this is not a problem). In addition, this method is fast and accurate.

The NetBIOS API only includes a function, just called NetBIOS. This function uses the Network Control Block structure as a parameter, which tells the function to do. Definition structure follows: typedef struct _NCB {UCHAR ncb_command; UCHAR ncb_retcode; UCHAR ncb_lsn; UCHAR ncb_num; PUCHAR ncb_buffer; WORD ncb_length; UCHAR ncb_callname [NCBNAMSZ]; UCHAR ncb_name [NCBNAMSZ]; UCHAR ncb_rto; UCHAR ncb_sto; void (CALLBACK * ncb_post (struct _ncb *); uchar ncb_lana_num; uchar ncb_cmd_cplt; #ifdef _win64 uchar ncb_reserve [18]; #ELSE UCHAR NCB_RESERVE [10]; #ENDIF HANDLE NCB_EVENT;} NCB, * PNCB;

The focus is on NCB_COMMAND members. This member tells NetBIOS what should I do. We use three commands to detect the MAC address. They are defined in MSDN as follows: Command Description: NcBenum Windows NT / 2000: Enumerally listing the number of network cards in the system. After using this command, the NCB_BUFFER member points to the buffer filled by the LANA_ENUM structure. Ncbenum is not a standard NetBIOS 3.0 command.

Ncbreset reset the network card. The NIC must reset before accepting the new NCB command. NCBastat accepts the status of the local or remote interface card. After using this command, the NCB_Buffer member points to the buffer filled by the Adapter_status structure, followed by an array of Name_Buffer structures.

Here is to get the steps of your system MAC address: 1 "list all interface cards. 2 "Reset each card to obtain its correct information. 3 "Query the interface card, get the MAC address and generate a standard colonial separation format.

Here is an example source program. Netbios.cpp

#include #include #include #include #include

Using namespace std; #define Bzero (Thing, SZ) MEMSET (Thing, 0, SZ)

bool GetAdapterInfo (int adapter_num, string & mac_addr) {// resets the card, so that we can query NCB Ncb; memset (& Ncb, 0, sizeof (Ncb)); Ncb.ncb_command = NCBRESET; Ncb.ncb_lana_num = adapter_num; if (Netbios (& NCB)! = NRC_GODRET) {mac_addr = ""; mac_addr = string (ncb.ncb_retcode); returnaf false;

// obtain ready interface card status block bzero (& Ncb, sizeof (Ncb); Ncb.ncb_command = NCBASTAT; Ncb.ncb_lana_num = adapter_num; strcpy ((char *) Ncb.ncb_callname, "*"); struct ASTAT {ADAPTER_STATUS adapt Name_buffer namebuff [30];} adapter; Bzero (& Adapter, SIZEOF (Adapter)); ncb.ncb_buffer = (unsigned char *) & adapter; ncb.ncb_length = sizeof (adapter); // Get information of the network card, and if the network card Normal work, return to standard colonial separation format. IF (NetBIOS (& NCB) == 0) {char acmac [18]; Sprintf (acmac, "% 02x:% 02x:% 02x:% 02x:% 02x:% 02x ", int (adapter.adapt.adapter_address [0]), int (adapter.adapt.adapter_address [1]), int (adapter.adapt.adapter_address [2]), int (adapter.adapt.adapter_address [3]), INT (Adapter.adapt.adapter_address [4]), int (adapter.adapt.adapter_address [5]))); mac_addr = acmac; return true;} else {mac_addr = "; mac_addr = String Ncb.ncb_retcode; return false;}}

int main () {// acquired card list LANA_ENUM AdapterList; NCB Ncb; memset (& Ncb, 0, sizeof (NCB)); Ncb.ncb_command = NCBENUM; Ncb.ncb_buffer = (unsigned char *) & AdapterList; Ncb.ncb_length = sizeof AdapterList); NetBIOS (& NCB);

/ / Take the address of the local Ethernet card string mac_addr; for (int i = 0; i

Return 0;}

转载请注明原文地址:https://www.9cbs.com/read-93763.html

New Post(0)