先获得本机IP地址和掩码,得到地址上下限,然后使用SendARP获得MAC地址。缺点是SendARP有超时,导至扫描速度很慢。
// Scaner.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdlib.h>
#include <WinSock2.h>
#include <IPHlpApi.h>
#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")
#define IT_TIME 3
int _tmain(int argc, _TCHAR* argv[])
{
PMIB_IPADDRTABLE IpTable;
int it = IT_TIME;
DWORD i;
int chose;
ULONG dwSize = 0x1000;
DWORD RetD;
while(it-- > 0){
IpTable = (PMIB_IPADDRTABLE)malloc(dwSize);
if(!IpTable){
printf("内存不足\n");
goto End;
}
//注意得到的是网络字节序,即ip的msb在底地址
RetD = GetIpAddrTable(IpTable, &dwSize, FALSE);
if(RetD == NO_ERROR)
break;
else if(RetD == ERROR_INSUFFICIENT_BUFFER){
free(IpTable);
}else{
free(IpTable);
printf("发生未知错误\n");
goto End;