LPCWSTR
I have a small C++ app in which I am just trying to display a message box. The following is all of the code in my app.
#include <windows.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
int temp;
temp = 0;
MessageBox (NULL,temp , "Tutorial", 0);
return 0;
}
When I try to run the app, I receive the following error:
error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'int' to 'LPCWSTR'
Can anyone help me with this error? Thank you.
int MessageBox( HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType);
You're passing an integer (temp) but the function expects a string.
Try this instead:
MessageBox(NULL,NULL,_T("Tutorial"), 0);
_T() is a macro that prefixes an "L" on the string literal when compiling with UNICODE defined. It will make the literal compatible with LPCWSTR. Include tchar.h.
Thanks LessBread. I was able to get a msgbox to display based on your suggestions. I now have the following code and it works correctly.
#include <windows.h>
#include <string>
#include <tchar.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
std::string temp;
temp = "Hello";
MessageBox(NULL,_T("Hello"),_T("Tutorial"),0);
return 0;
}
I am still wondering how to pass a string to the msgbox though. If it try the following line:
MessageBox(NULL,temp,_T("Tutorial"),0);
I still get the following error:
error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'std::string' to 'LPCWSTR'
#include <windows.h>
#include <string>
#include <tchar.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
std::string temp;
temp = "Hello";
MessageBox(NULL,_T("Hello"),_T("Tutorial"),0);
return 0;
}
I am still wondering how to pass a string to the msgbox though. If it try the following line:
MessageBox(NULL,temp,_T("Tutorial"),0);
I still get the following error:
error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'std::string' to 'LPCWSTR'
Thanks Razor. That worked for passing a string.
I do have another general question though. LessBread mentioned something about "compiling with UNICODE defined". I am not sure what this means. I googled UNICODE and have an understanding of what it is. However, I do not think I need it. Is it possible for me to set up my program to compile without UNICODE?
I do have another general question though. LessBread mentioned something about "compiling with UNICODE defined". I am not sure what this means. I googled UNICODE and have an understanding of what it is. However, I do not think I need it. Is it possible for me to set up my program to compile without UNICODE?
If you're using Visual Studio:
Project -> Properties -> Configuration Properties -> General -> Character Set -> "Not Set".
P.S.
You'll have to apply this to all of the configurations that you'll be using, for example, "Debug" and "Release", or you can do it once by chosing "All Configurations".
Project -> Properties -> Configuration Properties -> General -> Character Set -> "Not Set".
P.S.
You'll have to apply this to all of the configurations that you'll be using, for example, "Debug" and "Release", or you can do it once by chosing "All Configurations".
In this context, UNICODE, is a preprocessor token.
Here's the guts of the _T macro to illustrate:
If UNICODE is defined, _T inserts L before the string literal. If UNICODE is not defined, the string literal is untouched.
You can define UNICODE before the includes, or through the project settings along the lines that raz0r decribed.
Here's the guts of the _T macro to illustrate:
#ifdef UNICODE#define __T(x) L ## x#else#define __T(x) x #endif#define _T(x) __T(x)
If UNICODE is defined, _T inserts L before the string literal. If UNICODE is not defined, the string literal is untouched.
You can define UNICODE before the includes, or through the project settings along the lines that raz0r decribed.
Quote:Original post by CTEagle
Thanks LessBread. I was able to get a msgbox to display based on your suggestions. I now have the following code and it works correctly.
#include <windows.h>
#include <string>
#include <tchar.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
std::string temp;
temp = "Hello";
MessageBox(NULL,_T("Hello"),_T("Tutorial"),0);
return 0;
}
I am still wondering how to pass a string to the msgbox though. If it try the following line:
MessageBox(NULL,temp,_T("Tutorial"),0);
I still get the following error:
error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'std::string' to 'LPCWSTR'
try this:
MessageBox(NULL,temp.c_str(),_T("Tutorial"),0);
TEXT("Whatever") also seems to work for me:
MessageBox(hWnd, TEXT("HEY!"), TEXT("hey there!"), MB_OK);
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement