Convert string to LPCTSTR c ++

1

I am looking for the simple way or some way to convert a string to LPCTSTR because lately I have problems with this. For example, I have any string:

string welcome;

And I want to use this string in my LPCTSTR .

LPCTSTR ejemplo = "aqui vendría mi string welcome compatible con LPCTSTR";

But I can not make it compatible.

    
asked by jeronimo urtado 13.12.2016 в 00:56
source

1 answer

2

First, we must know what daemons is a LPCTSTR .

LPCTSTR .

It's just a constant pointer to char or wchar_t hidden after an alias:

  • L ong.
  • P ointer.
  • C onst.
  • T char.
  • STR ing.

The most confusing part is the T char, which turns out to be another alias with the particularity that defines one or the other type of char depending on whether the macro UNICODE is or is not defined : if the type TCHAR is defined, it will be an alias of WCHAR 1 and it will not be an alias of char .

As for the long pointer part ( L ong P ointer) is a distinction between 16-bit pointers (short pointer) and 32-bit pointers (long pointer), if I'm not mistaken the short pointers are in disuse and are a remnant of the Windows platforms of 16 bits.

So your code:

LPCTSTR ejemplo = "aqui vendría mi string welcome compatible con LPCTSTR";

It would be equivalent to:

const char *ejemplo = "aqui vendría mi string welcome compatible con LPCTSTR";

Or, with UNICODE defined:

const wchar_t *ejemplo = "aqui vendría mi string welcome compatible con LPCTSTR";

Problem.

You forgot to post the compilation error you have, so I'll try to guess what it is.

What you are doing is:

string welcome;
const char *ejemplo = welcome;

Or, with UNICODE defined:

string welcome;
const wchar_t *ejemplo = welcome;

You are confusing the string object with a text string literal ( const char * o const wchar_t * ) which are different types and the compiler does not like:

error: cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' to 'const char*' in initialization
error: cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' to 'const wchar_t*' in initialization

The object string is a class that stores a text string in an internal buffer, manages the life cycle of said buffer and offers additional functionalities for its management and management. A character pointer ( char or wchar_t ) is a basic type.

Solution.

If you want to access the internal data of a string object you should use the string::data or string::c_str ; these methods return a pointer to the beginning of the buffer that contains the string 2 .

string welcome;
LPCTSTR ejemplo = welcome.c_str(); // equivalente a const char *ejemplo

Or, with UNICODE defined:

wstring welcome;
LPCTSTR ejemplo = welcome.c_str(); // equivalente a const wchar_t *ejemplo

Notice that in the case of being in UNICODE the string must be of wide characters (wide string: wstring ).

Tips.

  • DO NOT USE THE MICROSOFT ALIAS , they are not portable (ie they do not exist on other platforms so your code would only compile for Windows), they are confusing (they hide the real type after an alias), they are obsolete (distinguish between long and short pointers) and they are ugly .
  • Use things as they are with the tools offered by the C ++ language itself:

    string welcome;
    const char *normal = welcome.c_str();
    
    wstring wwelcome;
    const wchar_t *unicode = wwelcome.c_str();
    
  • Do not access the internal data of a string , I do not know what use you want to give the LPCTSTR ejemplo but pointing to the internal data of an object is very prone to errors (for example: if destroy welcome the pointer ejemplo will still point to the same place even though the memory has been erased) use the object string directly.
  • 1 Which in turn will be an alias of wchar_t or unsigned short depending on whether the platform for which it is compiled has native support for wide characters or not.

    2 The standard states that string::c_str must return a pointer to a character string ending in a null character, whereas string::data did not have this guarantee until after C ++ 11. p>     

    answered by 13.12.2016 в 09:45