윈도우 Win32에서 MAC 주소를 알아내려면 다음과 같이 하면된다.
노트북을 사용한다고 가정하여 어댑터들 중에 "Wi-Fi"를 검색한다.
// 윈속 2를 반드시 include해야 한다.
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
#include <iphlpapi.h>
#include <iptypes.h>
// Link with Iphlpapi.lib
#pragma comment(lib, "IPHLPAPI.lib")
#define WORKING_BUFFER_SIZE 15000
#define MAX_TRIES 3
char* get_mac_adress()
{
int family = AF_INET;
PIP_ADAPTER_ADDRESSES pAddresses = NULL;
ULONG outBufLen = 0;
ULONG Iterations = 0;
// Allocate a 15 KB buffer to start with.
outBufLen = WORKING_BUFFER_SIZE;
DWORD dwRetVal = 0;
// Set the flags to pass to GetAdaptersAddresses
ULONG flags = GAA_FLAG_INCLUDE_PREFIX;
pAddresses = (IP_ADAPTER_ADDRESSES *)malloc(outBufLen);
dwRetVal = GetAdaptersAddresses(family, flags, NULL, pAddresses, &outBufLen);
char *ret = new char[128];
memset(ret, 0, 128);
while (pAddresses) {
if (wcsstr(pAddresses->FriendlyName, L"Wi-Fi")) {
for (int i = 0; i < 6; i++) {
sprintf(ret, "%s%02x", ret, pAddresses->PhysicalAddress[i]);
}
}
pAddresses = pAddresses->Next;
}
return ret;
}
int _tmain(int argc, _TCHAR* argv[])
{
char *mac = get_mac_adress();
printf("mac address: %s\n", mac);
delete[] mac;
return 0;
}
No comments:
Post a Comment