#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

    How to get the MAC address of another computer(client)?


    I have a client server program using UPD created in MFC. In the server there are two listboxes. The first one displays the ip addresses of the connected clients.
    I want the second listbox to display the MAC address of the client when i click the ip address from the listbox.

    So how can I get the MAC address of the client by clicking the ip address in the first listbox?

    so i used this code to get the MAC address:
    Code:
    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
    	do {
    		PrintMACaddress(pAdapterInfo->Address);	// Print MAC address
    		pAdapterInfo = pAdapterInfo->Next;		// Progress through linked list
    	}
    	while(pAdapterInfo);						// Terminate if last adapter
    }
    the code above just provides the currents computer's mac address so it doesn't matter which ip address i click in the listbox because it only displays the same mac address
    can someone please provide some code to do this
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    185
    Rep Power
    82
    NetWKstatransportEnum NetBios function is one way to get the MAC of remote computers.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0
    my current code now looks like this:
    Code:
    void CmfcServerDlg::OnLbnSelchangeListClientaddr()
    {
    	bool GetMACFromIP(BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH], const std::string &selected_ip_adr);
    }
    
    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)
    {
    	IP_ADAPTER_INFO AdapterInfo[16];			
    	DWORD dwBufLen = sizeof(AdapterInfo);		
    
    	DWORD dwStatus = GetAdaptersInfo(			
    		AdapterInfo,							// [out] buffer to receive data
    		&dwBufLen);								// [in] size of receive data buffer
    	assert(dwStatus == ERROR_SUCCESS);			
    
    	PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo;
    	bool found = false;
    	do {
    		m_ClientIdList.AddString(pAdapterInfo->AdapterName);
    		const IP_ADDR_STRING *addr_str = &pAdapterInfo->IpAddressList; 
    		while(addr_str != NULL)
    		{
    			
    		  if(selected_ip_adr == addr_str->IpAddress.String) 
    		  {	
    			m_ClientIdList.AddString(addr_str->IpAddress.String);
    		    found = true;
    		    break;
    		  }
    		}
    		if(found)
    		{
    		  memcpy(Address, pAdapterInfo->Address, MAX_ADAPTER_ADDRESS_LENGTH); 
    		  PrintMACaddress(pAdapterInfo->Address);
    		  break;
    		}
    		else
    		{
    			PrintMACaddress(pAdapterInfo->Address);
    			pAdapterInfo = pAdapterInfo->Next;		
    		}
    	}
    	while(pAdapterInfo);						
    	return found; 
    }
    but for some reason it skips the line of code that calls the GetMACFromIP function in the list box event handler

IMN logo majestic logo threadwatch logo seochat tools logo