Issues getting UuidToStringW to generate a proper UUID string

147 views Asked by At

Looking at replacing some id's in our main code base with UUIDs so that the ids can be used with multiple instances of our software. Before adding this functionality, I thought I would check out how to use these calls in a console app.

I can get the ASCII version working but I cannot get the wide character string version to work. The wide character version will only generate appears to be the first part of the string as a short while the ASCII version generates the whole UUID. Below is the code for the test, compiled run on VS2019

#include <iostream>
#include <rpc.h>
#pragma comment(lib,"Rpcrt4.lib")

int main()
{
    //-------------------------------------------------------------------------
    // Generate the UUID
    //-------------------------------------------------------------------------
    UUID newUUID;
    RPC_STATUS resultTypeCreate = UuidCreate(&newUUID);
    switch (resultTypeCreate)
    {
        case RPC_S_OK:
            std::cout << "UUID generated properly\n";
            break;
        case RPC_S_UUID_LOCAL_ONLY:
            std::cout << "UUID good for local only\n";
            break;
        case RPC_S_UUID_NO_ADDRESS:
            std::cout << "No hardware address for the UUID generation\n";
            break;
        default:
            std::cout << "Unknown UUID error\n";
    }

    //-------------------------------------------------------------------------
    // ASCII version of the UUID String.
    //-------------------------------------------------------------------------
    std::wcout << "Ascii Version\n";
    RPC_CSTR uuidAsciiString;
    RPC_STATUS resultTypeA = UuidToStringA(&newUUID, &uuidAsciiString);

    switch (resultTypeA)
    {
    case RPC_S_OK:
        std::cout << "UUID string generated properly\n";
        break;
    case RPC_S_OUT_OF_MEMORY:
        std::cout << "Out of memory\n";
        break;
    default:
        std::cout << "Unknown UUID string error\n";
    }

    std::cout << uuidAsciiString;

    resultTypeA = RpcStringFreeA(&uuidAsciiString);

    //-------------------------------------------------------------------------
    // Wide character version of the UUID string
    //-------------------------------------------------------------------------
    std::cout << "\n\n";

    std::wcout << "Wide Character Version\n";
    RPC_WSTR uuidString;
    RPC_STATUS resultTypeW = UuidToStringW(&newUUID, &uuidString);

    switch (resultTypeW)
    {
    case RPC_S_OK:
        std::cout << "UUID string generated properly\n";
        break;
    case RPC_S_OUT_OF_MEMORY:
        std::cout << "Out of memory\n";
        break;
    default:
        std::cout << "Unknown UUID string error\n";
    }

    std::wcout << uuidString;

    resultTypeW = RpcStringFree(&uuidString);
}

The output:

UUID generated properly Ascii Version UUID string generated properly 26d205a4-d4d5-4aa4-8837-38e91c507abc

Wide Character Version UUID string generated properly 00B35B58

1

There are 1 answers

0
Friddy On

Stupid, one of my team said maybe WCOUT doesn't know how to handle a wide RPC_WSTR. Casting it to a LPCWSTR fixed the problem.

'''
    std::wcout << (LPCWSTR)uuidString;
'''