Guid.NewGuid() 的 C++ 版本是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1327157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the C++ version of Guid.NewGuid()?
提问by Simon
I need to create a GUID
in an unmanaged windows C++ project. I'm used to C#, where I'd use Guid.NewGuid()
. What's the (unmanaged windows) C++ version?
我需要GUID
在非托管 Windows C++ 项目中创建一个。我习惯了 C#,在那里我会使用Guid.NewGuid()
. 什么是(非托管窗口)C++ 版本?
采纳答案by Alan
I think CoCreateGuid
is what you're after. Example:
我认为CoCreateGuid
这就是你所追求的。例子:
GUID gidReference;
HRESULT hCreateGuid = CoCreateGuid( &gidReference );
回答by sharptooth
UuidCreate()in Win32 API has exactly the same effect. However you need to pass an address of the variable that will receive the generated value:
Win32 API 中的UuidCreate()具有完全相同的效果。但是,您需要传递将接收生成值的变量的地址:
UUID newId;
UuidCreate( &newId );
I believe Guid.NewGuid() simply maps onto it inside the .NET runtime.
我相信 Guid.NewGuid() 只是在 .NET 运行时内映射到它。
回答by Wyatt O'Day
Here's a snippet of code to get the resulting string value of the generated GUID:
下面是一段代码,用于获取生成的 GUID 的结果字符串值:
// 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 reference:
API参考:
回答by IInspectable
The documentation for Guid.NewGuidpoints out, how it is implemented:
Guid.NewGuid的文档指出了它是如何实现的:
This is a convenient staticmethod that you can call to get a new Guid. The method wraps a call to the Windows CoCreateGuidfunction.
这是一个方便的静态方法,您可以调用它来获取新的Guid。该方法包装对 Windows CoCreateGuid函数的调用。
So the native equivalent to Guid.NewGuid()
is CoCreateGuide()
.
所以本机相当于Guid.NewGuid()
是CoCreateGuide()
。
CoCreateGuidCoCreateGuid调用UuidCreateUuidCreate,生成一个 GUID。不过,这两个 API 调用略有不同:虽然
UuidCreate
UuidCreate
返回一个 UUID,它保证对创建它的计算机是唯一的,但CoCreateGuid
CoCreateGuid
会生成一个绝对唯一的 GUID。If you need to decide, which API to use, here are the relevant sections from the documentation.
如果您需要决定使用哪个 API,这里是文档中的相关部分。
For security reasons, it is often desirable to keep ethernet addresses on networks from becoming available outside a company or organization. The UuidCreatefunction generates a UUIDthat cannot be traced to the ethernet address of the computer on which it was generated. It also cannot be associated with other UUIDscreated on the same computer.
出于安全原因,通常需要防止网络上的以太网地址在公司或组织之外可用。所述UuidCreate函数生成一个UUID无法追踪到在其上生成的计算机的以太网地址。它也不能与在同一台计算机上创建的其他UUID相关联。
The CoCreateGuidfunction calls the RPC function UuidCreate, which creates a GUID, a globally unique 128-bit integer. Use CoCreateGuidwhen you need an absolutely unique number that you will use as a persistent identifier in a distributed environment.
的CoCreateGuid函数调用RPC功能UuidCreate,它创建了一个GUID,全局唯一的128位的整数。当您需要一个绝对唯一的数字作为分布式环境中的持久标识符时,请使用CoCreateGuid。
回答by tcb
To generate a new guid in windows and get the resulting value as a string.
在 Windows 中生成新的 guid 并将结果值作为字符串获取。
#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;
}
Alternatively, you could use sprintf_s
for the string conversion
或者,您可以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);