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

前端 未结 7 1475
栀梦
栀梦 2021-01-31 14:04

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++ vers

相关标签:
7条回答
  • 2021-01-31 14:38

    Here's a snippet of code to get the resulting string value of the generated 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:

    • UuidCreate
    • UuidToString
    • RpcStringFree
    0 讨论(0)
  • 2021-01-31 14:43

    To get in guid in std::string on Windows

    #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;
    } 
    
    0 讨论(0)
  • 2021-01-31 14:49

    In the new WinRT api you can use winrt::Windows::Foundation::GuidHelper::CreateNewGuid() which returns a struct of winrt::guid type. You can later pass it to winrt::to_hstring to get struct of winrt::hstring type, then pass it to winrt::to_string to get std::string type.

    0 讨论(0)
  • 2021-01-31 15:01

    To generate a new guid in windows and get the resulting value as a string.

    #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

    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);
    
    0 讨论(0)
  • 2021-01-31 15:02

    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:

    UUID newId;
    UuidCreate( &newId );
    

    I believe Guid.NewGuid() simply maps onto it inside the .NET runtime.

    0 讨论(0)
  • 2021-01-31 15:03

    The documentation for Guid.NewGuid points out, how it is implemented:

    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.

    So the native equivalent to Guid.NewGuid() is CoCreateGuide().


    CoCreateGuid calls UuidCreate, to generate a GUID. Both API calls are slightly different, though: While UuidCreate returns a UUID, that is guaranteed to be unique to the computer that created it, CoCreateGuid produces a GUID that is absolutely unique.

    If you need to decide, which API to use, here are the relevant sections from the documentation.

    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.

    0 讨论(0)
提交回复
热议问题