Reputation: 199
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 :/
link to SendARP function(MSDN): http://msdn.microsoft.com/en-us/library/windows/desktop/aa366358%28v=vs.85%29.aspx
the mac of the laptop is really: e0:94:67:18:a7:dc Mac output by SendARP: e9:ad:2d:01:c8:11
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;
}
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 :/
Upvotes: 1
Views: 2962
Reputation: 229108
You can't do this:
BYTE *bMacAddr = (BYTE *) &DestMacAddr;
return bMacAddr;
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.
Upvotes: 1
Reputation: 117
Type 'arp -a' on command prompt to see what is MAC address of your laptop on your local arp table. If it is different then the original then type 'arp -d'(With Admin Rights On Vista and Win7/8) to clear arp table and try to check mac again by using yo SendARP App.
Upvotes: 0