-
December 26th, 2005, 08:29 AM
#1
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?
-
December 26th, 2005, 08:34 AM
#2
Re: Converting char to LPCWSTR
This -
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++.
-
December 26th, 2005, 08:38 AM
#3
Re: Converting char to LPCWSTR
Ok, thanks for the quick reply!
-
December 26th, 2005, 08:39 AM
#4
Re: Converting char to LPCWSTR
You are welcome...
-
December 26th, 2005, 10:48 AM
#5
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?
-
December 26th, 2005, 10:52 AM
#6
Re: Converting char to LPCWSTR
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...
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);
-
December 26th, 2005, 10:55 AM
#7
Re: Converting char to LPCWSTR
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).
-
December 26th, 2005, 10:55 AM
#8
Re: Converting char to LPCWSTR
Oh, sorry about that stupid post. I guess i am too tired right now.
Thanks for the reply though.
-
February 27th, 2006, 08:35 PM
#9
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
-
May 30th, 2013, 01:02 AM
#10
Re: Converting char to LPCWSTR
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.
-
May 30th, 2013, 01:31 AM
#11
Re: Converting char to LPCWSTR
Originally Posted by Punsher2011
[...]
_T() is a funtion of Tchar that do the magic [...]
_T() is not a function but a macro.
-
May 30th, 2013, 02:29 AM
#12
Re: Converting char to LPCWSTR
Originally Posted by ovidiucucu
_T() is not a function but a macro.
sorry , my mistake !
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|