// testArp.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <windows.h>
#include <Iphlpapi.h>
#include <Iprtrmib.h>
#include <vector>
int _tmain(int argc, _TCHAR* argv[])
{
ULONG uSize = 0;
PMIB_IPNETTABLE pIpNetTable = nullptr;
DWORD dwRet = GetIpNetTable(pIpNetTable, &uSize, false);
if (dwRet == ERROR_INSUFFICIENT_BUFFER)
{
pIpNetTable = (PMIB_IPNETTABLE)malloc(uSize);
if (NO_ERROR == GetIpNetTable(pIpNetTable, &uSize, false))
{
PMIB_IPNETROW pIpNetRow = pIpNetTable->table;
in_addr inaddr;
for (int k = 0; k < pIpNetTable->dwNumEntries; ++k)
{
char szIp[32] = "",szMac[64] = "";
//IP地址
inaddr.S_un.S_addr = pIpNetRow[k].dwAddr;
strcpy(szIp, inet_ntoa(inaddr));
//过滤Mac为空的
if (pIpNetRow[k].dwPhysAddrLen <= 0) continue;
UCHAR bPhysAddr[MAXLEN_PHYSADDR] = { 0 };
if (0 == memcmp(pIpNetRow[k].bPhysAddr, bPhysAddr, pIpNetRow[k].dwPhysAddrLen)) continue;
//MAC地址
for (int j = 0; j < pIpNetRow[k].dwPhysAddrLen; j++)
{
if (lstrcmp(szMac,"") == 0)
sprintf(szMac, "%02x", pIpNetRow[k].bPhysAddr[j]);
else
sprintf(szMac, "%s-%02x",szMac, pIpNetRow[k].bPhysAddr[j]);
}
char szBuf[1024] = "";
sprintf(szBuf, "%s--------%s\r\n", szIp, szMac);
printf(szBuf);
}
}
}
return 0;
}
通过arp列表就可以获取对应网关mac