July 18, 2008

How to generate UUID (GUID) in C++?

Sample Code:

#include <Rpc.h>
#pragma comment(lib, "Rpcrt4.lib")

UUID uuid;
::ZeroMemory(&uuid, sizeof(UUID));

// Create uuid or load from a string by UuidFromString() function
::UuidCreate(&uuid);

// If you want to convert uuid to string, use UuidToString() function
WCHAR* wszUuid = NULL;
::UuidToStringW(&uuid, &wszUuid);
if(wszUuid != NULL)
{
  ::RpcStringFree(&wszUuid);
  wszUuid = NULL;
}


Reference in MSDN: