Click here to Skip to main content
Click here to Skip to main content

Creating your own GUIDs

, 17 Jul 2001
Rate this:
Please Sign up or sign in to vote.
A simple method of creating a unique name or identifier.


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


#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;

 BYTE * str;
 UuidToString((UUID*)&guid, &str);

 CString unique((LPTSTR)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.
#if !defined(DDL_SetExe_449d5a10_7563_11d5_a037_006067718d04)
#define DDL_SetExe_449d5a10_7563_11d5_a037_006067718d04

#if _MSC_VER > 1000
   #pragma once

declarations go here
#endif // DDL_SetExe_449d5a10_7563_11d5_a037_006067718d04

The views expressed in these essays are those of the author, and in no way represent, nor are they endorsed by, Microsoft.

Send mail to with questions or comments about this article.
Copyright © 1999 All Rights Reserved


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Joseph M. Newcomer

United States United States
No Biography provided

Comments and Discussions

Generalan implemented solution I use PinmemberJeanLuc_6-May-08 5:33 
GeneralRe: an implemented solution I use PinmemberChizI21-Aug-09 5:47 
GeneralUpdated links PinmemberPeter Ritchie26-Apr-05 8:47 
GeneralRe: Updated links PinmemberJoseph M. Newcomer1-May-05 18:10 
GeneralSolution without rpcrt4.lib possible ! PinmemberDanoo17-Dec-02 23:09 
GeneralRe: Solution without rpcrt4.lib possible ! PinmemberRubio25-Mar-03 22:12 
Doesn't work. Suppose guid.Data2 is 1796 (in decimal). Your CString format will result 704 when it should be 0704.
-- Rubio
GeneralRe: Solution without rpcrt4.lib possible ! PinmemberYogurt18-Apr-03 5:18 
GeneralRe: Solution without rpcrt4.lib possible ! PinmemberRubio6-May-03 0:31 
GeneralThanks PinmemberRobert Pittenger30-Mar-02 16:56 
GeneralRe: Thanks PinsussZdenek Breitenbacher31-Jul-02 22:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150327.1 | Last Updated 18 Jul 2001
Article Copyright 2001 by Joseph M. Newcomer
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid