Library code snippets
Creating your own GUIDs
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
Related articles
Related discussion
-
job vacancy .Net Programmers - ( C# || C++ ) (5 full-time positions)
by tajulz (0 replies)
-
How to store C++ object pointers in Core foundation's ordered collections like CFMutableArrayRef, CFMutableDictionaryRef.
by elizas (0 replies)
-
Binary Studio | software development outsourcing Ukraine
by Soft Industry (5 replies)
-
searhing numeric data in urdu text file.
by adilsajjad (0 replies)
-
Programming MS Visual C++, David J. Kruglinski - missing files needed
by andwan0 (0 replies)
Why use "CoCreateGuid" function from "OLE32.dll" when you can directly use "UuidCreate" function and only link against "Rpcrt4.dll"?
MSDN says that "CoCreateGuid" just wraps the return value of "UuidCreate" to a "HRESULT" so please avoid the overhead of this indirection.
For performance reasons (when generating multiple GUIDs) and when the GUID generated is only used locally on your machine you may also consider using "UuidCreateSequential" function.
UuidToString performs a memory allocation that has to be freed. This is against the COM heap which is one of the biggest bottle necks in any multi-threaded application.
StringFromGUID2 instead of UUidtoString allows a string to be specified that is on the stack.
This thread is for discussions of Creating your own GUIDs.