Get MAC address

Source: Internet
Author: User

[Cpp]
// Define the NIC type
Typedef DWORD BFSYS_MAC_T;
# Define BFSYS_MAC_ALL 0 xFFFFFFFF // All NICs
# Define BFSYS_MAC_PHYSICAL 0x00000001 // physical Nic
# Define BFSYS_MAC_WIRELESS 0x00000002 // wireless Nic
# Define BFSYS_MAC_VIRTUAL 0x00000004 // virtual network card (for example, virtual network card with virtual machine)
# Define BFSYS_MAC_UNKNOWN 0x80000000 // unknown Nic
 
// Define the MAC address structure and array of the NIC
Typedef struct _ BFMAC_ADDR_STR _
{
BFSYS_MAC_T Type;
DWORD AddressLength;
BYTE Address [32];
} BFMACADDR, * PBFMACADDR;
Typedef vector <BFMACADDR> BFMacAddrArray;
 
// Obtain the NIC address and return the total number of NICs of the corresponding type
BFLIB_API
DWORD
BF_GetMacAddr (
IN BFSYS_MAC_T type, // type of the NIC to be retrieved
OUT BFMacAddrArray * pAddrAry = NULL // output the address information of the corresponding Nic type
);

// Define the NIC type
Typedef DWORD BFSYS_MAC_T;
# Define BFSYS_MAC_ALL 0 xFFFFFFFF // All NICs
# Define BFSYS_MAC_PHYSICAL 0x00000001 // physical Nic
# Define BFSYS_MAC_WIRELESS 0x00000002 // wireless Nic
# Define BFSYS_MAC_VIRTUAL 0x00000004 // virtual network card (for example, virtual network card with virtual machine)
# Define BFSYS_MAC_UNKNOWN 0x80000000 // unknown Nic

// Define the MAC address structure and array of the NIC
Typedef struct _ BFMAC_ADDR_STR _
{
BFSYS_MAC_T Type;
DWORD AddressLength;
BYTE Address [32];
} BFMACADDR, * PBFMACADDR;
Typedef vector <BFMACADDR> BFMacAddrArray;

// Obtain the NIC address and return the total number of NICs of the corresponding type
BFLIB_API
DWORD
BF_GetMacAddr (
IN BFSYS_MAC_T type, // type of the NIC to be retrieved
OUT BFMacAddrArray * pAddrAry = NULL // output the address information of the corresponding Nic type
);
[Cpp]
// Obtain the NIC address and return the total number of NICs of the corresponding type
BFLIB_API
DWORD
BF_GetMacAddr (
IN BFSYS_MAC_T type, // type of the NIC to be retrieved
OUT BFMacAddrArray * pAddrAry // output the address information of the corresponding Nic type
)
{
// Clear the previous information
If (pAddrAry! = NULL)
{
PAddrAry-> clear ();
}
 
// Obtain the Adapter Information
ULONG ulSize = sizeof (IP_ADAPTER_INFO );
BYTE * pBuf = new BYTE [ulSize];
If (pBuf = NULL)
{
Return 0;
}
PIP_ADAPTER_INFO pIPAdapterInfo = (PIP_ADAPTER_INFO) pBuf;
DWORD dwRet =: GetAdaptersInfo (pIPAdapterInfo, & ulSize );
 
// Apply for memory again if the Buffer Overflows
If (ERROR_BUFFER_OVERFLOW = dwRet)
{
Delete [] pBuf;
PBuf = NULL;
PIPAdapterInfo = NULL;
PBuf = new BYTE [ulSize];
If (pBuf = NULL)
{
Return 0;
}
PIPAdapterInfo = (PIP_ADAPTER_INFO) pBuf;
DwRet =: GetAdaptersInfo (pIPAdapterInfo, & ulSize );
}
 
// An error occurred while obtaining the Adapter information.
If (dwRet! = ERROR_SUCCESS)
{
Delete [] pBuf;
PBuf = NULL;
PIPAdapterInfo = NULL;
Return 0;
}
 
// Traverse all Nic Information
DWORD dwTotalCount = 0;
BFMACADDR MacAddr;
While (pIPAdapterInfo! = NULL)
{
// Obtain the MAC address information
Memset (& MacAddr, 0, sizeof (MacAddr ));
MacAddr. AddressLength = pIPAdapterInfo-> AddressLength;
For (UINT I = 0; I <pIPAdapterInfo-> AddressLength; I ++)
{
MacAddr. Address [I] = pIPAdapterInfo-> Address [I];
}
 
// Whether the network card is wireless
If (pIPAdapterInfo-> Type = 71)
{
MacAddr. Type = BFSYS_MAC_WIRELESS;
}
// Whether it is a physical Nic
Else if (pIPAdapterInfo-> Type = MIB_IF_TYPE_ETHERNET
& Strstr (pIPAdapterInfo-> Description, "PCI")> 0
)
{
MacAddr. Type = BFSYS_MAC_PHYSICAL;
}
// Whether the network interface is virtual
Else if (pIPAdapterInfo-> Type = MIB_IF_TYPE_ETHERNET
& Strstr (pIPAdapterInfo-> Description, "Virtual")> 0
)
{
MacAddr. Type = BFSYS_MAC_VIRTUAL;
}
// Other NICs
Else
{
MacAddr. Type = BFSYS_MAC_UNKNOWN;
}
 
// Required Nic type
If (type & MacAddr. Type) = MacAddr. Type)
{
DwTotalCount + = 1;
If (pAddrAry! = NULL)
{
PAddrAry-> push_back (MacAddr );
}
}
 
// Traverse the next Adapter
PIPAdapterInfo = pIPAdapterInfo-> Next;
}
 
