c++ UUID到stl字符串

C++ UUID to stl string

本文关键字:字符串 stl UUID c++      更新时间:2023-10-16

尝试将UUID转换为字符串而不进行boost。我有以下内容,但将wszUuid分配给字符串guid不起作用。有人知道我怎么做,这样我就可以返回一个stl字符串吗?

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;
}
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;
}

使用wstring代替string。

wstring  guid;
guid = wszUuid;