원본글에 있는 프로젝트를 받아 간단하게 함수 형태로 변환한 소스.
WOL 등에 필요한 Mac Address 를 구하기 위해.
원본글 : 찾아가기
----------------------------------
#include
#pragma comment(lib, "iphlpapi.lib")
CString GetMacByIP(const CString& strIP)
{
CAsyncSocket sock;
sock.Create(1000, SOCK_DGRAM);
sock.SendTo(&sock, 1, 1000, strIP);
CString strMAC;
PMIB_IPNETTABLE pIPNetTable;
pIPNetTable = (MIB_IPNETTABLE*) malloc( sizeof( MIB_IPNETTABLE) );
DWORD dwSize = 0, dwRetVal=0;
if ( (dwRetVal=GetIpNetTable(pIPNetTable, &dwSize, 0)) == ERROR_INSUFFICIENT_BUFFER)
{
free( pIPNetTable );
pIPNetTable = (MIB_IPNETTABLE *) malloc ( dwSize );
}
else
{
CString strErr;
LPTSTR lpMsgBuf;
FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
WSAGetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL );
OutputDebugString(lpMsgBuf);
LocalFree( lpMsgBuf );
}
// Make a second call to GetIpAddrTable to get the
// actual data we want
if ( (dwRetVal = GetIpNetTable( pIPNetTable, &dwSize, 0 )) == NO_ERROR )
{
for (UINT i=0;idwNumEntries;i++)
{
if ((pIPNetTable->table[i].dwAddr) == inet_addr(strIP))
{
BYTE *pt = (BYTE*)&pIPNetTable->table[i].bPhysAddr;
for (int j=0;j<6;j++)
strMAC.AppendFormat(_T("%02X"),*pt++);
break;
}
}
}
//Cleanup
if(pIPNetTable)
{
free( pIPNetTable );
pIPNetTable = NULL;
}
return strMAC;
}
WOL 등에 필요한 Mac Address 를 구하기 위해.
원본글 : 찾아가기
----------------------------------
#include
#pragma comment(lib, "iphlpapi.lib")
CString GetMacByIP(const CString& strIP)
{
CAsyncSocket sock;
sock.Create(1000, SOCK_DGRAM);
sock.SendTo(&sock, 1, 1000, strIP);
CString strMAC;
PMIB_IPNETTABLE pIPNetTable;
pIPNetTable = (MIB_IPNETTABLE*) malloc( sizeof( MIB_IPNETTABLE) );
DWORD dwSize = 0, dwRetVal=0;
if ( (dwRetVal=GetIpNetTable(pIPNetTable, &dwSize, 0)) == ERROR_INSUFFICIENT_BUFFER)
{
free( pIPNetTable );
pIPNetTable = (MIB_IPNETTABLE *) malloc ( dwSize );
}
else
{
CString strErr;
LPTSTR lpMsgBuf;
FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
WSAGetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL );
OutputDebugString(lpMsgBuf);
LocalFree( lpMsgBuf );
}
// Make a second call to GetIpAddrTable to get the
// actual data we want
if ( (dwRetVal = GetIpNetTable( pIPNetTable, &dwSize, 0 )) == NO_ERROR )
{
for (UINT i=0;i
{
if ((pIPNetTable->table[i].dwAddr) == inet_addr(strIP))
{
BYTE *pt = (BYTE*)&pIPNetTable->table[i].bPhysAddr;
for (int j=0;j<6;j++)
strMAC.AppendFormat(_T("%02X"),*pt++);
break;
}
}
}
//Cleanup
if(pIPNetTable)
{
free( pIPNetTable );
pIPNetTable = NULL;
}
return strMAC;
}
반응형