Netzwerk - Wie kann man die MAC-Adresse auslesen?



  • Hallo,

    vielleicht passt die Frage hier nicht so ganz her, aber trotzdem:

    Gibt es in MFC oder sonst irgendwie (z.b. in der winsock.lib) die Möglichkeit die MAC-Adresse auszulesen??? Ich hab im Netz dazu folgendes gefunden, aber das generiert mir offensichtlich eine zufällige MAC-Adresse:

    #include "stdafx.h"
    #include <rpc.h>
    #include <iostream>
    using namespace std;
    
    int main(int argc, char* argv[])
    {
    	cout << "MAC address is: ";
    
        // Ask RPC to create a UUID for us.  If this machine has an Ethernet
        // adapter, the last six bytes of the UUID (bytes 2-7 inclusive in
        // the Data4 element) should be the MAC address of the local
        // Ethernet adapter.
        UUID uuid;
        UuidCreate(&uuid);
    
        // Spit the address out
        for (int i = 2; i < 8; ++i) {
            cout << hex;
            cout.fill('0');
            cout.width(2);
            cout << int(uuid.Data4[i]);
            if (i < 7) {
                cout << ":";
            }
        }
        cout << endl;
    	return 0;
    }
    

    *****************************************
    Weiß jemand, wie man die MAC-Adresse auslesen kann bzw. gibts es vielleicht sowas, wie GetMACAdress() in der Winsock.lib???? Besten Dank



  • Siehe: How To Get the MAC Address for an Ethernet Adapter
    http://support.microsoft.com/kb/118623/en-us

    oder, aus meiner Sicht besser:

    CString GetMACAddress(int adapternumber) 
    { 
     int nAdapterCount = 0; 
     ULONG ip; 
     ULONG buflen; 
     PIP_ADAPTER_INFO pAdInfo = NULL; 
     PIP_ADAPTER_INFO pAdInfo_c = NULL; 
    
     buflen = 0; 
     GetAdaptersInfo(pAdInfo, &buflen); //since buflen=0, buffer is 
        // too small. function returns required buffersize in buflen. 
     pAdInfo = (struct _IP_ADAPTER_INFO *)new UCHAR[buflen+1]; 
     pAdInfo_c = pAdInfo; 
     if (GetAdaptersInfo(pAdInfo, &buflen) == ERROR_SUCCESS) 
     { 
      do 
      { 
       ip = inet_addr(pAdInfo->IpAddressList.IpAddress.String); 
       if ((ip != 0)&&(ip != 0x7f000001)) 
       { 
        nAdapterCount++; 
        if ((nAdapterCount == adapternumber)||(adapternumber == 0)) 
        { 
         if (pAdInfo->AddressLength != 0) 
         { 
          CString macstr; 
          for (int i = 0; i < (int)pAdInfo->AddressLength; i++) 
          { 
           CString temp; 
           temp.Format(_T(" %02X"), pAdInfo->Address[i]); 
           macstr += temp; 
          } 
          delete pAdInfo; 
          return macstr; 
         } 
        } 
       } 
      } while ((pAdInfo->Next != NULL)&&((pAdInfo = pAdInfo->Next) != 
    pAdInfo)); 
     } 
     delete pAdInfo_c; 
     return _T(""); 
    
    }
    


  • Damit der code von Jochen läuft, muss man noch einige Includes ergänzen:

    #include "Winsock2.h"
    #include "Iptypes.h"
    #include "Iphlpapi.h"
    

    Und die Iphlpapi.lib einbinden. 🙂


Anmelden zum Antworten