VC + +, the program reads the card MAC address



In practical applications, we often need to run the machine when the network card to get the current MAC address to use as some sort of identification, such as the legitimacy of such control procedures.Below on how to use Microsoft Visual C + + 6.0 to develop such a program demonstrates how to achieve its main points.

---- This approach is built through the Windows 9x/NT/Win2000 NetApi32.DLL function to achieve, first by sending NCBENUM command to get the number of cards and each card's internal number, and then for eachSend NCBASTAT card label to get its MAC address command.Note: This is bundled with the card NetBeui protocol communication stack, you can view the card at the property.

---- Run the VC + +, open a new project, choose to create a Win32 Console program, enter the code and then press the paper and see where the comment: # include "stdafx.h"

# Include

# Include

# Include

# Include

# Include

---- / / Because it is to get through NetAPI card information, so the need to include its header file nb30.h # include

typedef struct _ASTAT_

{

ADAPTER_STATUS adapt;

NAME_BUFFER NameBuff [30];

} ASTAT, backup bin conf config data eshow_sitemap.html generate.sh log maint sitemap.html svn tmp PASTAT;

ASTAT Adapter;

---- / / Define a variable storing return card information

---- / / Input parameters: lana_num for the card number, in general, starting at 0, but in Windows 2000, the distribution is not necessarily a continuous

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 card number

---- / / First, the selected card to send a NCBRESET command to initialize

uRetCode = Netbios (& ncb);

printf ("The NCBRESET return code is:

0x% x ", uRetCode);

memset (& ncb, 0, sizeof (ncb));

ncb.ncb_command = NCBASTAT;

ncb.ncb_lana_num = lana_num; / / specify the card number

strcpy ((char *) ncb.ncb_callname,

"*");

ncb.ncb_buffer = (unsigned char *) &Adapter;

---- / / Return the specified information is stored in the variable

ncb.ncb_length = sizeof (Adapter);

---- / / Then you can send NCBASTAT order to obtain the information card

uRetCode = Netbios (& ncb);

printf ("The NCBASTAT

return code is: 0x% x ", uRetCode);

if (uRetCode == 0)

{

---- / / MAC address of the card into a common format of 16 hexadecimal form, such as the 0010-A4E4-5802

printf ("The Ethernet Number [% d] [Page]

is:% 02X% 02X-% 02X% 02X-% 02X% 02X ",

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 card to obtain card information on the current machine, if the number of cards, each card number, etc.

uRetCode = Netbios (& ncb);

printf ("The NCBENUM return

code is:

0x% x ", uRetCode);

if (uRetCode == 0)

{

printf ("Ethernet Count is:% d", lana_enum.length);

---- / / For each piece of card, enter the number of its card number to obtain the MAC address

for (int i = 0; i
getmac_one (lana_enum.lana [i]);

}

return 0;

}

---- At this point, press F7 compile, until the adoption, you can press F5 to run.

---- This code can be embedded directly into the relevant applications, or packaged. DLL or COM control so that in the Visual Basic, Visual Foxpro, Power Builder or Delphi to call other programs