Question about Strings on Windows and OS X (LPWSTR, TCHAR, etc.)

Hi JUCErs,

I am implementing activation in my product using the LimeLM API (TurboActivate) and I am having a hard time finding a cross-platform way to send (UTF-8) strings to it. Honestly, I didn’t think I would ever be spending several hours at looking how to send a couple of strings to an API, but here we are…

The expected types (STRTYPE and STRCTYPE) are defined as follows:

#ifdef _WIN32

    typedef LPWSTR STRTYPE;
    typedef LPCWSTR STRCTYPE;

#else

    typedef char    TCHAR;
    typedef char* STRTYPE;
    typedef const char* STRCTYPE;

#endif

As usual, on OS X, I had no problem - I am passing String::toRawUTF8 () and everything just worked straight forward. But on Windows… this doesn’t work.

On Windows I can do String::toWideCharPointer () (at least it works on Windows 10 64bit) but this doesn’t work on OS X and after reading all I could on the MSDN and stackoverflow I am worried this is not going to even work on all Windows versions I need to support (Windows 7+) and specifically on all variations of filepath encodings…

Do you think this would be a correct implementation?

STRCTYPE convertString (const String& str)
{
#if JUCE_WINDOWS
    return str.toWideCharPointer ();
#else
    return str.toRawUTF8 ();
#endif
}

I would appreciate it if anyone like to share some experience on dealing with this problem in a cross-platform way or specifically how they handled it with TurboActivate.

Cheers,
Nikolay

P.S.

  • May someone enlighten me - why are strings such a mess in C++? Is it the support for old Windows code pages (the pre-UTF extended encodings)? What is the issue there? Is it filepaths that may be encoded differently?