2

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)?

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
Lluís
  • 55
  • 1
  • 7
  • Does this answer your question? [How to convert from wchar\_t to LPSTR?](https://stackoverflow.com/questions/8532855/how-to-convert-from-wchar-t-to-lpstr) – Asesh Jan 09 '21 at 10:01
  • Thank you very much. But for trying this I get a similar problem: value type "const char*" can not be used to start a entity "LPTSTR" type – Lluís Jan 09 '21 at 10:41
  • you are trying to assing a `const TCHAR*` (LPCTSTR) to a non const one (LPSTR), which you can't in c++. Try declaring process_name as LPCTSTR [This answer](https://stackoverflow.com/a/321448/3276027) can be helpful – Gian Paolo Jan 09 '21 at 10:47

2 Answers2

2

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:

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
  • Thank you very much, only 3rd option made me compile the pcode. Option ```LPCTSTR``` didn't work neither ```auto```. When I'm able I will test it with the computer that has the game. – Lluís Jan 09 '21 at 14:15
  • @Lluís I updated the second option. It's much safer now and it should work fine. – Aykhan Hagverdili Jan 09 '21 at 14:57
  • Now `TCHAR process_name[] = TEXT("rFactor2.exe");` also works and the code compiles correctly. Let's wish it works now as a plugin. Thank you very much! – Lluís Jan 09 '21 at 16:52
  • `const_cast(const char* expr)` solved my problem with using `LPTSTR a = _T(const char* expr)` in case someone is looking for this. – IOviSpot Feb 20 '22 at 21:59
0

last thing where tstr's needed was windows millenium. Really you dont need its stuff