How to transform const wchar_t type to LPTSTR (C++)

Lluís

I am trying to modify an old plugin to create a new one (in C++ and Visual Studio 2019). When I compile I get the following error marking TEXT in red.

E0144: A value of type "const wchar_t *" cannot be used to initialize an entity of type LPTSTR

LPTSTR process_name = TEXT("rFactor2.exe");
module_address = GetModuleBase(process_name, pID);

I investigated and saw a similar post suggesting this:

LPTSTR process_name = foo(TEXT("rFactor2.exe"));

And now I get the following error:

E0020: identifier "foo" is not defined

Could someone tell me how can I create the variable in LPTSTR format (it's the type that GetModuleBase expects)?

Ayxan Haqverdili

LPTSTR is defined as TCHAR*. What you want is a const pointer. You can use LPCTSTR, which is defined as TCHAR const*:

LPCTSTR process_name = TEXT("rFactor2.exe");

If your function requires a non-const pointer, you can create a copy:

TCHAR process_name[] = TEXT("rFactor2.exe");

Note that life time of the string literal and the array are not the same.


it's the type that GetModuleBase expects

Considering you are working with legacy code, it is possible that your functions take non-const pointers and don't modify them. If you are certain about that and can't go ahead and fix those function signatures to be const-correct, you can use a type cast. Do this only as a last resort:

auto process_name = const_cast<LPTSTR>(TEXT("rFactor2.exe"));

Recommended reading:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Argument of type "const wchar_t*" is incompatible with parameter of type "LPTSTR"

C++ Argument of type "const wchar_t" * incompatible with parameter of type "wchar_t"

how wchar_t data type working c?

C++ wifstream: Incompatible type char const*, wchar_t const*

How to fix " State Error (active) E0513 a value of type “const wchar_t *” cannot be assigned to an entity of type “wchar_t *”"

Understanding wchar_t type in c++

How to delete a file that has a name of type wchar_t in C++ in a portable way?

How could I assign the value from lua_tostring() to a variable in type of wstring or const wchar_t* in utf-8

How to use regex_iterator with const char/wchar_t*?

How to make concatenations at const wchar_t* parameters?

How to convert int variable and append to const wchar_t*

Error: argument of type "const wchar_t *" is incompatible with parameter of type "WCHAR *"

Is " const wchar_t* " a pointer?

Convert const wchar_t* into a WCHAR*

Type conversion from string to const wchar_t* . Type does not match

Visual Studio 2017; argument of type "const wchar_t *" is incompatible with parameter of type "PWSTR" when /permissive is enabled

How to change wchar.h to make wchar_t the same type as wint_t?

How to convert Python list of strings into C array of wchar_t?

How to copy/convert a char string to a wchar_t string in C?

C++: How to swap byte order of a wchar_t

C++ Builder 10.3 can not assign to const wchar_t* from const char[18]

Convert const char* to const wchar_t*

Cannot convert parameter 1 from 'const wchar_t *' to 'LPCTSTR' in MFC / C++ project

How to convert a data of type LPTSTR to HWND?

Convert LOWORD(wParam) to const wchar_t*

Make a condition on const wchar_t*

Convert const wchar_t* to LPWSTR

GLUT Problem: error redeclaration of c++ built-in type 'wchar_t'

GETTING THE ERROR IN VISUAL STUDIO: error C2664: '_chmod' : cannot convert parameter 1 from 'wchar_t [260]' to 'const char *'