Re: unique serial number for class objects
"wanwan" <email@example.com> wrote in message
your method only works in an application running alone in a local
system. Making such a counter is seriously flawed in a network
application, because applications can create class objects with
In Java, I've written a Peer to Peer application. I was able to get a
unique hash ID for any class object that I instantiate. There was a
built in function for me to do it.
I need to find some way to get the same result in MFC.
If what you want is way to generate a unique ID, then this code does that.
But it doesn't have anything to do with a class or object:
// global variable for illustrative purpose
static LPCTSTR GetUniqueId()
*g_szReturnBuf = 0; // return empty string in case Unique ID can't be
if ( UuidCreate (&uuid) == RPC_S_OK )
// this machine has a network card whose MAC address guarantees the
// uuid is globally unique. So we can use it. Convert guid to a string.
unsigned char *pGuidString;
if ( UuidToString (&uuid, &pGuidString) == RPC_S_OK )
StringCbCopy (g_szReturnBuf , sizeof(g_szReturnBuf ), (LPCSTR)
// Return global id as a string, or an empty string if it couldn't be
return g_szReturnBuf ;
This code uses the MAC address of a machine and the current time to generate
a GUID, which it then converts into a string. I guess in your class's
constructor it could call this to give itself a unique id.