Get MAC address using C++
There is a Simple way to capture the MAC address of the machine in Win32 environment. In Win32, UUID (version 1) uses MAC address & timestamp as the seeds for generating the UUID. In the UUID string, the last 6 bytes are the MAC address of the machine. UuidCreateSequential() is the Win32 API which is used to capture the MAC address via the UUID generated by it.
#include <rpc.h> // for UUID, UuidCreateSequential
#include <cstring> // for memcpy
#include <algorithm> // for std::swap
unsigned __int64
MACAddress( void ) const
{
UUID u;
::UuidCreateSequential( &u );
u.Data4[0] = u.Data4[1] = 0;
std::swap( u.Data4[0], u.Data4[7] );
std::swap( u.Data4[1], u.Data4[6] );
std::swap( u.Data4[2], u.Data4[5] );
std::swap( u.Data4[3], u.Data4[4] );
unsigned __int64 code;
::memcpy( &code, &u.Data4, sizeof(u.Data4) );
return code;
} // MACAddress
Let me share another hack. If you want to generate strings which do not repeat themselves, you could use UuidCreate() Win32 API, which generates unique UUIDs everytime the function is called. The UUID thus obtained can be converted to a number or a string depending upon the need, which guarantees non-repeatable ids.