Get the system network card MAC address method three

xiaoxiao2021-03-06  74

Third Method - The third method I want to discuss using the SNMP extended API is to use Windows SNMP (Simple Network Management Protocol) to get the MAC address. In my experience, this agreement is simple. The code is also forwarded. Basic steps and NetBIOS: 1 "Net Card List 2" Query the Type of Each Card and MAC Address 3 "Save the current network card, I personally know much about SNMP, but I just said that the code is very clear.

SNMP.cpp #include #include #include

Typedef Bool (WinApi * Psnmpextensioninit) (Out Handle * Hpollfortrapevent, Out AsnobjectiDentifier * SupportedView);

typedef bool (WINAPI * pSnmpExtensionTrap) (OUT AsnObjectIdentifier * enterprise, OUT AsnInteger * genericTrap, OUT AsnInteger * specificTrap, OUT AsnTimeticks * timeStamp, OUT RFC1157VarBindList * variableBindings);

Typeryf Bool (WinAPI * PsnmpextensionQuery) (in out RFC1157VARBINDLIST * VARIABIABINDINGS, OUT Asninteger * ErrorStatus, Out AsnIndeger * Errorindex);

Typedef Bool (WinAPI * PsnmpextensionInitex) (Out AsnObjectIdentifier * SupportedView);

void main () {HINSTANCE m_hInst; pSnmpExtensionInit m_Init; pSnmpExtensionInitEx m_InitEx; pSnmpExtensionQuery m_Query; pSnmpExtensionTrap m_Trap; HANDLE PollForTrapEvent; AsnObjectIdentifier SupportedView; UINT OID_ifEntryType [] = {1, 3, 6, 1, 2, 1, 2, 2, 1, 3}; uint oid_ifentrynum [] = {1, 3, 6, 1, 2, 1, 2, 1}; uint oid_ipmacentaddr [] = {1, 3, 6, 1, 2, 1, 2, 2, 1, 6}; AsnObjectIdentifier MIB_ifMACEntAddr = {sizeof (OID_ipMACEntAddr) sizeof (UINT), OID_ipMACEntAddr}; AsnObjectIdentifier MIB_ifEntryType = {sizeof (OID_ifEntryType) sizeof (UINT), OID_ifEntryType}; AsnObjectIdentifier MIB_ifEntryNum = {sizeof (OID_ifEntryNum) sizeof (UINT), OID_ifEntryNum} ; RFC1157VarBindList varBindList; RFC1157VarBind varBind [2]; AsnInteger errorStatus; AsnInteger errorIndex; AsnObjectIdentifier MIB_NULL = {0, 0}; int ret; int dtmp; int i = 0, j = 0; bool found = false; char TempEthernet [13] M_init = null; m_initex = null; m_query = null; m_trap = null; / * Load SNMP DLL and acquire an instance handle * / m_hinst = loadingLibrary ("inetmib 1.dll "); if (m_hInst <(HINSTANCE) HINSTANCE_ERROR) {m_hInst = NULL; return;} m_Init = (pSnmpExtensionInit) GetProcAddress (m_hInst," SnmpExtensionInit "); m_InitEx = (pSnmpExtensionInitEx) GetProcAddress (m_hInst," SnmpExtensionInitEx ") ; m_Query = (pSnmpExtensionQuery) GetProcAddress (m_hInst, "SnmpExtensionQuery"); m_Trap = (pSnmpExtensionTrap) GetProcAddress (m_hInst, "SnmpExtensionTrap"); m_Init (GetTickCount (), & PollForTrapEvent, & SupportedView);

/ * Initialize the variable list of the M_Query query results * / varbindlist.list = varbind; varbindlist.list = varbind; varbind [0] .name = mib_null; varbind [1] .name = mib_null;

/ * Copy the OID and find the number of entries in the interface table * / varBindList.len = 1; / * Only retrieving one item * / SNMP_oidcpy (& [0] .name, & MIB_ifEntryNum varBind); ret = m_Query (ASN_RFC1157_GETNEXTREQUEST, & varBindList, & ErrorStatus, & Errorindex; Printf ("# of adapters in this system:% in", varbind [0] .value.asnvalue.number); varbindlist.len = 2; / * Copy OID IFTYPE - Interface Type * / SNMP_OIDCPY & varbind [0] .name, & mib_ifenTryte;

/ * Copy OID IFPHYSADDRESS- Physical Address * / SNMP_OIDCPY (& Varbind [1] .Name, & MIB_IFMACENTDR);

Do {

/ * Submit the query, and the result will be loaded into VarbindList. It can be expected that the number of cyclic calls and the number of interface cards in the system are equal * / RET = M_Query (asn_rfc1157_getnextrequest, & varbindlist, & errorstatus, & errorindex); if (! RET) RET = 1; Else / * confirm the correct return type * / Ret = SNMP_OIDNCMP (& varbind [0] .name, & mib_ifenTrytype, MIB_IB_IFENTRYTYPE.IDLENGTH); if (! RET) {J ; DTMP = varbind [0] .value.asnvalue.Number; Printf ("interface #% i type:% in" , J, DTMP);

/ * TYPE 6 Describes EtherNet Interfaces * / if (DTMP == 6) {

/ * Confirm that we have obtained address * / return = snmp_oidncmp (& varbind [1] .name, & mib_ifmacentaddr, mib_ifmacentaddr.idlength; if ((! Ret) && (Varbind [1] .Value.asnvalue.address.Stream! = NULL) {IF ((varbind [1] .value.asnvalue.address.Stream [0] == 0x44) && (varbind [1] .value.asnvalue.address.stream [1] == 0x45) && Varbind [1] .Value.asnvalue.address.Stream [2] == 0x53) && (varbind [1] .value.asnvalue.address.Stream [3] == 0x54) && (varbind [1] .value.asnvalue .address.Stream [4] == 0x00)) {/ * ignore all dial-up network interface card * / printf ("Interface #% I is a dun adaptern", J); Continue;} IF ((Varbind [1] .value.asnvalue.address.Stream [0] == 0x00) && (varbind [1] .value.asnvalue.address.Stream [1] == 0x00) && (varbind [1] .view.asnvalue.address.Stream [2] == 0x00) && (varbind [1] .value.asnvalue.address.Stream [3] == 0x00) && (varbind [1] .value.asnvalue.address.Stream [4] == 0x00) && (Varbind [1] .Value.asnvalue.address.Stream [5] == 0x00)) {/ * ignores NULL addresses returned by other network interface cards * / printf ("Interface #% i is a null addressn", J); Continue;} sprintf (TemPethernet, "% 02x% 02x% 02x% 02x% 02x% 02X ", varbind [1] .value.asnvalue.address.Stream [0], varbind [1] .Value.asnvalue.address.Stream [1], varbind [1] .value.asnvalue. Address.Stream [2], Varbind [1] .value.asnvalue.address.Stream [3], Varbind [1] .Value.asnvalue.Address.Stream [4], varbind [1] .value.asnvalue.address. Stream [5]); Printf ("Mac Address of Interface #% i:% SN", J, Tempethernet);}}}} while (! RET); / * Error termination. * / getch (); freelibrary (m_hinst); / * Release binding * / snmp_freevarbind (& varbind [0]); snmp_freevarbind (& varbind [1]);}

转载请注明原文地址:https://www.9cbs.com/read-93761.html

New Post(0)