Click here to Skip to main content
15,879,474 members
Articles / Desktop Programming / MFC
Article

Creating a Unique String Using a UUID

Rate me:
Please Sign up or sign in to vote.
3.65/5 (17 votes)
12 Dec 2001CPOL2 min read 212.3K   20   33
A small function to either accept or create a UUID* and return a CString

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:

// UuidString.h
#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):
//UuidString.cpp
#include "stdafx.h"
#pragma comment( lib "rpcrt4")
#include "rpcdce.h"
#include "UuidString.h"

CString MakeUuidString(UUID* pUUID/*=NULL*/)
{
   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;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Paddedwall Software
United States United States
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.

Comments and Discussions

 
Generalit is neat Pin
Southmountain5-Oct-20 7:35
Southmountain5-Oct-20 7:35 
Generalthanks for the code Pin
pelegk220-Oct-08 12:42
pelegk220-Oct-08 12:42 
GeneralRe: thanks for the code Pin
#realJSOP20-Oct-08 23:33
mve#realJSOP20-Oct-08 23:33 
GeneralAllocating UUID on the heap Pin
Stephen Boissiere17-May-07 23:42
Stephen Boissiere17-May-07 23:42 
GeneralRe: Allocating UUID on the heap Pin
#realJSOP18-May-07 23:33
mve#realJSOP18-May-07 23:33 
QuestionWhy so rude? Pin
joelhumphrey3-Feb-06 14:48
joelhumphrey3-Feb-06 14:48 
AnswerRe: Why so rude? Pin
#realJSOP5-Sep-06 3:00
mve#realJSOP5-Sep-06 3:00 
GeneralCreating a string with \n in C on NT Pin
21-Jun-02 10:13
suss21-Jun-02 10:13 
GeneralRe: Creating a string with \n in C on NT Pin
#realJSOP22-Jun-02 2:13
mve#realJSOP22-Jun-02 2:13 
GeneralMake Sure To Remember... Pin
#realJSOP18-Dec-01 1:24
mve#realJSOP18-Dec-01 1:24 
GeneralHere's one for those of us not using MFC... Pin
ynohtna17-Dec-01 22:23
ynohtna17-Dec-01 22:23 
GeneralAnother way... Pin
NormDroid13-Dec-01 2:04
professionalNormDroid13-Dec-01 2:04 
GeneralRe: Another way... Pin
Fazlul Kabir13-Dec-01 4:36
Fazlul Kabir13-Dec-01 4:36 
GeneralRe: Another way... Pin
NormDroid13-Dec-01 4:59
professionalNormDroid13-Dec-01 4:59 
GeneralAnother Way Pin
Daniel Turini12-Dec-01 4:40
Daniel Turini12-Dec-01 4:40 
GeneralRe: Another Way Pin
#realJSOP12-Dec-01 6:26
mve#realJSOP12-Dec-01 6:26 
GeneralRe: Another Way Pin
Brad Bruce12-Dec-01 13:14
Brad Bruce12-Dec-01 13:14 
GeneralRe: Another Way Pin
Tim Smith12-Dec-01 14:08
Tim Smith12-Dec-01 14:08 
GeneralRe: Another Way Pin
Brad Bruce12-Dec-01 14:18
Brad Bruce12-Dec-01 14:18 
GeneralRe: Another Way Pin
#realJSOP12-Dec-01 14:46
mve#realJSOP12-Dec-01 14:46 
GeneralRe: Another Way Pin
Brad Bruce13-Dec-01 2:21
Brad Bruce13-Dec-01 2:21 
GeneralRe: Another Way Pin
Anders Dalvander13-Dec-01 23:25
Anders Dalvander13-Dec-01 23:25 
GeneralRe: Another Way Pin
#realJSOP12-Dec-01 14:41
mve#realJSOP12-Dec-01 14:41 
GeneralRe: Another Way Pin
Brad Bruce13-Dec-01 2:30
Brad Bruce13-Dec-01 2:30 
GeneralRe: Another Way Pin
Paul A. Howes13-Dec-01 2:41
Paul A. Howes13-Dec-01 2:41 

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.