Creating your own GUIDs

GUIDGen is one of my favorite tools. I use it for creating unique message numbers (see my essay on Message Management), unique clipboard format names, and unique identifiers for kernel objects such as Semaphores, Mutexes, and Events.

But I needed to create a unique name for another purpose. This takes little effort, but requires a few tricks to get right, so I'm putting it all down here.

What I needed was a unique ID for a #define that would keep a header file from being included more than once. The classic model for this is

#if !defined(uniqueID)
#define uniqueID
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER

declarations

#endif // uniqueID

Now the problem is to come up with a suitable unique ID, since you would not want two header files to accidentally use the same ID and consequently have the inclusion of one exclude the other.

Here's my GUID generator:

CString GUIDgen()
{
 GUID guid;
 CoCreateGuid(&guid);
 BYTE * str;
 UuidToString((UUID*)&guid, &str);
 
 CString unique((LPTSTR)str);

 RpcStringFree(&str);

 unique.Replace(_T("-"), _T("_"));

 return unique;
}

The only real trick is that you must include the rpcrt4.lib file in the link. Otherwise the RpcStringFree method will produce an undefined symbol at link time. Go to Project | Settings and select All Configurations.Go to the Link tab  Then go to the Object/Library Modules edit box, go to the end, and add rpcrt4.lib, as shown:

The string is formatted with hyphens between the digit sequences, so to get a valid C/C++ identifier, I used the replace method to replace them with underscores.

The resulting output is below:

// Header file setexe.h created from setexe.ddl DDL V.1.0.0.13
#if !defined(DDL_SetExe_449d5a10_7563_11d5_a037_006067718d04)
#define DDL_SetExe_449d5a10_7563_11d5_a037_006067718d04

#if _MSC_VER > 1000
#pragma once
#endif

declarations go here
#endif // DDL_SetExe_449d5a10_7563_11d5_a037_006067718d04

You might also like...

Comments

Joseph M. Newcomer

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“You can stand on the shoulders of giants OR a big enough pile of dwarfs, works either way.”