Getting Machine's MAC Address — Good Solution?

后端 未结 3 1287
再見小時候
再見小時候 2021-01-06 01:13

I\'ve heard it\'s not possible with my current library of winpcap.

Is this really true? I see lots of examples on the net but then comments saying \"This doesn\'t wo

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-06 01:57

    One common method is using bits from a UUID, but this isn't entirely dependable. For example, it'll return a value even on a machine that doesn't have a network adapter.

    Fortunately, there is a way that works dependably on any reasonably recent version of Windows. MSDN says it only goes back to Windows 2000, but if memory serves, it also works on NT 4, starting around SP 5, in case anybody's still using NT 4.

    #include 
    #include 
    #include 
    
    int main() {         
        IP_ADAPTER_INFO *info = NULL, *pos;
        DWORD size = 0;
    
        GetAdaptersInfo(info, &size);
    
        info = (IP_ADAPTER_INFO *)malloc(size);
    
        GetAdaptersInfo(info, &size);
    
        for (pos=info; pos!=NULL; pos=pos->Next) {
            printf("\n%s\n\t", pos->Description);
            printf("%2.2x", pos->Address[0]);
            for (int i=1; iAddressLength; i++)
                printf(":%2.2x", pos->Address[i]);
        }
    
        free(info);
        return 0;
    }
    

    Please forgive the ancient C code...

提交回复
热议问题