Click here to Skip to main content
15,881,882 members
Articles / Desktop Programming / MFC
Article

Creating your own GUIDs

Rate me:
Please Sign up or sign in to vote.
3.22/5 (7 votes)
17 Jul 20011 min read 147.9K   25   24
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:

Image 1

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


Written By
Retired
United States United States
PhD, Computer Science, Carnegie Mellon University, 1975
Certificate in Forensic Science and the Law, Duquesne University, 2008

Co-Author, [i]Win32 Programming[/i]

Comments and Discussions

 
Generalan implemented solution I use Pin
JeanLuc_6-May-08 4:33
JeanLuc_6-May-08 4:33 
GeneralRe: an implemented solution I use Pin
ChizI21-Aug-09 4:47
ChizI21-Aug-09 4:47 
GeneralUpdated links Pin
Peter Ritchie26-Apr-05 7:47
Peter Ritchie26-Apr-05 7:47 
GeneralRe: Updated links Pin
Joseph M. Newcomer1-May-05 17:10
Joseph M. Newcomer1-May-05 17:10 
GeneralSolution without rpcrt4.lib possible ! Pin
Danoo17-Dec-02 22:09
Danoo17-Dec-02 22:09 
GeneralRe: Solution without rpcrt4.lib possible ! Pin
Rubio25-Mar-03 21:12
Rubio25-Mar-03 21:12 
GeneralRe: Solution without rpcrt4.lib possible ! Pin
Yogurt18-Apr-03 4:18
Yogurt18-Apr-03 4:18 
GeneralRe: Solution without rpcrt4.lib possible ! Pin
Rubio5-May-03 23:31
Rubio5-May-03 23:31 
GeneralThanks Pin
Robert Pittenger, MCPD-EAD30-Mar-02 15:56
Robert Pittenger, MCPD-EAD30-Mar-02 15:56 
GeneralRe: Thanks Pin
Zdenek Breitenbacher31-Jul-02 21:42
Zdenek Breitenbacher31-Jul-02 21:42 
Agree with you. This article describes exactly what I need. Thanks again.
GeneralRe: Thanks Pin
Danoo17-Dec-02 4:05
Danoo17-Dec-02 4:05 
GeneralRe: Thanks Pin
Ali Rafiee5-Jan-05 9:01
Ali Rafiee5-Jan-05 9:01 
General#pragma once Pin
20-Jul-01 10:45
suss20-Jul-01 10:45 
GeneralRe: #pragma once Pin
Michael Dunn20-Jul-01 11:52
sitebuilderMichael Dunn20-Jul-01 11:52 
GeneralRe: #pragma once Pin
Joseph M. Newcomer20-Jul-01 16:06
Joseph M. Newcomer20-Jul-01 16:06 
QuestionDoesn't VC Already Do This? Pin
Brian C Hart19-Jul-01 19:27
professionalBrian C Hart19-Jul-01 19:27 
AnswerRe: Doesn't VC Already Do This? Pin
Joseph M. Newcomer19-Jul-01 19:46
Joseph M. Newcomer19-Jul-01 19:46 
GeneralRe: Doesn't VC Already Do This? Pin
Brian C Hart19-Jul-01 19:58
professionalBrian C Hart19-Jul-01 19:58 
GeneralRe: Doesn't VC Already Do This? Pin
Joseph M. Newcomer19-Jul-01 20:50
Joseph M. Newcomer19-Jul-01 20:50 
GeneralRe: Doesn't VC Already Do This? Pin
Andrew Peace20-Jul-01 12:50
Andrew Peace20-Jul-01 12:50 
GeneralRe: Doesn't VC Already Do This? Pin
Joseph M. Newcomer20-Jul-01 21:58
Joseph M. Newcomer20-Jul-01 21:58 
AnswerGone in VC7 [Re: Doesn't VC Already Do This?] Pin
Fazlul Kabir23-Jul-01 11:15
Fazlul Kabir23-Jul-01 11:15 
AnswerRe: Doesn't VC Already Do This? Pin
tracyd8230-Jun-09 16:29
tracyd8230-Jun-09 16:29 
GeneralRe: Doesn't VC Already Do This? Pin
Joseph M. Newcomer30-Jun-09 20:01
Joseph M. Newcomer30-Jun-09 20:01 

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

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