C++ UUID到stl字符串

use*_*521 3 c++ winapi

尝试将UUID转换为字符串而不进行提升.我有以下但是将wszUuid分配给字符串guid不起作用.任何人都知道如何做到这一点,所以我可以返回一个字符串?

string Server::GetNewGUID()
{
    UUID uuid;
    ::ZeroMemory(&uuid, sizeof(UUID));

    // Create uuid or load from a string by UuidFromString() function
    ::UuidCreate(&uuid);

    // If you want to convert uuid to string, use UuidToString() function
    WCHAR* wszUuid = NULL;
    ::UuidToStringW(&uuid, (RPC_WSTR*)&wszUuid);
    if (wszUuid != NULL)
    {
        ::RpcStringFree((RPC_CSTR*)&wszUuid);
        wszUuid = NULL;
    }

    string guid; 
    guid = wszUuid;            // ERROR: no operator "=" matches these operands operand types are: std::string = WCHAR*

    return guid;
}
Run Code Online (Sandbox Code Playgroud)

Rem*_*eau 7

string Server::GetNewGUID()
{
    UUID uuid = {0};
    string guid;

    // Create uuid or load from a string by UuidFromString() function
    ::UuidCreate(&uuid);

    // If you want to convert uuid to string, use UuidToString() function
    RPC_CSTR szUuid = NULL;
    if (::UuidToStringA(&uuid, &szUuid) == RPC_S_OK)
    {
        guid = (char*) szUuid;
        ::RpcStringFreeA(&szUuid);
    }

    return guid;
}
Run Code Online (Sandbox Code Playgroud)


小智 3

使用 wstring 代替 string。

wstring  guid;
guid = wszUuid;
Run Code Online (Sandbox Code Playgroud)