How to read the MAC address of the NIC in VC
Wen / Wen Weijiang
In active applications, we will go to the MAC address of the network card of the front machine when it is running, so as to use a certain identification, such as the legality of the control program. The following text describes how to use Microsoft Visual C 6.0.
---- The method used in this is to realize the functionality of NetApi32.dll built in the Windows 9X / NT / WIN2000. First pass the send ncbbenum command, get the number of network cards and the internal number of each network card, and then send the ncbastat command for each network card sign to get the MAC address. Note: The network card in this refers to a communication protocol stack bundled with the NetBeiu protocol, which can be viewed at the attribute of the network card.
---- Please run VC , open a new process, choose to create a Win32 console program, then press the text to enter the code, and see the comments in it:
#Include "stdafx.h"
#Include
#Include
#Include
#Include
#Include
// Because it is to obtain a network card information through NetApi,
The need to include the title of its header NB30.H
#Include
TYPEDEF STRUCT_STAT_ {Adapter_Status Adapt; Name_Buffer NameBuff [30];} ASTAT, * PASTAT; ASTAT Adapter; // Defines a variable // input parameter of returning to NIC information: LANA_NUM is the network card number, in general, starting from 0, but In Windows 2000, it is not necessarily a continuous allocated Void getMac_one (int LANA_NUM) {NCB NCB; Uchar Uretcode; MEMSET (& NCB, 0, SIZEOF (NCB)); ncb.ncb_command = ncbreset; ncb.ncb_lana_num = lana_num; // Specify the NIC number / / first sends a ncbreset command to the selected network card for initialization uretcode = netbios (& NCB); Printf ("The Ncbreset Return Code IS: 0x% x / n", uretcode); MEMSET (& NCB, 0 , SIZEOF (NCB)); ncb.ncb_command = ncbastat; ncb.ncb_lana_num = lana_num; // Specify NIC NS Number STRCPY ((char *) ncb.ncb_callname, "*"); ncb.ncb_buffer = (unsigned char *) & adapter; / / Specify the variable NCB.ncb_length = sizeOf (Adapter) of the returned information; // Next, the ncbastat command can be sent to obtain information URetcode = NetBIOS (& NCB); Printf ("The ncbastat return code is: 0x% x / N ", uretcode); if (uretcode == 0) {// put the network card MAC address into a commonly used 16-in-form, such as 0010 -A4E4 -5802 Printf "The Ethernet Number [% D] IS:% 02x% 02x -% 02x% 02x -% 02x% 02x / n", LANA_NUM, Adapter.Adapt.adapter_address [0], Adapter.adapt.adapter_address [1], Adapter. Adapt.adapter_address [2], adapter.adapt.adapter_address [3], adapter.adapt.adapter_address [4], adapter.adapt.adapter_address [5]);}}} int main (int Argc, char * argv []) { NCB NCB; UCHAR URETCODE; LANA_ENUM LANA_ENUM; MEMSET (& NCB, 0, SIZEOF (NCB)); ncb.ncb_command = ncbenum; ncb.ncb_buffer =
(unsigned char *) & lana_enum; ncb.ncb_length = sizeof (LANA_ENUM); / / Send NcBenum command to the network card to get the network card information of the current machine, if there are many NICs, each NIC number, etc. URETCODE = NetBIOS (& NCB) PRINTF ("THE NCBENUM RETURN CODE IS: 0X% X / N", URetcode); if (uretcode == 0) {Printf ("Ethernet Count IS:% D / N / N", LANA_ENUM.LENGTH); // For each network card, with its NIC number as the input number, obtain its MAC address for (INT i = 0; i ---- At this time, press F7 to compile, press F5 to run.
---- This code can be directly embedded in the application system, or in a .DLL or COM control, to be called in other programs such as Visual Basic, Visual FoxPro, Power Builder or Delphi.