void CmfcServerDlg::OnLbnSelchangeListClientaddr()
{
BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH];
GetMACFromIP(Address, "192.168.1.1");
GetMACFromIP(Address, m_ClientIdList.GetSelectedString());
}
void CmfcServerDlg::PrintMACaddress(unsignedchar MACData[])
{
CString strText;
strText.Format("%02X-%02X-%02X-%02X-%02X-%02X\n",MACData[0], MACData[1], MACData[2], MACData[3], MACData[4], MACData[5]);
m_ClientIdList.AddString(strText);
}
//bool CmfcServerDlg::GetMACFromIP(BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH], const std::string &selected_ip_adr)
void CmfcServerDlg:: PrintMACFromIP(const CString &selected_ip_adr)
{
IP_ADAPTER_INFO AdapterInfo[16]; // Allocate information for up to 16 NICs
DWORD dwBufLen = sizeof(AdapterInfo); // Save the memory size of buffer
DWORD dwStatus = GetAdaptersInfo( // Call GetAdapterInfo
AdapterInfo, // [out] buffer to receive data
&dwBufLen); // [in] size of receive data buffer
assert(dwStatus == ERROR_SUCCESS); // Verify return value is valid, no buffer overflow
PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo;// Contains pointer to current adapter info
bool found = false;
do {
m_ClientIdList.AddString(pAdapterInfo->AdapterName);
const IP_ADDR_STRING *addr_str = &pAdapterInfo->IpAddressList; // use the address list instead of CurrentIpAddress (which is often NULL)
while(addr_str != NULL)
{
if(selected_ip_adr == addr_str->IpAddress.String) // This compares the selected ip address with an associated Adapter ip address
{
m_ClientIdList.AddString(addr_str->IpAddress.String);
found = true;
break;
}
}
if(found)
{
// memcpy(Address, pAdapterInfo->Address, MAX_ADAPTER_ADDRESS_LENGTH); // copy the adapters MAC address
PrintMACaddress(pAdapterInfo->Address);
break;
}
else
{
PrintMACaddress(pAdapterInfo->Address);
pAdapterInfo = pAdapterInfo->Next; // Progress through linked list
}
}
while(pAdapterInfo); // Terminate if last adapter
// return found; // This determines whether the provided ip address is associated with a MAC address
}
GetMACFromIP(Address, "192.168.1.1");
what is this line for?
GetMACFromIP(Address, m_ClientIdList.GetSelectedString());
this line causes the error error C2039: 'GetSelectedString' : is not a member of 'CListBox'
is there something else i can use instead of GetSelectedString?
no its single selection.
i don't know if this helps but if i remove if (nIndex != LB_ERR)
then an error message comes up saying 'the parameter is incorrect'
i don't know what that first line is or why its displaying the ip address again but it is getting the MAC address.
but when i run the client on another computer the program crashes
and when i run the server and client on the same computer it displays this when i click the ip address:
so all of a sudden a client and a server is involved?
i don't know what that first line is or why its displaying the ip address again but it is getting the MAC address.
But you should. I told you about the debugging lines that you should remove!
So remove line 31, 38, 51 and you will see the MAC address only
I took the source code from the examples in the msdn doc. I cannot test it and I don't know why something like LB_ERR won't work. But if it's ok know...
but when i run the client on another computer the program crashes
so all of a sudden a client and a server is involved?
yes i said that in my very first post that this is a client server program. i run the server, then the client(s) and the server receives the ip address from the clients
yes sorry i forgot about the debugging lines
what crashes?
the server program crashes, it just freezes and says not responding
while debugging the program i found that it gets stuck in the while loop:
while(addr_str != NULL)
{
if(selected_ip_adr == addr_str->IpAddress.String) // This compares the selected ip address with an associated Adapter ip address
{
m_ClientIdList.AddString(addr_str->IpAddress.String);
found = true;
break;
}
addr_str = addr_str->Next; // Note!
}
So this prevents the server from freezing
is the program not able to find the MAC address?
You will not find the MAC address of one computer (client) on another computer (server)
You will not find the MAC address of one computer (client) on another computer (server)
You need to transmit the MAC address
Not necessarily. There are protocols in place for just this. Look at ARP. The MAC address is transmitted whenever you send a data frame out, it's part of the header. Though, if this program is working over a layer 3 network (ie the packets pass through a router) then you won't know the MAC address.