Cast to LPCWSTR?
Asked Answered
M

4

8

I'm trying to create a (very) simple Win32 GUI program, but for some reason the compiler (I'm using VC++ 2008 Express) wants me to manually typecast every string or char* to LPCWSTR:

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

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

Please tell me I don't have to cast every time I do this....

Here's the code:

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
    LPSTR lpCmdLine, int nCmdShow)
{
    MessageBox(NULL, "Hello", "Note", MB_OK);
    return 0;
}
Mare answered 20/3, 2009 at 19:27 Comment(1)
or, simply invoke MessageBoxA(NULL, "Hello", "Note", MB_OK);Chatman
M
15

The default for new projects in VS2008 is to build UNICODE aware applications. You can either change that default and go back to using ANSI or MBCS apps (Properties->Configuration Properties->General->Character Set), or use Unicode Strings like this:

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

Do not cast your strings to LPCWSTR because that will lead to undefined behavior! A char is not the same as a wchar_t!

Mentality answered 20/3, 2009 at 19:33 Comment(0)
U
1

The problem is that you're building for UNICODE and are passing non-Unicode strings.

Try:

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

or set up your build for ANSI/MBCS or look into using TCHARs (which are a pain).

Unionism answered 20/3, 2009 at 19:31 Comment(0)
S
1

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

_T("mystring")

When you build unicode, this will be converted to a unicode string.

If you only build unicode, or you are sure that you are only handling a particular string in unicode, you can use the L"" marker, which is what the _T macro does under the covers.

You may need to include the tchar.h header.

When doing win32 programming, I usually declare strings as TCHAR * szWhatever so that things work on Win9x almost as well as NT/Win2k/XP. (There are other convenience macros in there as well such as the LPTSTR and so on, and MFC contains some easy conversion macros for the cases when you actually need to convert between ansi and unicode to call specific APIs).

Secondbest answered 20/3, 2009 at 19:35 Comment(0)
S
1

As other posts said, you are building a Unicode application. You can switch to and from Unicode project in project settings (don't forget to set it for both "Debug" and "Release" configurations.

If you want to use it, you'll have to prepend all your static strings with L:

L"Some static string"

For char[] strings there's a method mbstowcs_s, which is used roughly 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 exact usage refer to MSDN.

Subsellium answered 20/3, 2009 at 19:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.