<!-- Add the rest of your HTML here -->
I recently had to find a way to be able to programatically assign [guaranteed] unique file names to some data files. I could have gone with the "today's date and time" route but that didn't seem too interesting, and there was a chance that I would have ended up with some identical file names since the files were created one right after the other (which is the same reason I couldn't just use the
GetTempFileName API function). Instead, I chose to use universally unique identifiers (UUID).
A UUID is guaranteed to be unique on the current machine, no matter how many were created, or how quickly they were created. The only downside is that they're kind of long-winded in string form, but hey, life is full of little trade-offs.
The Resulting Code
Performing this task is fairly easy (definitely not rocket surgery) if you know the names of the API functions you need to implement the code. Since most of you probably don't know (or can't/won't/don't want to immediately recall) the API function names, here's a little function that does it all for you.
If you have a UUID handy, just pass a pointer (to the UUID) to this function. If you don't have a UUID, simply don't pass any parameters to this function, and it will create one just long enough to make a string out of it.
You will have to
#include rpcdce.h into your code and link rpcrt4.lib into your program in order for this to work. If you don't have either of these files, download the latest *release* version of the Platform SDK. I've taken the liberty of showing you the contents of the two files I created to make it a little simpler. If you create a CPP and an H file, and then copy/paste these two sections into the approriate files, you won't have to do anything other than adding the files to your project (and installing the SDK of course), and then doing a build.
Here's the contents of my header file:
CString MakeUuidString(UUID* pUUID=NULL);
And here's the contents of my CPP file. It will automatically link in the necessary files (assuming you have the latest SDK installed and integrated into VC6):
#pragma comment( lib "rpcrt4")
CString MakeUuidString(UUID* pUUID)
CString sUUID = "";
unsigned char* sTemp;
BOOL bAllocated = FALSE;
if (pUUID == NULL)
pUUID = new UUID;
bAllocated = TRUE;
if (pUUID != NULL)
hr = UuidCreateSequential(pUUID);
if (hr == RPC_S_OK)
hr = UuidToString(pUUID, &sTemp);
if (hr == RPC_S_OK)
sUUID = sTemp;
pUUID = NULL;