1

i'm trying to display in client area of my window app for example a system local hour.

In rezult I get an error: IntelliSense: argument of type "CHAR *" is incompatible with parameter of type "LPCWSTR" at TextOut function(parameter 4). Can somebody help me?

case WM_PAINT:

    hdc = BeginPaint(hWnd, &ps);
       SYSTEMTIME lt;
       GetLocalTime(&lt);
       CHAR info[20] ;
       _itoa(lt.wHour,info,16);
       TextOut(hdc,200,200,info,strlen(info));
    EndPaint(hWnd, &ps);}

break;
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
user3528605
  • 11
  • 1
  • 2
  • Have a look here: http://stackoverflow.com/questions/21834833/argument-of-type-const-char-is-incompatible-with-parameter-of-type-lpcwstr This should solve your issue. – Markus May 04 '14 at 13:35
  • You mean string to wide string. – chris May 04 '14 at 13:41

2 Answers2

3

You are compiling your program for Unicode which means that Win32 functions that operate on text are mapped to wide versions. So TextOut is a macro that expands to TextOutW and expects UTF-16 encoded const wchar_t* text. But you are providing 8 bit text.

A simple fix is to call the ANSI version of TextOut, namely TextOutA.

TextOutA(hdc, 200, 200, info, strlen(info));

But in the longer run you might consider sticking to the native wide API. This would require you to use wide versions of any text processing functions. You would also do well to avoid itoa and start using standard C++ methods for converting between text and integer.

For example, using C++11 you could use std::to_wstring.

std::wstring hour = std::to_wstring(lt.wHour);
TextOut(hdc, 200, 200, hour.c_str(), hour.length());
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
0

Usually what you'll want is to use the wide-character alternative to _itoa(). In this case, you'd want _itow(). Reference.

So you'd do it like this:

case WM_PAINT: {
    hdc = BeginPaint(hWnd, &ps);
    SYSTEMTIME lt;
    GetLocalTime(&lt);
    WCHAR info[20];
    _itow(lt.wHour, info, 16);
    TextOut(hdc, 200, 200, info, wcslen(info));
    EndPaint(hdc, &ps);
}
break;

Particularly, note the use of WCHAR instead of CHAR, _itow() instead of _itoa() and wcslen() instead of strlen().

Also, note the distinction of the length of the string "in characters" (as the documentation of TextOut() points out) and its length "in bytes". In a so-called "Ansi string", they are identical, but in WCHAR strings they are different (each character in the BMP takes two bytes; each character outside of the BMP takes four).

Otherwise, the code is directly analogous. In time you'll get the hang of using these wide-character string functions rather than the regular ones when programming using UTF-16.

Wtrmute
  • 168
  • 5