1. NDIS (DDK) gets the MAC address through the driver
The NDIS specification specifies that the NIC Driver supports the ioctl_ndis_query_stats interface.
The parameters are as follows:
Oid_802_3_permanent_address: physical address
Oid_802_3_current_address: MAC address
So we can get the method.
First, look at the Registry and find out how many NICs are there, and what are the device names respectively.
The specific location is related to OS. In 2000, it is in HLM/software/Microsoft/Windows NT/current version/networkcards.
Then createfile (devicename,...). Note that you must use linkname. Therefore
Add "//. // device //".
Next
Deviceiocontrol (HMAC, ioctl_ndis_query_stats,
Oid_802_3_permanent_address/oid_802_3_current_address ...)
For more information, see
Oid_802_3_current_address entry
2. NetAPI-2 get Mac (MsdnRecommended Methods)
# Include <windows. h>
// # Include <wincon. h>
# Include <stdlib. h>
# Include <stdio. h>
# Include <time. h>
Typedef struct _ astat _
{
Adapter_status adapt;
Name_buffer namebuff [30];
} Astat, * pastat;
Astat adapter;
Int main (void)
{
NCB;
Uchar uretcode;
Char netname [50];
Lana_enum lenum;
Int I;
Memset (& NCB, 0, sizeof (NCB ));
NCB. ncb_command = ncbenum;
NCB. ncb_buffer = (uchar *) & lenum;
NCB. ncb_length = sizeof (lenum );
Uretcode = NetBIOS (& NCB );
Printf ("The ncbenum return code is: 0x % x/N", uretcode );
For (I = 0; I <lenum. length; I ++)
{
Memset (& NCB, 0, sizeof (NCB ));
NCB. ncb_command = ncbreset;
NCB. ncb_lana_num = lenum. Lana;
Uretcode = NetBIOS (& NCB );
Printf ("The ncbreset on Lana % d return code is: 0x % x/N ",
Lenum. Lana, uretcode );
Memset (& NCB, 0, sizeof (NCB ));
NCB. ncb_command = ncbastat;
NCB. ncb_lana_num = lenum. Lana;
Strcpy (NCB. ncb_callname ,"*");
NCB. ncb_buffer = (char *) & adapter;
NCB. ncb_length = sizeof (adapter );
Uretcode = NetBIOS (& NCB );
Printf ("The ncbastat on Lana % d return code is: 0x % x/N ",
Lenum. Lana, uretcode );
If (uretcode = 0)
{
Printf ("the Ethernet number on Lana % d is: % 02x % 02x % 02x % 02x % 02x % 02x/N ",
Lenum. Lana,
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]);
}
}
}
3. Use com api to obtain the MAC address of the NIC
This method uses the com api to create a guid (globally unique identifier) and inherit the MAC address from it.
GUID is usually used to identify COM components and other objects in the system. They are calculated from MAC addresses (combined with other things). On the surface, MAC addresses are included in them.
On the surface, it is because it is not actually included. I provide this method to serve as a negative textbook. You may use this method to obtain the MAC address,
Sometimes you only get a random hexadecimal value. The example below is very simple and does not need to be discussed. We use cocreateguid to create the guid and convert the last six bytes
In the string. They may be MAC addresses, but they are not necessary.
UUID. cpp
# Include <windows. h>
# Include <iostream>
# Include <conio. h>
Using namespace STD;
Int main ()
{
Cout <"MAC address is :";
// Request a uuid from Com. If the machine has an Ethernet card,
// The last six bytes of UUID (2-7 bytes of data4) should be the MAC address of the local ethernet card.
Guid UUID;
Cocreateguid (& UUID );
// Spit the address out
Char mac_addr [18];
Sprintf (mac_addr, "% 02x: % 02x: % 02x: % 02x: % 02x: % 02x ",
UUID. data4 [2], UUID. data4 [3], UUID. data4 [4],
UUID. data4 [5], UUID. data4 [6], UUID. data4 [7]);
Cout <mac_addr <Endl;
Getch ();
Return 0;
}
4. Use netapi to obtain the nic mac address
First, add # include "nb30.h" to the header file definition"
# Pragma comment (Lib, "netapi32.lib ")
Typedef struct _ astat _
{
Adapter_status adapt;
Name_buffer namebuff [30];
} Astat, * pastat;
You can call this operation to obtain the MAC address of the remote NIC:
Cstring getmacaddress (cstring snetbiosname)
{
Astat adapter;
NCB;
Uchar uretcode;
Memset (& NCB, 0, sizeof (NCB ));
NCB. ncb_command = ncbreset;
NCB. ncb_lana_num = 0;
Uretcode = NetBIOS (& NCB );
Memset (& NCB, 0, sizeof (NCB ));
NCB. ncb_command = ncbastat;
NCB. ncb_lana_num = 0;
Snetbiosname. makeupper ();
Fillmemory (NCB. ncb_callname, ncbnamsz-1, 0x20 );
Strcpy (char *) NCB. ncb_callname, (lpctstr) snetbiosname );
NCB. ncb_callname [snetbiosname. getlength ()] = 0x20;
NCB. ncb_callname [ncbnamsz] = 0x0;
NCB. ncb_buffer = (unsigned char *) & adapter;
NCB. ncb_length = sizeof (adapter );
Uretcode = NetBIOS (& NCB );
Cstring smacaddress;
If (uretcode = 0)
{
Smacaddress. Format (_ T ("% 02x % 02x % 02x % 02x % 02x % 02x "),
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]);
}
Return smacaddress;
}