[Visual C++]MAC address 받아오는 3가지 방법

C,C++ 2010/03/02 00:44
Three ways to get your MAC address

3가지 방법을 샘플로 만들었습니다.
전 기존에 Netbios 의 방법을 사용하여 맥어드레스를 얻었으나, 와이브로나 티로그인, 기타 CDMA,(Wiress LAN, 일명 Wi-Fi) 장치의 맥어드레스를 받을 수 없어, 방법을 찾던중 GetMACAdapters 의 샘플로 맥어드레스를 잡을 수 있었습니다.
각 샘플의 특이점을 파악하시고 이용하시면 좋을듯 보입니다.
참고로 GetMACAdapters 는 플랫폼SDK 설치 후 컴파일 하실 수 있습니다. 아래 코드는 코드그루에서 가져왔습니다.

Method One: UuidCreate

One quick way to find out your MAC address, which is very hacky and I wouldn't recommend would be to create a sequential Uuid. Apparently Microsoft uses your MAC address to help it create a universally unique identifier.

All you have to do is check out bytes 2 through 8 and you are done. The code is below and the downloadable EXE and sample code is listed at the end of the article.

// Fetches the MAC address and prints it
static void GetMACaddress(void)
  unsigned char MACData[6];

  UUID uuid;
  UuidCreateSequential( &uuid );    // Ask OS to create UUID

  for (int i=2; i<8; i++)  // Bytes 2 through 7 inclusive 
                           // are MAC address
    MACData[i - 2] = uuid.Data4[i];

  PrintMACaddress(MACData);         // Print MAC address
This code will only work in Windows 2000/XP since Microsoft replaced UuidCreate in Windows 2000/XP with one that doesn't use the PC's MAC address. UuidCreate with your MAC address can easily be considered a security risk since you are distributing your ethernet card's address.

Microsoft created UuidCreateSequential in Win2K and XP to do what the old UuidCreate did on Windows 95/98/Me. The current UuidCreate in Windows 2000/XP is not composed of a number which includes the MAC address of your primary NIC, hence they moved over that functionality to UuidCreateSequential. On the older OSes you may still use the UuidCreate function to obtain its MAC address.

This example only supports one NIC card on your PC.

Method Two: Use NetBIOS

This solution is a lot more complicated then the final solution. It supports multiple NIC cards, but requires NetBIOS to be installed on the computer. It also requires you to have the cable connected to a valid NetBIOS network. It works great under all OSes including 95/98/Me/NT/2000/XP.

// Fetches the MAC address and prints it
static void GetMACaddress(void)
  unsigned char MACData[8];      // Allocate data structure 
                                 // for MAC (6 bytes needed)

  WKSTA_TRANSPORT_INFO_0 *pwkti; // Allocate data structure 
                                 // for NetBIOS
  DWORD dwEntriesRead;
  DWORD dwTotalEntries;
  BYTE *pbBuffer;
  // Get MAC address via NetBIOS's enumerate function
  NET_API_STATUS dwStatus = NetWkstaTransportEnum(
   NULL,                 // [in]  server name
   0,                    // [in]  data structure to return
   &pbBuffer,            // [out] pointer to buffer
   MAX_PREFERRED_LENGTH, // [in]  maximum length
   &dwEntriesRead,       // [out] counter of elements 
                         //       actually enumerated
   &dwTotalEntries,      // [out] total number of elements 
                         //       that could be enumerated
   NULL);                // [in/out] resume handle
  assert(dwStatus == NERR_Success);

  pwkti = (WKSTA_TRANSPORT_INFO_0 *)pbBuffer; // type cast the buffer

  for(DWORD i=1; i< dwEntriesRead; i++)  // first address is
                                            // 00000000, skip it
  {                                         // enumerate MACs & print
    swscanf((wchar_t *)pwkti[i].wkti0_transport_address,

  // Release pbBuffer allocated by above function
  dwStatus = NetApiBufferFree(pbBuffer);
  assert(dwStatus == NERR_Success);
As you can tell converting the wide string which is returned from NetWkstaTransportEnum to an BYTE based array is a mess in itself. The great thing about this is that it goes through all the NICs located on your PC. You are also able to easily query other people's PCs by passing in a NetBIOS computer name as the first parameter to this function.

Method Three: Use GetAdaptersInfo

The cleanest way I could find to get all the MAC addresses located on a PC was to use the GetAdaptersInfo method. It includes almost as much information as IPCONFIG /ALL including your DHCP server, Gateway, IP address list, subnet mask and WINS servers. It also enumerates all the NICs on your PC and is supported in 95/98/Me/NT/2000/XP. Finally it also works if your NICs are not connected to valid networks (eg. wires are not even hooked up), but the NICs do have to be "enabled" in Windows.

// Fetches the MAC address and prints it
static void GetMACaddress(void)
  IP_ADAPTER_INFO AdapterInfo[16];       // Allocate information 
                                         // for up to 16 NICs
  DWORD dwBufLen = sizeof(AdapterInfo);  // Save memory size of buffer

  DWORD dwStatus = GetAdaptersInfo(      // Call GetAdapterInfo
    AdapterInfo,                 // [out] buffer to receive data
    &dwBufLen);                  // [in] size of receive data buffer
  assert(dwStatus == ERROR_SUCCESS);  // Verify return value is 
                                      // valid, no buffer overflow

  PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo; // Contains pointer to
                                               // current adapter info
  do {
    PrintMACaddress(pAdapterInfo->Address); // Print MAC address
    pAdapterInfo = pAdapterInfo->Next;    // Progress through 
                                          // linked list
  while(pAdapterInfo);                    // Terminate if last adapter
I probably should mention that statically allocating an array for up to 16 NICs is not the best way to do this. It is a quick and dirty solution that should show you essentially how to get and enumerate all the MAC addresses on your PC.

Method Four: The Miniport Driver

I never implemented this method since it required some very low level coding. It also probably wouldn't be a good method since you are directly talking to the underlying NDIS miniport driver. The basic concept of this is to hit the miniport driver with an OIS query of OID_802_3_CURRENT_ADDRESS. This should return a buffer with the current MAC address.

The way problem with this solution is that there is no easy way to do this from user mode, which is exactly the mode we are coding all our apps in, this is as opposed to kernal mode which drivers reside in.


Although there are no functions named GetMACaddress in the Win32 API, ATL, MFC, or C#, it is fairly easy to find and associate your MAC address with its related IP addresses by calling GetAdaptersInfo(). GetAdaptersInfo is located in the Platform SDK.

I hope you had an interesting time reading this article.

원본링크 : http://www.codeguru.com/cpp/i-n/network/networkinformation/article.php/c5451

저작자 표시 비영리 변경 금지
크리에이티브 커먼즈 라이선스
Creative Commons License

티스토리 툴바