ip/ mac 주소 얻기
const char* get_mac()
{
DWORD dwRetVal = 0;
PIP_ADAPTER_INFO pai = 0;
ULONG* pul = 0;
static char buf [20] ={};
ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO);
pai = (IP_ADAPTER_INFO *)calloc(sizeof (IP_ADAPTER_INFO), 1);
if (pai == NULL) {
printf( "Error allocating memory needed to call GetAdaptersinfo\n");
return 0;
}
if (GetAdaptersInfo(pai, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
pul = new ULONG[ulOutBufLen];
pai = PIP_ADAPTER_INFO(pul);
if (pai == NULL) {
printf( "Error allocating memory needed to call GetAdaptersinfo\n");
return 0;
}
}
if ((dwRetVal = GetAdaptersInfo(pai, &ulOutBufLen)) == NO_ERROR)
{
while( pai )
{
if( strcmp( pai->IpAddressList.IpAddress.String, "0.0.0.0" ))
break;
pai = pai->Next;
}
BYTE addr [MAX_ADAPTER_ADDRESS_LENGTH] = {};
if(pai)
{
memcpy (addr, pai->Address, sizeof (addr));
sprintf_s( buf, sizeof(buf), "%0.2X-%0.2X-%0.2X-%0.2X-%0.2X-%0.2X", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5] );
}
if(pul)
delete [] pul;
}else{
printf( "Failed to get an AdapterInfo!" );
if(pai)
free(pai);
return 0;
}
return buf;
}
void get_ip()
{
WORD wVersionRequested;
WSADATA wsaData;
char name[255]= {0,};
CString ip;
PHOSTENT hostinfo;
wVersionRequested = MAKEWORD(2,0);
try{
if(WSAStartup(wVersionRequested, & wsaData))
throw WSAGetLastError();
if( gethostname(name, sizeof(name)) || !(hostinfo = gethostbyname(name)))
throw WSAGetLastError();
ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
printf("\n ip : %s \n", ip.GetString());
WSACleanup();
}
catch(int n)
{
WSACleanup();
printf("WSAGetLastError : %d\n",n);
}
}