C ++ obtains the MAC address

Source: Internet
Author: User
# Include <nb30.h> # pragma comment (Lib, "netapi32.lib") typedef struct _ astat _ {adapter_status adapt; name_buffer namebuff [30];} astat, * pastat; astadapter; // define a variable for storing the returned Nic information // input parameter: lana_num is the NIC number, generally starting from 0, however, in Windows 2000, it is not necessarily the consecutively assigned void getmac_one (INT lana_num) {NCB; uchar uretcode; memset (& NCB, 0, sizeof (NCB); NCB. ncb_command = ncbreset; NCB. ncb_lana_num = lana_num; // specify the nic id // first, send an ncbreset command to the selected Nic to initialize 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 the NIC number strcpy (char *) NCB. ncb_callname, "*"); NCB. ncb_buffer = (unsigned char *) & adapter; // specify the variable NCB where the returned information is stored. ncb_length = sizeof (adapter); // you can then send the ncbastat command to obtain the NIC information uretcode = NetBIOS (& NCB); // printf ("The ncbastat return code is: 0x % x \ n ", uretcode); If (uretcode = 0) {// format the nic mac address into a common hexadecimal format, for example, 0010-a4e4-5802printf ("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 () {NCB; uchar uretcode; 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 the ncbenum command to the NIC to obtain the NIC information of the current machine, uretcode = NetBIOS (& NCB); // printf ("The ncbenum return code is: 0x % x \ n", uretcode ); if (uretcode = 0) {// printf ("Ethernet count is: % d \ n", lana_enum.length); // For each Nic, enter the ID of the network card and obtain the MAC address for (INT I = 0; I <lana_enum.length; ++ I) getmac_one (lana_enum.lana [I]);} return 0 ;}

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.