Embed in LPCWSTR?

I am trying to create a (very) simple Win32 GUI program, but for some reason the compiler (I use VC ++ 2008 Express) wants me to manually output each line or char * to LPCWSTR:

I get this compiler error every time I do this, for example, I get this error for "Hello" and "Note":

error C2664: 'MessageBoxW': cannot convert parameter 2 from 'const char [22]' to 'LPCWSTR'

Please tell me that I do not need to quit every time I do it ....

Here is the code:

#include <windows.h> int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MessageBox(NULL, "Hello", "Note", MB_OK); return 0; } 
+6
c windows winapi unicode
source share
4 answers

The standard for new projects in VS2008 is the creation of applications that support UNICODE. You can either change this default value, or return to using ANSI or MBCS applications (Properties-> Configuration Properties-> General-> Character Set) or use Unicode strings as follows:

 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MessageBox(NULL, L"Hello", L"Note", MB_OK); return 0; } 

Do not convert strings to LPCWSTR, because this will lead to undefined behavior! A char is not the same as wchar_t!

+14
source share

The problem is that you are creating for UNICODE and passing strings other than Unicode.

Try:

 MessageBox(NULL, L"Hello", L"Note", MB_OK); 

either tune your build for ANSI / MBCS or look at using TCHARs (which are a pain).

+1
source share

My Win32 C programming memories are vague, but as I recall, you need to start by wrapping your string literals in this macro:

_T ("MyString")

When creating Unicode, this is converted to a Unicode string.

If you are only creating Unicode or are sure that you are working only with a specific Unicode string, you can use the L "" token, which makes the _T macro under covers.

You may need to include the tchar.h header.

When doing win32 programming, I usually declare the lines as TCHAR * szWh regardless of the fact that everything works on Win9x in much the same way as NT / Win2k / XP. (There are other convenience macros such as LPTSTR, etc., And MFC contains some simple conversion macros for cases where you really need to convert ansi and unicode to call certain APIs).

+1
source share

As stated in other posts, you are creating a Unicode application. You can switch to and from the Unicode project in the project settings (do not forget to set it for the "Debug" and "Vacation" configurations.

If you want to use it, you will need to add all your static lines with L:

 L"Some static string" 

For char [] strings, there is the mbstowcs_s method, which is used something like this:

 std::string str; // the string you want to convert WCHAR wstr[128]; size_t convertedChars = sizeof(wstr)/sizeof(WCHAR); mbstowcs_s(&convertedChars, wstr, str.c_str(), _TRUNCATE); 

This is how I used it in one of my projects. For accurate use, refer to MSDN .

0
source share

All Articles