how to get MAC address of another computer and display in a listbox?

Pages: 123
those lines do nothing. nothing happens when i click the ip address in the first list box. i don't think the program is reading GetMACFromIP

did i call it correctly?
1
2
3
4
5
void CmfcServerDlg::OnLbnSelchangeListClientaddr()
{
	bool GetMACFromIP(BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH], const std::string &selected_ip_adr);

}
did i call it correctly?
No, it's not a call. It's newly declared.

A call is so:
1
2
3
4
5
6
7
void CmfcServerDlg::OnLbnSelchangeListClientaddr()
{
	BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH];
	GetMACFromIP(Address, "192.168.1.1");
	GetMACFromIP(Address, listbox.GetSelectedString());

}

I suggest to change it to PrintMACFromIP as previously suggested. Address has not longer any use.
And I suggest to change std::string to CString
ok code now looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
void CmfcServerDlg::OnLbnSelchangeListClientaddr()
{
	BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH];
	GetMACFromIP(Address, "192.168.1.1");
	GetMACFromIP(Address, m_ClientIdList.GetSelectedString());
}

void CmfcServerDlg::PrintMACaddress(unsigned char 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?
please help. i don't know what the problem is
what is this line for?
As an example for testing/debugging. If it works you need to keep in mind to remove all this debugging lines.

is there something else i can use instead of GetSelectedString?
Well, GetSelectedString is not a part of CListBox (I think that you use it?)

take a look at this:
http://msdn.microsoft.com/en-US/library/k8cseaky%28v=vs.80%29.aspx

MFC is a beast sometimes. To retrieve the selected string from a listbox you need to write something like this:
1
2
3
4
5
6
7
int nIndex = pmyListBox->GetCurSel();
if (nIndex != LB_ERR)
{
  CString str;
  pmyListBox->GetText(nIndex, str);
  PrintMACFromIP(str);
}

Mind you that I cannot test it!
still nothing is happening when i click the ip address
the list box event handler now looks like this:
1
2
3
4
5
6
7
8
9
10
11
void CmfcServerDlg::OnLbnSelchangeListClientaddr()
{
	BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH];
	int nIndex = m_ClientIdList.GetCurSel();
	if (nIndex != LB_ERR)
	{
		CString str;
		m_ClientIdList.GetText(nIndex, str);
		PrintMACFromIP(str);
	}
}


when i debugged it, the program skipped if (nIndex != LB_ERR) so it didn't get the string
Last edited on
That probably means that you use the wrong event. It seems the selection isn't done at this point.

There might be an event before and one after the selection is done.

Check that out (what happens if you click a second time?)
nothing happens if i click a second time. I even tried the OnLbnDblclk event but that didn't work either
See

http://msdn.microsoft.com/en-US/library/6ty0th77%28v=vs.80%29.aspx

msdn wrote:
It is LB_ERR if no item is currently selected or if the list box is a multiple-selection list box.
is it multiple-selection?
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'
Last edited on
ok i changed the code to this:
1
2
3
4
5
6
7
	int nIndex = m_ClientAddrList.GetCurSel();
    if(nIndex < 0)
        return;
    
    CString s1;
    m_ClientAddrList.GetText(nIndex , s1);
	PrintMACFromIP(s1);


and when i run the server and client on the same computer it displays this when i click the ip address:

{D8341FED-09B9-4820-A-82343B0D5D}
00-25-64-A9-32-E9
10.14.32.131


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
Last edited on
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
what crashes?
Last edited on
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:
1
2
3
4
5
6
7
8
9
while(addr_str != NULL)
{
			
	  if(selected_ip_adr == addr_str->IpAddress.String) ess
	  {	
		   found = true;
		   break;
	  }
}


it jumps out of the while loop when it reaches if(selected_ip_adr == addr_str->IpAddress.String) so found is never set to true
Last edited on
i replaced that code i just posted with this code:
1
2
3
4
5
6
7
8
9
	if (addr_str != NULL)
		{
			if (selected_ip_adr == addr_str->IpAddress.String)
			{
				PrintMACaddress(pAdapterInfo->Address);
			}              
		}   

		pAdapterInfo = pAdapterInfo->Next; 


but the program is not executing PrintMACaddress(pAdapterInfo->Address);
is the program not able to find the MAC address?
Okay, I found the bug:
1
2
3
4
5
6
7
8
9
10
11
		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 need to transmit the MAC address
You will not find the MAC address of one computer (client) on another computer (server)
well that is what i am trying to do

You need to transmit the MAC address
how do i do that?
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.
can i add ARP code to my existing function?
I am using a UDP connection
@ResidentBiscuit
I totally forgot about ARP
So if you receive a TCP/IP packet you're theoretically able to extract the MAC address? How?



how do i do that?
You may use the according ARP function:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa366358%28v=vs.85%29.aspx

Use SendARP with the appropriate ip addresses

Or alternatively: Well, the MAC address is data as anything else. You may transmit it as a string or raw data
Which option is easiest?

do i need to change much in the code?
Pages: 123