+ Reply to Thread
Results 1 to 2 of 2

Thread: How to get ethernet addresss of the machine?

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    48

    How to get ethernet addresss of the machine?

    I want to display list of available Ethernet or NIC addresses on the specified machine.

    How to get the list of Ethernet or NIC card addresses using WIN32 API?

    Please provide some idea.

  2. #2
    Use GetAdaptersInfo API to get list of Ethernet card address.

    Below is an example:

    Code:
    void GetMACAddressList(CArray<CString, CString>& strHiddenFiles)
    {
        PIP_ADAPTER_INFO pAdapterInfo ;
        DWORD dwBytesLength = sizeof(IP_ADAPTER_INFO) ;
    	pAdapterInfo = (IP_ADAPTER_INFO *) malloc(sizeof(IP_ADAPTER_INFO)) ;
    	if (NULL != pAdapterInfo)
    	{
    		if (GetAdaptersInfo(pAdapterInfo, &dwBytesLength) == ERROR_BUFFER_OVERFLOW)
    		{
    			free(pAdapterInfo) ;
    			pAdapterInfo = (IP_ADAPTER_INFO *) malloc(dwBytesLength) ;
    		}
    	}
    
    	if (pAdapterInfo != NULL)
    	{
    		DWORD dwStatus = GetAdaptersInfo(pAdapterInfo, &dwBytesLength) ;
    		if(NO_ERROR == dwStatus)
    		{
    			PIP_ADAPTER_INFO pCurrentAdapter = pAdapterInfo ;
    			char szMACAddess[64] ;
    			while(pCurrentAdapter)
    			{
    				if(pCurrentAdapter->Type == MIB_IF_TYPE_ETHERNET)
    				{
    					sprintf(szMACAddess, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x"
    							, pCurrentAdapter->Address[0]
    							, pCurrentAdapter->Address[1]
    							, pCurrentAdapter->Address[2]
    							, pCurrentAdapter->Address[3]
    							, pCurrentAdapter->Address[4]
    							, pCurrentAdapter->Address[5]
    							) ;
    					strHiddenFiles.Add(CString(szMACAddess)) ;
    				}
    
    				pCurrentAdapter = pCurrentAdapter->Next ;
    			}
    		}
            
    		free(pAdapterInfo) ;
    		pAdapterInfo = NULL ;
    	}
    }

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts