VS2012编写获取ARP命令
分享如下:
[cpp]
PMIB_IPNETTABLE MyGetIpNetTable(BOOL bOrder);
void MyFreeIpNetTable(PMIB_IPNETTABLE pIpNetTable);
PMIB_IPADDRTABLE MyGetIpAddrTable(BOOL bOrder);
void MyFreeIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable);
BOOL Inte易做图ceIdxToInte易做图ceIp(PMIB_IPADDRTABLE pIpAddrTable,DWORD dwIndex,DWORD& dwIpAddr);
BOOL PrintARPTable();
/*
Inte易做图ceIdxToInte易做图ceIp 根据IP地址表,将接口索引转化为IP地址
pIpAddrTable是IP地址表
dwIndex是接口索引
函数执行成功之后,str将包含接口的IP地址
*/
BOOL Inte易做图ceIdxToInte易做图ceIp(PMIB_IPADDRTABLE pIpAddrTable,DWORD dwIndex,DWORD& dwIpAddr)
{
//遍历IP地址表,查找索引dwIndex对应的IP地址
for (DWORD dwIdx = 0;dwIdx < pIpAddrTable->dwNumEntries;dwIdx++)
{
if (dwIndex == pIpAddrTable->table[dwIdx].dwIndex)
{
// 返回查询结果
dwIpAddr = pIpAddrTable->table[dwIdx].dwAddr;//inet_ntoa(*((in_addr*)&pIpAddrTable->table[dwIdx].dwAddr));
if (dwIpAddr != 0)
{
return TRUE;
}
else
return FALSE;
}
}
return FALSE;
}
//获取IP地址到适配器的映射关系,即ARP表
PMIB_IPNETTABLE MyGetIpNetTable(BOOL bOrder)
{
PMIB_IPNETTABLE pIpNetTable = NULL;
DWORD dwActualSize = 0;
//查询所需缓冲区的大小
if (::GetIpNetTable(pIpNetTable,&dwActualSize,bOrder) == ERROR_INSUFFICIENT_BUFFER)
{
//为MIB_IPNETTABLE结构申请内存
pIpNetTable = (PMIB_IPNETTABLE)::GlobalAlloc(GPTR,dwActualSize);
//获取ARP表
if (::GetIpNetTable(pIpNetTable,&dwActualSize,bOrder) == NO_ERROR)
{
return pIpNetTable;
}
::GlobalFree(pIpNetTable);
}
return NULL;
}
void MyFreeIpNetTable(PMIB_IPNETTABLE pIpNetTable)
{
if (pIpNetTable != NULL)
{
::GlobalFree(pIpNetTable);
}
}
//获取IP地址表
PMIB_IPADDRTABLE MyGetIpAddrTable(BOOL bOrder)
{
PMIB_IPADDRTABLE pIpAddrTable = NULL;
DWORD dwActualSize = 0;
if (::GetIpAddrTable(pIpAddrTable,&dwActualSize,bOrder) == ERROR_INSUFFICIENT_BUFFER)
{
pIpAddrTable = (PMIB_IPADDRTABLE)::GlobalAlloc(GPTR,dwActualSize);
if (::GetIpAddrTable(pIpAddrTable,&dwActualSize,bOrder) == NO_ERROR)
{
return pIpAddrTable;
}
::GlobalFree(pIpAddrTable);
}
return NULL;
}
void MyFreeIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable)
{
if (pIpAddrTable != NULL)
{
::GlobalFree(pIpAddrTable);
}
}
BOOL PrintARPTable()
{
/*
ARP表格打印
*/
DWORD i=0,dwCurrIndex=1;
char szType[128] = {""};
DWORD dwIpAddr = 0;
//首先获取ARP表
PMIB_IPNETTABLE pIpNetTable = MyGetIpNetTable(TRUE);
if (pIpNetTable == NULL)
{
printf("pIpNetTable == NULL in line %d\n",__LINE__);
return FALSE;
}
//获取IP地址表,以便根据它将ARP表项中的接口索引转化为IP地址
PMIB_IPADDRTABLE pIpAddrTable = MyGetIpAddrTable(TRUE);
//当前的适配器索引。注意,ARP表应该按照接口索引排序
if (Inte易做图ceIdxToInte易做图ceIp(pIpAddrTable,dwCurrIndex,dwIpAddr))
{
printf("\nInte易做图ce: %s on Inte易做图ce 0x%X\n",inet_ntoa(*((in_addr*)&dwIpAddr)),dwCurrIndex);
printf(" Internet Address\tPhysical Address\tType\n");
}
else
{
printf("Error: Could not convert Inte易做图ce number 0x%X to IP address.\n",
pIpNetTable->table[0].dwIndex);
return FALSE;
}
//打印出索引为dwCurrIndex的适配器上的ARP表项
for (i=0; i < pIpNetTable->dwNumEntries; ++i)
{
//不相等则说明要打印下一个适配器上的ARP表项
if (pIpNetTable->table[i].dwIndex != dwCurrIndex)
&nbs
补充:软件开发 , Vc ,