SendARP返回错误的mac地址

C++ SendARP returns wrong mac address?

本文关键字:mac 地址 错误 返回 SendARP      更新时间:2023-10-16

我试图检索设备的mac地址在我的局域网和我使用SendARP功能来做到这一点,但由于一些奇怪的原因,它给了我错误的mac地址,我告诉它得到我的笔记本电脑的mac也在局域网,但它不工作:/

链接到SendARP功能(MSDN): http://msdn.microsoft.com/en-us/library/windows/desktop/aa366358%28v=vs.85%29.aspx

笔记本电脑的MAC实际上是:e0:94:67:18:a7:dcSendARP: e9:ad:2d:01:c8:11

这是我创建的函数,用于简单地从ip地址获取mac: p

BYTE* GetMacAddress(IPAddr destination, IPAddr source) {
Sleep(500);
ULONG DestMacAddr[2];
ULONG PhysicalLength = 6;
memset(&DestMacAddr, 0xff, sizeof(DestMacAddr));
DWORD returnValue = SendARP(destination, source, &DestMacAddr, &PhysicalLength);
if(returnValue == NO_ERROR) {
    cout << "Fetched destination mac" << endl;
}else {
    printf("Error: %dn", returnValue);
    if(returnValue == ERROR_BAD_NET_NAME) {
        printf("ERROR_BAD_NET_NAMEn trying to fetch mac address...");
        return GetMacAddress(destination, source);
    }
    if(returnValue == ERROR_BUFFER_OVERFLOW) {
        printf("ERROR_BUFFER_OVERFLOWn");
    }
    if(returnValue == ERROR_GEN_FAILURE) {
        printf("ERROR_GEN_FAILUREn");
    }
    if(returnValue == ERROR_INVALID_PARAMETER) {
        printf("ERROR_INVALID_PARAMETERn");
    }
    if(returnValue == ERROR_INVALID_USER_BUFFER) {
        printf("ERROR_INVALID_USER_BUFFERn");
    }
    if(returnValue == ERROR_NOT_FOUND) {
        printf("ERROR_NOT_FOUNDn");
    }
    if(returnValue == ERROR_NOT_SUPPORTED) {
        printf("ERROR_NOT_SUPPORTEDn");
    }
}
BYTE *bMacAddr = (BYTE *) &DestMacAddr;
return bMacAddr;

}

我认为这可能是因为它是网络字节顺序或其他东西,但nothl()也没有工作:/请帮助我在这里:/

你不能这么做:

BYTE *bMacAddr = (BYTE *) &DestMacAddr;
return bMacAddr;

你正在返回一个指针,指向GetMacAddress()函数的堆栈上的某个东西,当函数结束时它将消失。

在命令提示符下输入'arp -a'查看本地arp表中笔记本电脑的MAC地址。如果它是不同的,那么原始然后键入'arp -d'(在Vista和Win7/8上具有管理员权限)清除arp表,并尝试使用yo SendARP应用程序再次检查mac。