Simple Generation of GUID in C
Asked Answered
L

2

11

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.

Leduc answered 3/9, 2009 at 19:3 Comment(0)
R
12

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.

Rolf answered 3/9, 2009 at 19:4 Comment(8)
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.Isogloss
@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.Cipher
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.Leduc
Morinar: you can always statically link to libuuid.Eidolon
When I posted my first comment, the entire answer was: CoCreateGuid Whether 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.Leduc
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.Isogloss
Well, thanks to my other question (#1376684), 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...Leduc
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.Leduc
B
2

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

Bronco answered 12/9, 2009 at 7:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.