关于Windows:Guid.NewGuid()的C ++版本是什么?

What's the C++ version of Guid.NewGuid()?

我需要在非托管Windows C ++项目中创建GUID。 我已经习惯了C#,在这里我会使用Guid.NewGuid()。 什么是(非托管Windows)C ++版本?


Win32 API中的UuidCreate()具有完全相同的效果。但是,您需要传递将接收生成值的变量的地址:

1
2
UUID newId;
UuidCreate( &newId );

我相信Guid.NewGuid()只是在.NET运行时内部映射到它。


我认为CoCreateGuid是您所追求的。例:

1
2
GUID gidReference;
HRESULT hCreateGuid = CoCreateGuid( &gidReference );


这是一段代码,用于获取生成的GUID的结果字符串值:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// 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参考:

  • Uuid创建
  • UuidToString
  • RpcStringFree

Guid.NewGuid的文档指出了如何实现:

This is a convenient static method that you can call to get a new Guid. The method wraps a call to the Windows CoCreateGuid function.

因此,等同于Guid.NewGuid()的本机是CoCreateGuide()


CoCreateGuid调用UuidCreate,以生成GUID。但是,两个API调用都稍有不同:UuidCreate返回一个UUID(保证对创建它的计算机是唯一的),而CoCreateGuid会生成一个绝对唯一的GUID。

如果您需要决定使用哪个API,请参阅文档中的相关部分。

UuidCreate:

For security reasons, it is often desirable to keep ethernet addresses on networks from becoming available outside a company or organization. The UuidCreate function generates a UUID that cannot be traced to the ethernet address of the computer on which it was generated. It also cannot be associated with other UUIDs created on the same computer.

CoCreateGuid:

The CoCreateGuid function calls the RPC function UuidCreate, which creates a GUID, a globally unique 128-bit integer. Use CoCreateGuid when you need an absolutely unique number that you will use as a persistent identifier in a distributed environment.


在Windows中生成新的Guid并以字符串形式获取结果值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#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进行字符串转换

1
2
3
4
5
6
7
8
9
10
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);