Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ UUID to stl string

Tags:

c++

winapi

Trying to convert a UUID to string without boost. I have the following but assigning the wszUuid to the string guid doesn't work. Anyone know how I can do this so I can return a stl string?

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;
}
like image 608
user441521 Avatar asked Jul 27 '14 12:07

user441521


1 Answers

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;
}
like image 194
Remy Lebeau Avatar answered Sep 27 '22 19:09

Remy Lebeau