Converting char to LPCWSTR
I recently switched from Dev-C++ to VS 2005 but when i try compiling this program:
Code:
#include "stdafx.h"
#include <windows.h>
int main()
{
MessageBox(NULL,"Text.","Caption.",MB_OK);
return 0;
}
it says that it can't convert char[4] to LPCWSTR.
This compiled perfectly fine in Dev-C++. Is there any setting that i need to change to get it to work?
Re: Converting char to LPCWSTR
This -
Quote:
Originally Posted by Bliutte
This compiled perfectly fine in Dev-C++.
...even if it compiled fine is not correct.
Your parameters are actually wrong.
MessageBox API accepts a TCHAR string.
The correct way to call it is -
Code:
MessageBox(NULL,_T ("Text."),_T("Caption."),MB_OK);
You faced the compile error on MSVC because _UNICODE was defined, and not on Dev C++.
Re: Converting char to LPCWSTR
Ok, thanks for the quick reply! :)
Re: Converting char to LPCWSTR
Re: Converting char to LPCWSTR
Does this work with strings? I tried this:
Code:
char text = (char)"Text.";
MessageBox(NULL,_T (text), _T ("Caption."),MB_OK);
That gave this error:
Code:
error C2065: 'Ltext' : undeclared identifier
How do i get it to work with strings?
And it also said:
Code:
error C2440: 'initializing' : cannot convert from 'const char [6]' to 'char'
before i put (char) before "Text.". Is there any way to make VC convert it automatically?
Re: Converting char to LPCWSTR
Quote:
Originally Posted by Bliutte
Does this work with strings? I tried this:
Code:
char text = (char)"Text.";
I hope you realize that the variable called text is not a string - it is a character.
The correct code would be -
Code:
const char* text = "Text.";
And...
Quote:
Originally Posted by Bliutte
it also said:
Code:
error C2440: 'initializing' : cannot convert from 'const char [6]' to 'char'
...Look, it actually told you just what I did.
It said that you cannot assign a string (i.e. a character array) to a single character.
Also, like I said before, Win APIs like MessageBox typically accept TCHAR strings... So, your text variable should correct be -
Code:
const TCHAR* text = _T ("Text.");
And the call to MessageBox would be -
Code:
MessageBox(NULL, text, _T ("Caption."),MB_OK);
Re: Converting char to LPCWSTR
Quote:
Originally Posted by Bliutte
How do i get it to work with strings?
You can always use C++ std::string or std::wstring class by including <string>
If your MSVC project is one with MFC support, you can also use CString (wraps TCHAR string).
Re: Converting char to LPCWSTR
Oh, sorry about that stupid post. I guess i am too tired right now. :o
Thanks for the reply though.
Re: Converting char to LPCWSTR
I had a similar problem. I made the following chnages
MessageBox(NULL, _T(errorMsg), _T("socketIndication"), MB_OK);
Now it says: _T identifier not found.
Can you please help me here.
Thanks,
deve06
Re: Converting char to LPCWSTR
Quote:
Originally Posted by
deve06
I had a similar problem. I made the following chnages
MessageBox(NULL, _T(errorMsg), _T("socketIndication"), MB_OK);
Now it says: _T identifier not found.
Can you please help me here.
Thanks,
deve06
you need to include tchar.h
----
also if you dont want to include tchar or use that you do this .
LPCWSTR Hello = L"salam";
MessageBox(NULL, L"Text.", Hello, MB_OK);
_T() is a funtion of Tchar that do the magic , but you need to include tchar.h , you can use L"somestring" . done.
Re: Converting char to LPCWSTR
Quote:
Originally Posted by
Punsher2011
[...]
_T() is a funtion of Tchar that do the magic [...]
_T() is not a function but a macro.
Re: Converting char to LPCWSTR
Quote:
Originally Posted by
ovidiucucu
_T() is not a function but a macro.
sorry , my mistake ! :blush:
Re: Converting char to LPCWSTR
Quote:
Originally Posted by
Punsher2011
sorry , my mistake ! :blush:
You also made the mistake of reviving a 7 year old thread. :p
Re: Converting char to LPCWSTR
Quote:
Originally Posted by
OReubens
You also made the mistake of reviving a 7 year old thread. :p
And the mistake of not identifying that the _T() macro doesn't work with a variable (i.e. _T(errorMsg) is incorrect).