2012-10-26 108 views
0

嗨任何人都可以指導我如何獲得網卡採用C在用戶計算機上的地址++?我不能完全肯定,如果這是可能的,作爲一個初學者,我也不太清楚,從哪裏開始獲取網卡地址

感謝

+0

做什麼,你打算做什麼? –

+0

什麼操作系統? (我相信這已經被問了好幾次了。)另外,你說的NIC地址是什麼意思?蘋果電腦? IP? – Mat

+0

你在考慮什麼操作系統? –

回答

0

在Windows中,你可以使用GetAdaptersAddresses功能,檢索phsyical地址。

std::string ConvertPhysicalAddressToString(BYTE* p_Byte, int iSize) 
{ 
    string strRetValue; 

    char cAux[3]; 
    for(int i=0; i<iSize; i++) 
    { 
     sprintf_s(cAux,"%02X", p_Byte[i]); 
     strRetValue.append(cAux); 
     if(i < (iSize - 1)) 
      strRetValue.append("-"); 
    } 

    return strRetValue; 
} 

void GetEthernetDevices(std::vector<std::string> &vPhysicalAddress) 
{  
    // Call the Function with 0 Buffer to know the size of the buffer required 
    unsigned long ulLen = 0; 
    IP_ADAPTER_ADDRESSES* p_adapAddress = NULL; 
    if(GetAdaptersAddresses(AF_INET, 0, NULL, p_adapAddress,&ulLen) == ERROR_BUFFER_OVERFLOW) 
    { 
     p_adapAddress = (PIP_ADAPTER_ADDRESSES)malloc(ulLen); 
     if(p_adapAddress) 
     { 
      DWORD dwRetValue = GetAdaptersAddresses(AF_INET, 0, NULL, p_adapAddress,&ulLen); 
      if(dwRetValue == NO_ERROR) 
      {    
       IP_ADAPTER_ADDRESSES* p_adapAddressAux = p_adapAddress; 
       do 
       { 
        // Only Ethernet 
        if(p_adapAddressAux->IfType == IF_TYPE_ETHERNET_CSMACD)     
         vPhysicalAddress.push_back(ConvertPhysicalAddressToString(p_adapAddress->PhysicalAddress, p_adapAddress->PhysicalAddressLength)); 

        p_adapAddressAux = p_adapAddressAux->Next; 
       } 
       while(p_adapAddressAux != NULL);       
      } 
      free(p_adapAddress); 
     } 
    } 
} 


int _tmain(int argc, _TCHAR* argv[]) 
{ 
    std::vector<std::string> vPhysicalAddress; 
    GetEthernetDevices(vPhysicalAddress); 
} 
+0

超級...謝謝.. :) –