How can I generate UUIDs in C ++ without using boost library?

I want to generate a UUID for my application in order to distinguish each installation of my application. I want to generate this UUID using C ++ without boost library support. How can I generate a UUID using another open source library?

Note. My platform is windows

+12
c ++ uuid
source share
4 answers

As mentioned in the comments, you can use UuidCreate

#pragma comment(lib, "rpcrt4.lib") // UuidCreate - Minimum supported OS Win 2000 #include <windows.h> #include <iostream> using namespace std; int main() { UUID uuid; UuidCreate(&uuid); char *str; UuidToStringA(&uuid, (RPC_CSTR*)&str); cout<<str<<endl; RpcStringFreeA((RPC_CSTR*)&str); return 0; } 
+11
source share

The ossp-uuid library can generate UUIDs and has C ++ bindings.

Seems extremely easy to use:

 #include <uuid++.hh> #include <iostream> using namespace std; int main() { uuid id; id.make(UUID_MAKE_V1); const char* myId = id.string(); cout << myId << endl; delete myId; } 

Note that it selects and returns a C-style string that the calling code must free to avoid leakage.

Another option is libuuid, which is part of the util-linux package available from ftp://ftp.kernel.org/pub/linux/utils/util-linux/ . Any Linux computer will already install it. It does not have a C ++ API, but it can still be called from C ++ using the C API.

+3
source share

Traditionally, the UUID is simply the MAC address of the machine, combined with the number of 100 nanosecond intervals since the adoption of the Gregorian calendar in the West. So it’s not difficult to write a C ++ class that will do this for you.

+1
source share

If you just want something random, I wrote this little function:

 string get_uuid() { static random_device dev; static mt19937 rng(dev()); uniform_int_distribution<int> dist(0, 15); const char *v = "0123456789abcdef"; const bool dash[] = { 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0 }; string res; for (int i = 0; i < 16; i++) { if (dash[i]) res += "-"; res += v[dist(rng)]; res += v[dist(rng)]; } return res; } 
0
source share

All Articles