1

I'm new to C/C++ coding on windows, and have had this error when running my code. A similar question was asked before which I will link below, however, this solution does not work for me because I do not have the option to change my character set.

argument of type const char* is incompatible with parameter of type "LPCWSTR"

Here is what my code looks like.

#include <Windows.h>

INT CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    PSTR lpCmdLine, INT nCmdShow)
{

    OutputDebugString("Lets test this out \n");

    return 0;
}
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
User9123
  • 675
  • 1
  • 8
  • 20
  • 2
    Did you try `OutputDebugString(L"Lets test this out \n");`? Be aware to use the _w_ versions of `std::wstring` and I/O classes (`wostream`) as well. – πάντα ῥεῖ Dec 05 '20 at 21:11
  • 1
    Or call [`OutputDebugStringA`](https://learn.microsoft.com/en-us/windows/win32/api/debugapi/nf-debugapi-outputdebugstringa), which seems to be the preferred route given the string being passed and how [`OutputDebugStringW`](https://learn.microsoft.com/en-us/windows/win32/api/debugapi/nf-debugapi-outputdebugstringw) works. – 1201ProgramAlarm Dec 05 '20 at 21:12
  • @πάνταῥεῖ that worked! thanks! I'm kind of new to this but what does the L mean? – User9123 Dec 05 '20 at 21:12
  • @User9123 Wide string character literal. – πάντα ῥεῖ Dec 05 '20 at 21:14

1 Answers1

7

There are two ways to fix this.

First is to use a string made of wide characters:

OutputDebugString(L"Lets test this out \n");
//                ^

Second is to call the version of the function that takes a narrow character string:

OutputDebugStringA("Lets test this out \n");
//               ^

Since the Windows API prefers to work with wide character strings, I'd prefer the first solution.

P.S. LPCWSTR stands for "Long Pointer to Constant Wide STRing". The L is obsolete, you can ignore that.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • A third way - use the TEXT() macro: `OutputDebugString(TEXT(“Let’s test this out \n”));` which ensures the string literal is encoded in the way that `OutputDebugString()` (without the A or W suffix) is expecting. – Remy Lebeau Dec 05 '20 at 22:31
  • @RemyLebeau the `TEXT()` or `_T()` macros were useful back when people were transitioning from Ansi to Unicode APIs. Today I'd say if they aren't obsolete, they should be. I'm not going to suggest it unless it's obvious by the question context that it's needed. – Mark Ransom Dec 05 '20 at 22:39
  • 1
    using the TCHAR-based APIs is obsolete in general. Using `OutputDebugStringA` or `outputDebugStringW` directly is best, but if the OP decides to stick with `OutputDebugString` then `TEXT()` should be used to match. – Remy Lebeau Dec 05 '20 at 22:40
  • Just expanding on what Mark said above, Replace all the functions with the error on the, with an A at the end and it will work perfectly. –  Jan 16 '22 at 08:36
  • @Triangle4 I actually gave *two* solutions in this answer, and you emphasized the second; but the better one is the first. Of course there's no harm in adding a W to those function names to be explicit too. – Mark Ransom Jan 16 '22 at 14:41