매우 좋은 예제인것 같습니다.
사용하실때는 여러대의 어답터가 연결되어 있을 경우의 처리를 추가해 주시는 것도 고려해보세요.


// GetIfTale 을 이용하는 방법. 성능이 가장 좋다.
// ppp의 맥어드레스와 네트워크 상의 통계 정도도 얻어 올수 있다.
#include "Iphlpapi.h" // Iphlpapi.lib 링크 필요
CString GetMacAddress()
{
    CString strMacAddress = "";
    CString strEthernet = "";
    CString strPppSlip = "";
    CString strOthers = "";
    DWORD dwError;
    DWORD sizeReq = 0 ;
    PMIB_IFTABLE pInfo = NULL;
 
    ::GetIfTable(NULL, &sizeReq, FALSE) ;
 
    pInfo = (PMIB_IFTABLE) new BYTE [sizeReq] ;
    memset (pInfo, 0, sizeReq) ;
 
    DWORD sizeToUse = sizeReq ;
 
    dwError =  ::GetIfTable( (PMIB_IFTABLE)pInfo,&sizeToUse, FALSE);
    if(dwError != NO_ERROR)
    {
        if(pInfo != NULL)
            delete[] (PMIB_IFTABLE)pInfo;
        pInfo = NULL;
        return strMacAddress;
    }
 
    for( unsigned int index = 0; index < ((PMIB_IFTABLE)pInfo)->dwNumEntries; index ++ )
    {
        MIB_IFROW& details = ((PMIB_IFTABLE)pInfo)->table[index];
 
        if(details.dwPhysAddrLen == 6)
        {
            if(details.dwType == MIB_IF_TYPE_ETHERNET)
            {
                if(strEthernet.IsEmpty())
                {
                    strEthernet.Format("%02x%02x%02x%02x%02x%02x",
                        details.bPhysAddr[0],details.bPhysAddr[1],details.bPhysAddr[2],
                        details.bPhysAddr[3],details.bPhysAddr[4],details.bPhysAddr[5]);
                }
            }
            else if(details.dwType == MIB_IF_TYPE_PPP || details.dwType == MIB_IF_TYPE_SLIP)
            {
                if(strPppSlip.IsEmpty())
                {
                    strPppSlip.Format("%02x%02x%02x%02x%02x%02x",
                        details.bPhysAddr[0],details.bPhysAddr[1],details.bPhysAddr[2],
                        details.bPhysAddr[3],details.bPhysAddr[4],details.bPhysAddr[5]);
                }
            }
            else
            {
                if(strOthers.IsEmpty())
                {
                    strOthers.Format("%02x%02x%02x%02x%02x%02x",
                        details.bPhysAddr[0],details.bPhysAddr[1],details.bPhysAddr[2],
                        details.bPhysAddr[3],details.bPhysAddr[4],details.bPhysAddr[5]);
                }
            }
        }
    }
 
    if(strEthernet.IsEmpty() == FALSE)
        strMacAddress = strEthernet;
    else if(strPppSlip.IsEmpty() == FALSE)
        strMacAddress = strPppSlip;
    else
        strMacAddress = strOthers;
 
    if(pInfo != NULL)
        delete[] (PMIB_IFTABLE)pInfo;
 
    return strMacAddress;
}
 


// GetAdaptersInfo 를 이용하는 방법
// ADSL 내장형 모뎀에서 사용하는 MAC Address 도 얻을 수 있다.
#include "Iphlpapi.h" // Iphlpapi.lib 링크 필요
CString GetMacAddress()
{
    CString strMacAddress = "";
    PIP_ADAPTER_INFO pAdapterInfo;
    PIP_ADAPTER_INFO pAdapter = NULL;
    DWORD dwRetVal = 0;
 
    pAdapterInfo =(IP_ADAPTER_INFO *)(new BYTE[sizeof(IP_ADAPTER_INFO)]);
    ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
 
    if (GetAdaptersInfo( pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
        if(pAdapterInfo != NULL)
            delete[] pAdapterInfo;
        pAdapterInfo =(IP_ADAPTER_INFO *)(new BYTE[ulOutBufLen]);
    }
 
    if ((dwRetVal = GetAdaptersInfo( pAdapterInfo, &ulOutBufLen)) == ERROR_SUCCESS) {
        pAdapter = pAdapterInfo;
        while (pAdapter) {
            strMacAddress.Format("%02x%02x%02x%02x%02x%02x",
                pAdapter->Address[0],
                pAdapter->Address[1],
                pAdapter->Address[2],
                pAdapter->Address[3],
                pAdapter->Address[4],
                pAdapter->Address[5] );
            if(strMacAddress != "")
            {
                break;
            }
            pAdapter = pAdapter->Next;
        }
    }
    if(pAdapterInfo != NULL)
        delete[] pAdapterInfo;
 
    return strMacAddress;
}

 
// NetBIOS를 이용하는 방법
// Fetches the MAC address and prints it
static void GetMacAddress(void)
{
    unsigned char MACData[8];
 
    WKSTA_TRANSPORT_INFO_0 *pwkti;
 
    DWORD dwEntriesRead;
    DWORD dwTotalEntries;
    BYTE *pbBuffer;
 
    NET_API_STATUS dwStatus = NetWkstaTransportEnum(
        NULL,
        0,
        &pbBuffer,
        MAX_PREFERRED_LENGTH,
        &dwEntriesRead,
        &dwTotalEntries,
        NULL);
    assert(dwStatus == NERR_Success);
 
    pwkti = (WKSTA_TRANSPORT_INFO_0 *)pbBuffer;
 
    for(DWORD i=1; i< dwEntriesRead; i++)
    {
        swscanf((wchar_t *)pwkti[i].wkti0_transport_address,
            L"%2hx%2hx%2hx%2hx%2hx%2hx",
            &MACData[0],
            &MACData[1],
            &MACData[2],
            &MACData[3],
            &MACData[4],
            &MACData[5]);
        PrintMACaddress(MACData);
    }
 
    dwStatus = NetApiBufferFree(pbBuffer);
    assert(dwStatus == NERR_Success);
}

 

Posted by 띠깜
,