0

I declared two variable like:

WCHAR w_ErrorMessage[256];
LPCWSTR lp_ErrMsg;

The first variable is for returned SQLite error message, And the second variable is for MessageBox text. I tried and wrote this code:

lp_ErrMsg = L"Database sql error: " + WCHAR(w_ErrorMessage);
MessageBox(0, lp_ErrMsg, L"Error", MB_ICONERROR | MB_OK);

but something like this shows for message: ScreenShot

If I remove the WCHAR behind the w_ErrorMessage variable I get the "Error: Cannot add two pointers".

I want MessageBox show error: SQLite Error: %TheActualErrorMessage%. How can I do that?

  • 1
    This is the most pretty and surprising case of **Undefined Behavior** I ever saw. It would be interesting to know whether these Chinese characters make any sense... – Scheff's Cat Jul 09 '17 at 10:36
  • `WCHAR(w_ErrorMessage)` converts the array `w_ErrorMessage` (which is probably implicitly casted to `(WCHAR*)` before) to `WCHAR`. Literally, a pointer casted to a wide character. This makes the compiler silent but does probably not provide what is intended. Btw.: If you use `wcscat()` as recommended in the answer, please, don't forget to allocate sufficient memory for the destination pointer. – Scheff's Cat Jul 09 '17 at 10:41

1 Answers1

0

The + operator is not used to concatenate strings in C/C++. Use wcscat function instead.

Harsh Shankar
  • 506
  • 5
  • 16