Introduction
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:
#ifndef __UUIDSTRING_H
#define __UUIDSTRING_H
CString MakeUuidString(UUID* pUUID=NULL);
#endif
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):
#include "stdafx.h"
#pragma comment( lib "rpcrt4")
#include "rpcdce.h"
#include "UuidString.h"
CString MakeUuidString(UUID* pUUID)
{
CString sUUID = "";
unsigned char* sTemp;
BOOL bAllocated = FALSE;
if (pUUID == NULL)
{
pUUID = new UUID;
bAllocated = TRUE;
}
if (pUUID != NULL)
{
HRESULT hr;
hr = UuidCreateSequential(pUUID);
if (hr == RPC_S_OK)
{
hr = UuidToString(pUUID, &sTemp);
if (hr == RPC_S_OK)
{
sUUID = sTemp;
sUUID.MakeUpper();
RpcStringFree(&sTemp);
}
}
if (bAllocated)
{
delete pUUID;
pUUID = NULL;
}
}
return sUUID;
}
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.
My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.