跨平台生成GUID/UUID
Posted 朝闻道
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了跨平台生成GUID/UUID相关的知识,希望对你有一定的参考价值。
#ifndef XGUID_H
#define XGUID_H
#include <string>
#include <stdio.h>
#ifdef WIN32
#include <objbase.h>
#else
#include <uuid/uuid.h>
typedef struct _GUID
{
unsigned long Data1;
unsigned short Data2;
unsigned short Data3;
unsigned char Data4[8];
} GUID, UUID;
#endif
namespace XGUID
{
GUID CreateGuid()
{
GUID guid;
#ifdef WIN32
CoCreateGuid(&guid);
#else
uuid_generate(reinterpret_cast<unsigned char *>(&guid));
#endif
return guid;
}
std::string GuidToString(const GUID &guid)
{
char buf[64] = {0};
#ifdef __GNUC__
snprintf(
#else // MSVC
_snprintf_s(
#endif
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);
}
std::string CreateGuidString()
{
return GuidToString(CreateGuid());
}
}//namespace
#endif // XGUID_H
eg:
QString str1(XGUID::CreateGuidString().c_str());
http://blog.csdn.net/itjobtxq/article/details/8224676
以上是关于跨平台生成GUID/UUID的主要内容,如果未能解决你的问题,请参考以下文章
如何将 NEWID() / GUID / UUID 插入代码编辑器?