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.
Basically, a a GUID is generated using a combination of: The MAC address of the machine used to generate the GUID (so GUIDs generated on different machines are unique unless MAC addresses are re-used) Timestamp (so GUIDs generated at different times on the same machine are unique)
A GUID is a 128-bit value consisting of one group of 8 hexadecimal digits, followed by three groups of 4 hexadecimal digits each, followed by one group of 12 hexadecimal digits. The following example GUID shows the groupings of hexadecimal digits in a GUID: 6B29FC40-CA47-1067-B31D-00DD010662DA.
Not guaranteed, since there are several ways of generating one. However, you can try to calculate the chance of creating two GUIDs that are identical and you get the idea: a GUID has 128 bits, hence, there are 2128 distinct GUIDs – much more than there are stars in the known universe.
Insert a new GUID by invoking the command under the Edit top-level menu or hit CTRL+K,Space . If Visual Studio is unable to insert the GUID, it will be copied to the clipboard so you can easily paste it in manually. When a GUID is copied to the clipboard, a notification will be displayed in the status bar.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With