How to be unique - the GUID

Started by sukishan, Aug 13, 2009, 01:03 PM

Previous topic - Next topic


There are really only two definitive ways to ensure that a name is unique:

you register the names with some quasi-governmental organization.
you use a special algorithm that generates unique numbers that are guaranteed to be unique world-wide (no small task).
The first approach is how domain names are managed on the network. This approach has the problem that you must pay $50 to register a new name and it takes several weeks for registration to take effect.

The second approach is far cleaner for developers. If you can invent an algorithm that is guaranteed to create a unique name each time anyone on the planet calls it, the problem is solved. Actually, this problem was originally addressed by the Open Software Foundation (OSF). OSF came up with an algorithm that combines a network address, the time (in 100 nanosecond increments), and a counter. The result is a 128-bit number that is unique.

The number 2128 power is an extremely large number. You could identify each nanosecond since the beginning of the universe - and still have 39 bits left over. OSF called this the UUID, for Universally Unique Identifier. Microsoft uses this same algorithm for the COM naming standard. In COM Microsoft decided to re-christen it as a Globally Unique Identifier.

The convention for writing GUID's is in hexadecimal. Case isn't important. A typical GUID looks like this:


Since there is no standard 128-bit data type in C++, we use a structure. Although the GUID structure consists of four different fields, you'll probably never need to manipulate its members. The structure is always used in its entirety.

typedef struct _GUID
    unsigned long Data1;
    unsigned short Data2;
    unsigned short Data3;
    unsigned char Data4[8];

The common pronunciation of GUID is "gwid", so it sounds like 'squid'. Some people prefer the more awkward pronunciation of "goo-wid" (sounds like druid).

GUIDs are generated by a program called GUIDGEN. In GUIDGEN you push a button to generate a new GUID. You are guaranteed that each GUID you generate will be unique, no matter how many GUIDs you generate, and how many people on the planet generate them. This can work because of the following assumption: all machines on the Internet have, by definition, a unique address. Therefore, your machine must be on the network in order for GUIDGEN to work to it's full potential. Actually, if you don't have a network address GUIDGEN will fake one, but you reduce the probability of uniqueness.

Both COM objects and COM interfaces have GUIDs to identify them. So the name "Beeper" that we choose for our object would actually be irrelevant. The object is named by its GUID. We would call the object's GUID the class ID for the object. We could then use a #define or a const to relate the name Beeper to the GUID so that we don't have 128-bit values floating throughout the code. In the same way the interface would have a GUID. Note that many different COM objects created by many different programmers might support the same IBeep interface, and they would all use the same GUID to name it. If it is not the same GUID, then as far as COM is concerned it is a different interface. The GUID is the name.
A good beginning makes a good ending

Quick Reply

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it has been approved by a moderator.

Please leave this box empty:
Type the letters shown in the picture
Listen to the letters / Request another image

Type the letters shown in the picture:

Shortcuts: ALT+S post or ALT+P preview