C ++ SendARP返回错误的mac地址? [英] C++ SendARP returns wrong mac address?

查看:530
本文介绍了C ++ SendARP返回错误的mac地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图检索我的lan和im使用SendARP功能的设备的mac地址这样做,但由于一些奇怪的原因,它给我错误的mac地址,告诉它获取我的笔记本电脑的mac是也在lan,但它不工作:/

im trying to retrieve the mac address of devices on my lan and im using the SendARP function to do this but for some weird reason it is giving me the wrong mac address, im telling it to get the mac of my laptop that is also on lan but it does not work :/

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

link to SendARP function(MSDN): http://msdn.microsoft.com/en-us/library/windows/desktop/aa366358%28v=vs.85%29.aspx

笔记本电脑的mac确实是:e0:94:67:18:a7:dc
Mac由sendARP输出:e9:ad:2d:01:c8:11

the mac of the laptop is really: e0:94:67:18:a7:dc Mac output by SendARP: e9:ad:2d:01:c8:11

这是我创建的函数,只是从ip地址获取mac:P

this is the function i created to simply fetch the mac from an ip address :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: %d\n", returnValue);

    if(returnValue == ERROR_BAD_NET_NAME) {
        printf("ERROR_BAD_NET_NAME\n trying to fetch mac address...");
        return GetMacAddress(destination, source);
    }

    if(returnValue == ERROR_BUFFER_OVERFLOW) {
        printf("ERROR_BUFFER_OVERFLOW\n");
    }

    if(returnValue == ERROR_GEN_FAILURE) {
        printf("ERROR_GEN_FAILURE\n");
    }

    if(returnValue == ERROR_INVALID_PARAMETER) {
        printf("ERROR_INVALID_PARAMETER\n");
    }

    if(returnValue == ERROR_INVALID_USER_BUFFER) {
        printf("ERROR_INVALID_USER_BUFFER\n");
    }

    if(returnValue == ERROR_NOT_FOUND) {
        printf("ERROR_NOT_FOUND\n");
    }

    if(returnValue == ERROR_NOT_SUPPORTED) {
        printf("ERROR_NOT_SUPPORTED\n");
    }
}
BYTE *bMacAddr = (BYTE *) &DestMacAddr;

return bMacAddr;

}

因为它是网络字节顺序或某事,但nothl()没有工作:/请帮助我在这里:/

i was thinking it might be because it is network byte order or something but nothl() did not work either :/ please help me out here :/

推荐答案

你不能这样做:

BYTE *bMacAddr = (BYTE *) &DestMacAddr;

return bMacAddr;

你返回一个指向GetMacAddress()函数栈的指针将在函数结束时消失。

You're returning a pointer to some thing that's on the stack of your GetMacAddress() function, which will be gone when the function ends.

这篇关于C ++ SendARP返回错误的mac地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