// Release the memory
Delete [] pBuf;
PBuf = NULL;
PIPAdapterInfo = NULL;
 
// Returns the number of retrieved results.
Return dwTotalCount;
}

// Obtain the NIC address and return the total number of NICs of the corresponding type
BFLIB_API
DWORD
BF_GetMacAddr (
IN BFSYS_MAC_T type, // type of the NIC to be retrieved
OUT BFMacAddrArray * pAddrAry // output the address information of the corresponding Nic type
)
{
// Clear the previous information
If (pAddrAry! = NULL)
{
PAddrAry-> clear ();
}

// Obtain the Adapter Information
ULONG ulSize = sizeof (IP_ADAPTER_INFO );
BYTE * pBuf = new BYTE [ulSize];
If (pBuf = NULL)
{
Return 0;
}
PIP_ADAPTER_INFO pIPAdapterInfo = (PIP_ADAPTER_INFO) pBuf;
DWORD dwRet =: GetAdaptersInfo (pIPAdapterInfo, & ulSize );

// Apply for memory again if the Buffer Overflows
If (ERROR_BUFFER_OVERFLOW = dwRet)
{
Delete [] pBuf;
PBuf = NULL;
PIPAdapterInfo = NULL;
PBuf = new BYTE [ulSize];
If (pBuf = NULL)
{
Return 0;
}
PIPAdapterInfo = (PIP_ADAPTER_INFO) pBuf;
DwRet =: GetAdaptersInfo (pIPAdapterInfo, & ulSize );
}

// An error occurred while obtaining the Adapter information.
If (dwRet! = ERROR_SUCCESS)
{
Delete [] pBuf;
PBuf = NULL;
PIPAdapterInfo = NULL;
Return 0;
}

// Traverse all Nic Information
DWORD dwTotalCount = 0;
BFMACADDR MacAddr;
While (pIPAdapterInfo! = NULL)
{
// Obtain the MAC address information
Memset (& MacAddr, 0, sizeof (MacAddr ));
MacAddr. AddressLength = pIPAdapterInfo-> AddressLength;
For (UINT I = 0; I <pIPAdapterInfo-> AddressLength; I ++)
{
MacAddr. Address [I] = pIPAdapterInfo-> Address [I];
}

// Whether the network card is wireless
If (pIPAdapterInfo-> Type = 71)
{
MacAddr. Type = BFSYS_MAC_WIRELESS;
}
// Whether it is a physical Nic
Else if (pIPAdapterInfo-> Type = MIB_IF_TYPE_ETHERNET
& Strstr (pIPAdapterInfo-> Description, "PCI")> 0
)
{
MacAddr. Type = BFSYS_MAC_PHYSICAL;
}
// Whether the network interface is virtual
Else if (pIPAdapterInfo-> Type = MIB_IF_TYPE_ETHERNET
& Strstr (pIPAdapterInfo-> Description, "Virtual")> 0
)
{
MacAddr. Type = BFSYS_MAC_VIRTUAL;
}
// Other NICs
Else
{
MacAddr. Type = BFSYS_MAC_UNKNOWN;
}

// Required Nic type
If (type & MacAddr. Type) = MacAddr. Type)
{
DwTotalCount + = 1;
If (pAddrAry! = NULL)
{
PAddrAry-> push_back (MacAddr );
}
}

// Traverse the next Adapter
PIPAdapterInfo = pIPAdapterInfo-> Next;
}

// Release the memory
Delete [] pBuf;
PBuf = NULL;
PIPAdapterInfo = NULL;

// Returns the number of retrieved results.
Return dwTotalCount;
}
Use

# Include <Iphlpapi. h>
# Pragma comment (lib, "Iphlpapi. lib") // you need to add the Iphlpapi. lib Library

 

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.