Click here to Skip to main content
11,582,911 members (73,545 online)
Click here to Skip to main content

Creating a Unique String Using a UUID

, 12 Dec 2001 CPOL 167.3K 17
Rate this:
Please Sign up or sign in to vote.
A small function to either accept or create a UUID* and return a CString
<!-- Add the rest of your HTML here -->

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)

Share

About the Author

John Simmons / outlaw programmer
Software Developer (Senior)
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.

You may also be interested in...

Comments and Discussions

 
Generalthanks for the code Pin
pelegk220-Oct-08 12:42
memberpelegk220-Oct-08 12:42 
GeneralRe: thanks for the code Pin
John Simmons / outlaw programmer20-Oct-08 23:33
mvpJohn Simmons / outlaw programmer20-Oct-08 23:33 
GeneralAllocating UUID on the heap Pin
Stephen Boissiere17-May-07 23:42
memberStephen Boissiere17-May-07 23:42 
GeneralRe: Allocating UUID on the heap Pin
John Simmons / outlaw programmer18-May-07 23:33
mvpJohn Simmons / outlaw programmer18-May-07 23:33 
QuestionWhy so rude? Pin
joelhumphrey3-Feb-06 14:48
memberjoelhumphrey3-Feb-06 14:48 
AnswerRe: Why so rude? Pin
John Simmons / outlaw programmer5-Sep-06 3:00
memberJohn Simmons / outlaw programmer5-Sep-06 3:00 
GeneralCreating a string with \n in C on NT Pin
Dave Moss21-Jun-02 10:13
memberDave Moss21-Jun-02 10:13 
GeneralRe: Creating a string with \n in C on NT Pin
John Simmons / outlaw programmer22-Jun-02 2:13
memberJohn Simmons / outlaw programmer22-Jun-02 2:13 
GeneralMake Sure To Remember... Pin
John Simmons / outlaw programmer18-Dec-01 1:24
memberJohn Simmons / outlaw programmer18-Dec-01 1:24 
GeneralHere's one for those of us not using MFC... Pin
ynohtna17-Dec-01 22:23
memberynohtna17-Dec-01 22:23 
GeneralAnother way... Pin
Norm Almond13-Dec-01 2:04
memberNorm Almond13-Dec-01 2:04 
GeneralRe: Another way... Pin
Fazlul Kabir13-Dec-01 4:36
memberFazlul Kabir13-Dec-01 4:36 
GeneralRe: Another way... Pin
Norm Almond13-Dec-01 4:59
memberNorm Almond13-Dec-01 4:59 
GeneralAnother Way Pin
Daniel Turini12-Dec-01 4:40
memberDaniel Turini12-Dec-01 4:40 
GeneralRe: Another Way Pin
John Simmons / outlaw programmer12-Dec-01 6:26
memberJohn Simmons / outlaw programmer12-Dec-01 6:26 
GeneralRe: Another Way Pin
Brad Bruce12-Dec-01 13:14
memberBrad Bruce12-Dec-01 13:14 
GeneralRe: Another Way Pin
Tim Smith12-Dec-01 14:08
memberTim Smith12-Dec-01 14:08 
GeneralRe: Another Way Pin
Brad Bruce12-Dec-01 14:18
memberBrad Bruce12-Dec-01 14:18 
GeneralRe: Another Way Pin
John Simmons / outlaw programmer12-Dec-01 14:46
memberJohn Simmons / outlaw programmer12-Dec-01 14:46 
GeneralRe: Another Way Pin
Brad Bruce13-Dec-01 2:21
memberBrad Bruce13-Dec-01 2:21 
GeneralRe: Another Way Pin
Dalle13-Dec-01 23:25
memberDalle13-Dec-01 23:25 
GeneralRe: Another Way Pin
John Simmons / outlaw programmer12-Dec-01 14:41
memberJohn Simmons / outlaw programmer12-Dec-01 14:41 
GeneralRe: Another Way Pin
Brad Bruce13-Dec-01 2:30
memberBrad Bruce13-Dec-01 2:30 
GeneralRe: Another Way Pin
Paul A. Howes13-Dec-01 2:41
memberPaul A. Howes13-Dec-01 2:41 
GeneralRe: Another Way Pin
Brad Bruce13-Dec-01 2:53
memberBrad Bruce13-Dec-01 2:53 
GeneralRe: Another Way Pin
Tim Smith13-Dec-01 12:46
memberTim Smith13-Dec-01 12:46 
GeneralRe: Another Way Pin
Daniel Turini14-Dec-01 23:25
memberDaniel Turini14-Dec-01 23:25 
GeneralRe: Another Way Pin
John Simmons / outlaw programmer15-Dec-01 1:27
memberJohn Simmons / outlaw programmer15-Dec-01 1:27 
GeneralRe: Another Way Pin
Daniel Turini15-Dec-01 2:44
memberDaniel Turini15-Dec-01 2:44 
GeneralRe: Another Way [modified] Pin
molafish10-Nov-08 12:13
membermolafish10-Nov-08 12:13 
GeneralRe: Another Way Pin
Norm Almond14-Dec-01 0:00
memberNorm Almond14-Dec-01 0:00 
GeneralRe: Another Way Pin
Matt Gullett12-Dec-01 14:45
memberMatt Gullett12-Dec-01 14:45 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150603.1 | Last Updated 13 Dec 2001
Article Copyright 2001 by John Simmons / outlaw programmer
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid