Home  >  Article  >  Backend Development  >  A brief discussion on two methods of generating GUID in C++

A brief discussion on two methods of generating GUID in C++

little bottle
little bottleforward
2019-04-29 12:22:034548browse

GUID is a commonly used component in software development, used to generate unique objects in languages ​​such as C#, Java and Python, because of their The standard library is relatively rich, so the generation of GUID is relatively simple and can often be done in one or two lines of code. Neither the C language itself nor the standard library provides an API for generating GUIDs. Therefore, C cannot be used to generate GUIDs directly. This article introduces two methods of indirectly creating GUID using C: 1) calling the Win32API-CoCreateGuid function; 2) using the Boost library.

Using the CoCreateGuid function

CoCreateGuid is an API function provided by the Windows system itself and is located in the objbase.h header file. Therefore, we must first include the header file:

include <objbase.h>

The code for calling CoCreateGuid is as follows:

  GUID guid;
  HRESULT h = CoCreateGuid(&guid);
  if (h == S_OK)
   ans = GuidToString(guid);
  else
   throw runtime_error("generate guid failed!");

In the above code, if the GUID is successfully created, the value of h is S_OK, and if it fails, it is other flags. HRESULT is a type of return value of COM components, please refer to HRESULT. The GuidToString function is used to convert the generated Guid to std::string. Related tutorials: C Video Tutorial

std::string GuidToString(const GUID &guid)
{
 char buf[64] = { 0 };
 sprintf_s(buf, sizeof(buf),
  "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
  guid.Data1, guid.Data2, guid.Data3,
  guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3], 
  guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
 return std::string(buf);
}

The advantage of this method of generating GUID based on Win32API is that it does not need to rely on other libraries. The disadvantage is that it cannot be cross-platform and can only be used on the Windows platform.

Using Boost library

It is relatively simple to use Boost library to generate GUID. The code is as follows:

#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>

boost::uuids::uuid uid = boost::uuids::random_generator()();
 const string uid_str = boost::uuids::to_string(uid);
 cout << uid_str << endl;

First load the package, and then use the boost::uuids namespace The related types and functions can create a GUID of std::string type. The method of generating GUID based on BOOST is relatively simple, but the disadvantage is that it requires the introduction of an additional Boost library.

The above is the detailed content of A brief discussion on two methods of generating GUID in C++. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete