0
SetTextColor(hdc, RGB(0, 0, 0));
char str[20]="";
sprintf(str, "sorce: %d", sorce);
TextOut(hdc, 930, 810, str,strlen(str));

It showed the error that char* cannot be converted to LPCWSTR. How can I solve it?

Michael Tsai
  • 751
  • 2
  • 11
  • 21
  • Use wsprintf instead of sprintf. No need to do any conversion – Danh Jan 01 '17 at 04:40
  • Or use [`std::to_wstring`](http://en.cppreference.com/w/cpp/string/basic_string/to_wstring) – Danh Jan 01 '17 at 04:43
  • could you show me an example? Thank you very much. – Michael Tsai Jan 01 '17 at 04:51
  • 4
    If one was to google "stack overflow c++ how to convert LPCWSTR and char?" one might quickly find numerous existing questions about this kind of thing, including http://stackoverflow.com/questions/3225682/convert-char-to-lpcwstr which is very similar to this case. – TheUndeadFish Jan 01 '17 at 05:21

1 Answers1

0

The W in LPCWSTR means wide characters. You have a few options.

You can change from using char types to wchar_t

You can convert using codecvt or Win32 API

Use TextOutA to keep using char types.

Boofhead
  • 511
  • 1
  • 3
  • 7
  • Could you give an example? Because I want to output the string and the variable. I don't know how can do that. Thank you. – Michael Tsai Jan 01 '17 at 04:48