Click here to Skip to main content
Licence CPOL
First Posted 11 Dec 2001
Views 143,777
Bookmarked 16 times

Creating a Unique String Using a UUID

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)

About the Author

John Simmons / outlaw programmer

Software Developer (Senior)

United States United States

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalthanks for the code Pinmemberpelegk212:42 20 Oct '08  
GeneralRe: thanks for the code PinmvpJohn Simmons / outlaw programmer23:33 20 Oct '08  
GeneralAllocating UUID on the heap PinmemberStephen Boissiere23:42 17 May '07  
GeneralRe: Allocating UUID on the heap PinmvpJohn Simmons / outlaw programmer23:33 18 May '07  
QuestionWhy so rude? PinPopularmemberjoelhumphrey14:48 3 Feb '06  
AnswerRe: Why so rude? PinmemberJohn Simmons / outlaw programmer3:00 5 Sep '06  
GeneralCreating a string with \n in C on NT PinmemberDave Moss10:13 21 Jun '02  
GeneralRe: Creating a string with \n in C on NT PinmemberJohn Simmons / outlaw programmer2:13 22 Jun '02  
GeneralMake Sure To Remember... PinmemberJohn Simmons / outlaw programmer1:24 18 Dec '01  
GeneralHere's one for those of us not using MFC... Pinmemberynohtna22:23 17 Dec '01  
GeneralAnother way... PinmemberNorm Almond2:04 13 Dec '01  
GeneralRe: Another way... PinmemberFazlul Kabir4:36 13 Dec '01  
GeneralRe: Another way... PinmemberNorm Almond4:59 13 Dec '01  
GeneralAnother Way PinmemberDaniel Turini4:40 12 Dec '01  
GeneralRe: Another Way PinmemberJohn Simmons / outlaw programmer6:26 12 Dec '01  
GeneralRe: Another Way PinmemberBrad Bruce13:14 12 Dec '01  
GeneralRe: Another Way PinmemberTim Smith14:08 12 Dec '01  
GeneralRe: Another Way PinmemberBrad Bruce14:18 12 Dec '01  
GeneralRe: Another Way PinmemberJohn Simmons / outlaw programmer14:46 12 Dec '01  
GeneralRe: Another Way PinmemberBrad Bruce2:21 13 Dec '01  
GeneralRe: Another Way PinmemberDalle23:25 13 Dec '01  
GeneralRe: Another Way PinmemberJohn Simmons / outlaw programmer14:41 12 Dec '01  
GeneralRe: Another Way PinmemberBrad Bruce2:30 13 Dec '01  
GeneralRe: Another Way PinmemberPaul A. Howes2:41 13 Dec '01  
GeneralRe: Another Way PinmemberBrad Bruce2:53 13 Dec '01  

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.

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