tags:

views:

796

answers:

2

I feel like GUID/UUID questions have been done to death here, but I can't seem to find quite what I'm looking for. I need a function that will generate V4 GUIDs in C, i.e. something that is essentially compatibile with Guid.NewGuid from C#.

This has to work on Windows (XP, Server 2003/8, etc) and 3 different distros of Unix (AIX, Sun, and HP).

I've seen some of the specification white papers, some that even have some sample implementations, but there always seems to be an issue with proper random numbers, or they only generate V1 or V3 UUIDs, etc. The wikipedia page for UUIDs pointed me at a couple of sample libraries, but these are WAY too heavy weight for what I'm trying to accomplish here.

I feel pretty strongly that I could implement something myself, but don't want to waste time reinventing the wheel if there is something really simple and lightweight I could just drop in. Anybody have something or know of something?

Thanks.

+4  A: 

CoCreateGuid is standard for all forms of Windows. Linux standard is libuuid, which is standard on all Linux versions and should be lightweight. I don't know of any library that will work for both Windows and Unix. I think that an #if branch for Windows and Linux is actually appropriate here.

JSBangs
Do you actually need to distribute the same binary on both systems? Or just compile the same source? You question doesn't specify. If you really need to distribute the same binary, then you can probably get the source for libuuid and compile it into your binary, which will make it cross-platform.
JSBangs
@Morinar: This *is* platform independent. You wrap under a common interface some OS specific APIs. In the end, you will need to compile the application under each category of OS, so compile time switch for selecting the right implementation of the same interface should do the job. Some good libraries I know use this "trick". See boost::thread for example.
Cătălin Pitiș
Just compile the same source... an #if branch is perfectly acceptable. Is it a guarantee that my Unix distros will have libuuid? I don't really have the ability to force all our customers to install it or even to properly package it.
Morinar
Morinar: you can always statically link to libuuid.
John Ledbetter
@Morinar: You obviously didn't read JS Bangs' answer very carefully. Sheesh.
Charlie Salts
When I posted my first comment, the entire answer was:CoCreateGuidWhether he added more to it or SO had some sort of issue, I'm not sure of. I of course removed my first comment after his full answer was there and will now apologize for the haste of my initial reply.
Morinar
I edited my original answer, since I realized that I didn't adequately cover the Linux half of the question. This was before I read your comment, but evidently after you posted it. No harm, no foul.
JSBangs
Well, thanks to my other question (http://stackoverflow.com/questions/1376184/determine-if-c-library-is-installed-on-unix), I determined that libuuid is on the Sun systems, but not AIX or HPUX. I was also able to easily implement the Windows solution. Suppose I'm going to have to jump through some linking hoops or something. Still wish there was something easier...
Morinar
While I wouldn't necessarily classify my final solution as simple, I DID get it to work. My #ifndef WIN32 branch was basically the uuid_generate_random() code (and dependencies) lifted directly from the libuuid source. Thanks at least for pointing me in the right direction.
Morinar
A: 

In fact CoCreateGuid() calls UuidCreate(). The generated Data Types(UUID,GUID) are exactly the same. On Windows you can use both functions to create GUIDs

Thomas Maierhofer