Click here to Skip to main content
Licence 
First Posted 17 Jul 2001
Views 102,197
Bookmarked 24 times

Creating your own GUIDs

By | 17 Jul 2001 | Article
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

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalan implemented solution I use PinmemberJeanLuc_4:33 6 May '08  
GeneralRe: an implemented solution I use PinmemberChizI4:47 21 Aug '09  
GeneralUpdated links PinmemberPeter Ritchie7:47 26 Apr '05  
GeneralRe: Updated links PinmemberJoseph M. Newcomer17:10 1 May '05  
GeneralSolution without rpcrt4.lib possible ! PinmemberDanoo22:09 17 Dec '02  
GeneralRe: Solution without rpcrt4.lib possible ! PinmemberRubio21:12 25 Mar '03  
GeneralRe: Solution without rpcrt4.lib possible ! PinmemberYogurt4:18 18 Apr '03  
GeneralRe: Solution without rpcrt4.lib possible ! PinmemberRubio23:31 5 May '03  
GeneralThanks PinmemberRobert Pittenger15:56 30 Mar '02  
GeneralRe: Thanks PinsussZdenek Breitenbacher21:42 31 Jul '02  
GeneralRe: Thanks PinmemberDanoo4:05 17 Dec '02  
GeneralRe: Thanks PinmemberAliRafiee9:01 5 Jan '05  
General#pragma once PinmemberAnonymous10:45 20 Jul '01  
GeneralRe: #pragma once PinmemberMichael Dunn11:52 20 Jul '01  
GeneralRe: #pragma once PinmemberJoseph M. Newcomer16:06 20 Jul '01  
QuestionDoesn't VC Already Do This? PinmemberBrian Hart19:27 19 Jul '01  
AnswerRe: Doesn't VC Already Do This? PinmemberJoseph M. Newcomer19:46 19 Jul '01  
GeneralRe: Doesn't VC Already Do This? PinmemberBrian Hart19:58 19 Jul '01  
GeneralRe: Doesn't VC Already Do This? PinmemberJoseph M. Newcomer20:50 19 Jul '01  
GeneralRe: Doesn't VC Already Do This? PinmemberAndrew Peace12:50 20 Jul '01  
GeneralRe: Doesn't VC Already Do This? PinmemberJoseph M. Newcomer21:58 20 Jul '01  
AnswerGone in VC7 [Re: Doesn't VC Already Do This?] PinmemberFazlul Kabir11:15 23 Jul '01  
AnswerRe: Doesn't VC Already Do This? Pinmembertracyd8216:29 30 Jun '09  
GeneralRe: Doesn't VC Already Do This? PinmemberJoseph M. Newcomer20:01 30 Jun '09  

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.

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