简体   繁体   中英

Proper way crossplatfom convert from std::string to 'const TCHAR *'

I'm working for crossplatrofm project in c++ and I have variable with type std::string and need convert it to const TCHAR * - what is proper way, may be functions from some library ?

UPD 1 : - as I see in function definition there is split windows and non-Windows implementations:

#if defined _MSC_VER || defined __MINGW32__
#define _tinydir_char_t TCHAR
#else
#define _tinydir_char_t char
#endif

- so is it a really no way for non spliting realization for send parameter from std::string ?

Proper way crossplatfom convert from std::string to 'const TCHAR *'

TCHAR should not be used in cross platform programs at all; Except of course, when interacting with windows API calls, but those need to be abstracted away from the rest of the program or else it won't be cross-platform. So, you only need to convert between TCHAR strings and char strings in windows specific code.

The rest of the program should use char , and preferably assume that it contains UTF-8 encoded strings. If user input, or system calls return strings that are in a different encoding, you need to figure out what that encoding is, and convert accordingly.

Character encoding conversion functionality of the C++ standard library is rather weak, so that is not of much use. You can implement the conversion according the encoding specification or you can use a third party implementation, as always.

may be functions from some library ?

I recommend this.

as I see in function definition there is split windows and non-Windows implementations

The library that you use doesn't provide a uniform API to different platforms, so it cannot be used in a truly cross-platform way. You can write a wrapper library with uniform function declarations that handles the character encoding conversion on platforms that need it.

Or, you can use another library, which provides a uniform API and converts the encoding transparently.

I came across boost.nowide the other day. I think it will do exactly what you want.

http://cppcms.com/files/nowide/html/

TCHAR are Windows type and it defined in this way:

#ifdef  UNICODE                     
typedef wchar_t TCHAR, *PTCHAR;
#else
typedef char TCHAR, *PTCHAR;
#endif

UNICODE macro is typically defined in project settings (in case when your use Visual Studio project on Windows).

You can get the const TCHAR* from std::string (which is ASCII or UTF8 in most cases) in this way:

std::string s("hello world");
const TCHAR* pstring = nullptr;
#ifdef UNICODE
    std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
    std::wstring wstr = converter.from_bytes(s);
    pstring = wstr.data();
#else
    pstring = s.data();
#endif

pstring will be the result.

But it's highly not recommended to use the TCHAR on other platforms. It's better to use the UTF8 strings (char*) within std::string

As others have pointed out, you should not be using TCHAR except in code that interfaces with the Windows API (or libraries modeled after the Windows API).

Another alternative is to use the character conversion classes/macros defined in atlconv.h. CA2T will convert an 8-bit character string to a TCHAR string. CA2CT will convert to a const TCHAR string (LPCTSTR). Assuming your 8-bit strings are UTF-8, you should specify CP_UTF8 as the code page for the conversion.

If you want to declare a variable containing a TCHAR copy of a std::string:

CA2T tstr(stdstr.c_str(), CP_UTF8);

If you want to call a function that takes an LPCTSTR:

FunctionThatTakesString(CA2CT(stdsr.c_str(), CP_UTF8));

If you want to construct a std::string from a TCHAR string:

std::string mystdstring(CT2CA(tstr, CP_UTF8));

If you want to call a function that takes an LPTSTR then maybe you should not be using these conversion classes. (But you can if you know that the function you are calling does not modify the string outside its current length.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM