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.

Introduction

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

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 newcomer@flounder.com with questions or comments about this article.
Copyright © 1999 All Rights Reserved
www.flounder.com/mvp_tips.htm

License

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 4:33 
GeneralRe: an implemented solution I use PinmemberChizI21-Aug-09 4:47 
GeneralUpdated links PinmemberPeter Ritchie26-Apr-05 7:47 
Hi Joseph.
 
Any change up updating this article (and your other CodeProject articles) so that links to your useful essays (et al) are up-to-date?
 
I know where to track them down; but, I'd hate to see someone miss the useful information you've tirelessly made available because get got an error 404.
 
Keep up the good work.
 
PeterRitchie.com
GeneralRe: Updated links PinmemberJoseph M. Newcomer1-May-05 17:10 
GeneralSolution without rpcrt4.lib possible ! PinmemberDanoo17-Dec-02 22:09 
GeneralRe: Solution without rpcrt4.lib possible ! PinmemberRubio25-Mar-03 21:12 
GeneralRe: Solution without rpcrt4.lib possible ! PinmemberYogurt18-Apr-03 4:18 
GeneralRe: Solution without rpcrt4.lib possible ! PinmemberRubio5-May-03 23:31 
GeneralThanks PinmemberRobert Pittenger30-Mar-02 15:56 
GeneralRe: Thanks PinsussZdenek Breitenbacher31-Jul-02 21:42 
GeneralRe: Thanks PinmemberDanoo17-Dec-02 4:05 
GeneralRe: Thanks PinmemberAliRafiee5-Jan-05 9:01 
General#pragma once PinmemberAnonymous20-Jul-01 10:45 
GeneralRe: #pragma once PinmemberMichael Dunn20-Jul-01 11:52 
GeneralRe: #pragma once PinmemberJoseph M. Newcomer20-Jul-01 16:06 
QuestionDoesn't VC Already Do This? PinmemberBrian Hart19-Jul-01 19:27 
AnswerRe: Doesn't VC Already Do This? PinmemberJoseph M. Newcomer19-Jul-01 19:46 
GeneralRe: Doesn't VC Already Do This? PinmemberBrian Hart19-Jul-01 19:58 
GeneralRe: Doesn't VC Already Do This? PinmemberJoseph M. Newcomer19-Jul-01 20:50 
GeneralRe: Doesn't VC Already Do This? PinmemberAndrew Peace20-Jul-01 12:50 
GeneralRe: Doesn't VC Already Do This? PinmemberJoseph M. Newcomer20-Jul-01 21:58 
AnswerGone in VC7 [Re: Doesn't VC Already Do This?] PinmemberFazlul Kabir23-Jul-01 11:15 
AnswerRe: Doesn't VC Already Do This? Pinmembertracyd8230-Jun-09 16:29 
GeneralRe: Doesn't VC Already Do This? PinmemberJoseph M. Newcomer30-Jun-09 20:01 

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 | Mobile
Web01 | 2.8.140709.1 | Last Updated 18 Jul 2001
Article Copyright 2001 by Joseph M. Newcomer
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid