生成UUID的几种方式

生成UUID的几种方式,第1张

一、boost库
#include 
#include 
#include 

boost::uuids::uuid my_uuid = boost::uuids::random_generator(); 
std::string uuid_to_string = boost::uuids::to_string(a_uuid);
二、windows
  • UuidCreate 创建Uuid
  • UuidToString 将Uuid转换为字符串
  • RpcStringFree 释放Uuid
#include 
#pragma comment(lib,"Rpcrt4.lib")
std::string create_Uuid(){
    std::string ret_uuid;
	UUID uuid;
	RPC_STATUS status = UuidCreate(&uuid);
	if (RPC_S_OK == status || RPC_S_UUID_LOCAL_ONLY == status) {
		RPC_CSTR ch_uuid = nullptr;
		RPC_STATUS status = UuidToStringA(&uuid, &ch_uuid);
		if (RPC_S_OK == status) {
			ret_uuid= (char*)ch_uuid;
			RpcStringFreeA(&ch_uuid);
		}
	}
	return ret_uuid;
}
  • CoCreateGuid
    嵌入式系统API,内部调用UuidCreate。
HRESULT CoCreateGuid(
  GUID* pguid  //Pointer to the requested GUID on return.
);

OS Versions: Windows CE 3.0 and later. CE标识嵌入式系统
Header: Objbase.h.
Link Library: Ole32.lib.

#include 
#pragma comment(lib,"Ole32.lib")
    GUID guid;
    HRESULT result = CoCreateGuid(&guid);
三、Linux
#include 
#define GUID_LEN 64
    char buf[GUID_LEN] = { 0 };
    uuid_t uu;   
    uuid_generate( uu );   
    int32_t index = 0;
    for (int32_t i = 0; i < 16; i++)
    {
        int32_t len = i < 15 ? sprintf(buf + index, "%02X-", uu[i]) : sprintf(buf + index, "%02X", uu[i]);
        if(len < 0 ){
            return std::move(std::string(""));
        }
        index += len;
    }

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1296118.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-10
下一篇 2022-06-10

发表评论

登录后才能评论

评论列表(0条)

保存