××××××××××××××××××××××××× Get a remote network card MAC address. ××××××××××××××××××××××××××
First, add #include "nb30.h" #pragma Comment (lib, "netapi32.lib") typefma command (lib); name_buffer namebuff [30];} ASTAT, * PASTAT;
You can use it to get the remote network card MAC address: CString getMacadDress (cstring snetbiosname) {Astat Adapter; NCB 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;}
××××××××××××××××××××××××××××××××××××× NetBIOS API gets network card MAC address × × ×××××××××××××××××××××××××××××××××××××
#include "nb30.h" #pragma Comment (lib, "netapi32.lib")
Typedef struct tagmac_address {BYTE B1, B2, B3, B4, B5, B6;} Mac_Address, * lpmac_address
typedef struct tagASTAT {ADAPTER_STATUS adapt; NAME_BUFFER NameBuff [30];} ASTAT, * LPASTAT; UCHAR GetAddressByIndex (int lana_num, ASTAT & Adapter) {NCB ncb; UCHAR uRetCode; memset (& ncb, 0, sizeof (ncb)); ncb.ncb_command = Ncbreset; ncb.ncb_lana_num = lana_num; // Specify the NIC number, first send an ncbreset command to the selected network card for initialization uretcode = NetBIOS (& NCB); MEMSET (& NCB, 0, SIZEOF (NCB)); NCB. NCB_COMMAND = NCBASTAT; NCB.NCB_LANA_NUM = LANA_NUM; / / Specifies the NIC NPC STRCPY ((char *) ncb.ncb_callname, "*"); ncb.ncb_buffer = (unsigned char *) & adapter; // Specify the returned information stored Ncb.ncb_length = sizeof (adapter); // Next, the ncbastat command can be sent to obtain information URETCODE = NetBIOS (& NCB); Return URetcode;}
int GetMAC (LPMAC_ADDRESS pMacAddr) {NCB ncb; UCHAR uRetCode; int num = 0; 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 is a number of NRETCODE = NetBIOS (& NCB) of each network card number, IF (uretcode == 0) {Num = LANA_ENUM.LENGTH; / / For each network card, with its NIC number as the input number, obtain its MAC address for (INT i = 0; I ======= Call: Mac_address m_macaddr [10]; //, for example, up to 10 network card INT n = getmac (m_macaddr); // get the number of network cards Tchar Szaddr [128]; WSPrintf (szaddr, _t ("% 02x-% 02x-% 02x-% 02x -% 02x-% 02x "), m_macaddr [0] .b1, m_macaddr [0] .b2, m_macaddr [0] .b3, m_macaddr [0] .b4, m_macaddr [0] .b5, m_macaddr [0] .b6 ); _TCSUPR (SZADDR); // This can obtain a MAC address string such as 00-E0-AA-AA-AA-AA. ××××××××××××××××××××××××××××××××××××× i 的 h h h h h 地址 网 地址 地址 地址 地址××××××××××××××××××××××××××××××××××××× Oh, the most common way is in the final GetAdaptersInfo function Here is most of the information of a network card, friends can choose ^ _ ^ as appropriate #include Typedef struct tagadapterinfo {char szdevicename [128]; // Name char szipaddrstr [16]; // ip char szhwaddrstr [18]; // Mac DWORD dwindex; // number} info_adapter, * pinfo_adapter; INFO_ADAPTER AdapterList [10]; // NIC list, such as ten / ********************************* *************************************************** Name & params :: * formatMactostr * (* LPSTR LPHWADDRSTR: Displayed "-" Mac string * unsigned char * hwaddr: incoming mac string *) * purpose: * Transfers the user input MAC address characters into corresponding formats **** *********************************************************** ***************************** / VOID FORMATMACTOSTR (LPSTR LPHWADDRSTR, Const unsigned char * hwaddr) {INT i; short temp; char szstr [3]; STRCPY (LPHWADDRSTR, ""); for (i = 0; i <6; i) {temp = (Short) (* (hwaddr i)); _ITOA (TEMP, SZSTR, 16); IF (Strlen SZSTR) == 1) STRCAT (LPHWADDRSTR, "0"); STRCAT (LPHWADDRSTR, SZSTR); IF (i <5) strcat (lphwaddrstr, "-"); // plus-}} Filling structure // void GetAdapterInfo () {char tempChar; ULONG uListSize = 1; PIP_ADAPTER_INFO pAdapter; // define the structure of the memory card information PIP_ADAPTER_INFO int nAdapterIndex = 0; DWORD dwRet = GetAdaptersInfo ((PIP_ADAPTER_INFO) & tempChar, & uListSize); // key functions if (dwRet == ERROR_BUFFER_OVERFLOW) {PIP_ADAPTER_INFO pAdapterListBuffer = (PIP_ADAPTER_INFO) new (char [uListSize]); dwRet = GetAdaptersInfo (pAdapterListBuffer, & uListSize); if (dwRet == ERROR_SUCCESS) {pAdapter = pAdapterListBuffer; while (pAdapter) // pieces Lift network card {cstring straTemp = padapter-> adapter; // NIC name strTemp = "// device // npf_" strTemp; // plus prefix STRCPY (AdapterList [NADAPTERINDEX] .szdeviceName, strTemp); STRCPY (AdapterList [NadapterIndex] .szipaddrStr, Padapter-> ipaddresslist.ipaddress.string); // ip FormatMactostr (Adapterlist [NadapterIndex] .szhwaddrstr, Padapter-> Address); // Mac !!!!!!!!!!!!!!!!! Adapterlist [NadapterIndex] .dwindex = Padapter-> index; // number Padapter = padapter-> next; DELETE PADAPTERLISTBUFFER;}}}