Click here to Skip to main content
11,570,750 members (57,821 online)
Click here to Skip to main content

Creating your own GUIDs

, 17 Jul 2001 119.6K 24
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

Share

About the Author

Joseph M. Newcomer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
Generalan implemented solution I use Pin
JeanLuc_6-May-08 4:33
memberJeanLuc_6-May-08 4:33 
GeneralRe: an implemented solution I use Pin
ChizI21-Aug-09 4:47
memberChizI21-Aug-09 4:47 
GeneralUpdated links Pin
Peter Ritchie26-Apr-05 7:47
memberPeter Ritchie26-Apr-05 7:47 
GeneralRe: Updated links Pin
Joseph M. Newcomer1-May-05 17:10
memberJoseph M. Newcomer1-May-05 17:10 
GeneralSolution without rpcrt4.lib possible ! Pin
Danoo17-Dec-02 22:09
memberDanoo17-Dec-02 22:09 
GeneralRe: Solution without rpcrt4.lib possible ! Pin
Rubio25-Mar-03 21:12
memberRubio25-Mar-03 21:12 
GeneralRe: Solution without rpcrt4.lib possible ! Pin
Yogurt18-Apr-03 4:18
memberYogurt18-Apr-03 4:18 
GeneralRe: Solution without rpcrt4.lib possible ! Pin
Rubio5-May-03 23:31
memberRubio5-May-03 23:31 
GeneralThanks Pin
Robert Pittenger30-Mar-02 15:56
memberRobert Pittenger30-Mar-02 15:56 
Even though VC already did this, this article helped me with a different problem, and that is finding a way to convert strings to and from GUIDs. So don't be afraid to post the "trivial" articles. Nothing is trivial when you can't find that one API function you need.
GeneralRe: Thanks Pin
Zdenek Breitenbacher31-Jul-02 21:42
sussZdenek Breitenbacher31-Jul-02 21:42 
GeneralRe: Thanks Pin
Danoo17-Dec-02 4:05
memberDanoo17-Dec-02 4:05 
GeneralRe: Thanks Pin
AliRafiee5-Jan-05 9:01
memberAliRafiee5-Jan-05 9:01 
General#pragma once Pin
Anonymous20-Jul-01 10:45
memberAnonymous20-Jul-01 10:45 
GeneralRe: #pragma once Pin
Michael Dunn20-Jul-01 11:52
memberMichael Dunn20-Jul-01 11:52 
GeneralRe: #pragma once Pin
Joseph M. Newcomer20-Jul-01 16:06
memberJoseph M. Newcomer20-Jul-01 16:06 
QuestionDoesn't VC Already Do This? Pin
Brian Hart19-Jul-01 19:27
memberBrian Hart19-Jul-01 19:27 
AnswerRe: Doesn't VC Already Do This? Pin
Joseph M. Newcomer19-Jul-01 19:46
memberJoseph M. Newcomer19-Jul-01 19:46 
GeneralRe: Doesn't VC Already Do This? Pin
Brian Hart19-Jul-01 19:58
memberBrian Hart19-Jul-01 19:58 
GeneralRe: Doesn't VC Already Do This? Pin
Joseph M. Newcomer19-Jul-01 20:50
memberJoseph M. Newcomer19-Jul-01 20:50 
GeneralRe: Doesn't VC Already Do This? Pin
Andrew Peace20-Jul-01 12:50
memberAndrew Peace20-Jul-01 12:50 
GeneralRe: Doesn't VC Already Do This? Pin
Joseph M. Newcomer20-Jul-01 21:58
memberJoseph M. Newcomer20-Jul-01 21:58 
AnswerGone in VC7 [Re: Doesn't VC Already Do This?] Pin
Fazlul Kabir23-Jul-01 11:15
memberFazlul Kabir23-Jul-01 11:15 
AnswerRe: Doesn't VC Already Do This? Pin
tracyd8230-Jun-09 16:29
membertracyd8230-Jun-09 16:29 
GeneralRe: Doesn't VC Already Do This? Pin
Joseph M. Newcomer30-Jun-09 20:01
memberJoseph 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 | Terms of Use | Mobile
Web03 | 2.8.150624.2 | Last Updated 18 Jul 2001
Article Copyright 2001 by Joseph M. Newcomer
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid