我需要在一个非托管的Windows C++项目中创建一个GUID
。在C#中,我习惯使用Guid.NewGuid()
。那么(非托管Windows)C++版是什么?
我认为CoCreateGuid
是你需要的。例如:
GUID gidReference;
HRESULT hCreateGuid = CoCreateGuid( &gidReference );
CoCreateGuid
确实调用了 UuidCreate
,但它提供了关于唯一性的更强保证。CoCreateGuid:当你需要一个在分布式环境中作为持久标识符使用的绝对唯一号码时,请使用 CoCreateGuid。 - IInspectableCoCreateGuid
比UuidCreate
做得更多。 - zahirWin32 API中的UuidCreate()具有完全相同的效果。但是您需要传递一个变量地址以接收生成的值:
UUID newId;
UuidCreate( &newId );
我认为Guid.NewGuid()只是在.NET运行时内部映射到它。
// For UUID
#include <Rpc.h>
#pragma comment(lib, "Rpcrt4.lib")
int _tmain(int argc, _TCHAR* argv[])
{
// Create a new uuid
UUID uuid;
RPC_STATUS ret_val = ::UuidCreate(&uuid);
if (ret_val == RPC_S_OK)
{
// convert UUID to LPWSTR
WCHAR* wszUuid = NULL;
::UuidToStringW(&uuid, (RPC_WSTR*)&wszUuid);
if (wszUuid != NULL)
{
//TODO: do something with wszUuid
// free up the allocated string
::RpcStringFreeW((RPC_WSTR*)&wszUuid);
wszUuid = NULL;
}
else
{
//TODO: uh oh, couldn't convert the GUID to string (a result of not enough free memory)
}
}
else
{
//TODO: uh oh, couldn't create the GUID, handle this however you need to
}
return 0;
}
API 参考:
Guid.NewGuid 的文档指出了其实现方式:
这是一个方便的 静态 方法,您可以调用它来获取一个新的GUID。该方法将调用 Windows CoCreateGuid 函数进行封装。
因此,与 Guid.NewGuid()
等价的本地方法是 CoCreateGuid()
。
CoCreateGuid 调用 UuidCreate 生成一个GUID。虽然这两个API调用略有不同: UuidCreate
返回一个UUID,并保证该值在创建它的计算机上是唯一的,而 CoCreateGuid
产生的GUID则绝对是独一无二的。
如果您需要决定使用哪个API,请参阅文档中的相关部分。
出于安全考虑,通常希望将网络上的以太网地址保持在公司或组织内部不可用。 UuidCreate 函数生成一个UUID,该 UUID 无法被追溯到生成它的计算机的以太网地址。 它也不能与在同一台计算机上创建的其他 UUID 相关联。
CoCreateGuid函数产生一个新的GUID。
CoCreateGuid函数调用RPC函数UuidCreate来创建GUID,即全局唯一的128位整数。在分布式环境中需要使用绝对唯一的数字作为持久标识符时,请使用CoCreateGuid。
#include <string>
#include <sstream>
#include <iostream>
#include <windows.h>
#include <iomanip>
int main()
{
GUID guid;
CoCreateGuid(&guid);
std::ostringstream os;
os << std::hex << std::setw(8) << std::setfill('0') << guid.Data1;
os << '-';
os << std::hex << std::setw(4) << std::setfill('0') << guid.Data2;
os << '-';
os << std::hex << std::setw(4) << std::setfill('0') << guid.Data3;
os << '-';
os << std::hex << std::setw(2) << std::setfill('0') << static_cast<short>(guid.Data4[0]);
os << std::hex << std::setw(2) << std::setfill('0') << static_cast<short>(guid.Data4[1]);
os << '-';
os << std::hex << std::setw(2) << std::setfill('0') << static_cast<short>(guid.Data4[2]);
os << std::hex << std::setw(2) << std::setfill('0') << static_cast<short>(guid.Data4[3]);
os << std::hex << std::setw(2) << std::setfill('0') << static_cast<short>(guid.Data4[4]);
os << std::hex << std::setw(2) << std::setfill('0') << static_cast<short>(guid.Data4[5]);
os << std::hex << std::setw(2) << std::setfill('0') << static_cast<short>(guid.Data4[6]);
os << std::hex << std::setw(2) << std::setfill('0') << static_cast<short>(guid.Data4[7]);
std::string s(os.str());
std::cout << s << std::endl;
}
或者,您可以使用 sprintf_s
进行字符串转换。
GUID guid;
CoCreateGuid(&guid);
char guidStr[37];
sprintf_s(
guidStr,
"%08lX-%04hX-%04hX-%02hhX%02hhX-%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX",
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]);
std::string s(guidStr);
#include <Windows.h>
#include <array>
std::string getGUID()
{
std::string result{};
GUID guid;
if (S_OK == CoCreateGuid(&guid))
{
std::array<char, 36> buffer{}; //32 characters of guid + 4 '-' in-between
snprintf(buffer.data(), buffer.size(), "{%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]);
result = std::string(buffer.data());
}
return result;
}
winrt::Windows::Foundation::GuidHelper::CreateNewGuid()
,它返回一个winrt::guid
类型的结构体。您稍后可以将其传递给winrt::to_hstring
以获取winrt::hstring
类型的结构体,然后将其传递给winrt::to_string
以获取std::string
类型。