-
September 21st, 2006, 12:51 AM
#1
using std::string unicode build
Let us take a simple function
::MessageBox(0,data.c_str(),"log",0);
where data is a std::sting type.
When i change the build type to unicode ,this will spit out error
error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char *' to 'LPCWSTR'
How can i feed in the underlying data of a std::string type(const char *),so that it builds smoothly in both MBCS and UNICODe builds.
-
September 21st, 2006, 12:55 AM
#2
Re: using std::string unicode build
For UNICODE you have std::wstring. You will also have a problem with strings such as "log". They should be replaced with L"log". To compile both with ANSI and UNICODE use the _T() macro to wrap all the strings and chars in your code.
Take a look at this FAQ, http://www.codeguru.com/forum/showthread.php?t=231165.
-
September 21st, 2006, 01:10 AM
#3
Re: using std::string unicode build
thats where the twist comes in ;i dont want to do a ifdef at each line where i have used std::string for the different build-types.
And if i do a _T on c_str member ,it wont accept in UNICODE.
So what is a least-painful way to make this std::string work in such situations - we use std::string at a lot of places in the code ;and we cant use a ifdef/std::wstring option easily?
-
September 21st, 2006, 01:29 AM
#4
Re: using std::string unicode build
Why Not you Follow Cilu Suggestion and Proceed in a Simple Way Note T("string") or TEXT("string") are macros which become L"string" if UNICODE is defined, and "string" if UNICODE is not defined.and to defined unicode you can use #define unicode before you include windows.
here is how you can convert your Unicode to ASCII string and ASCII to Unicode.
Code:
// Convert an ASCII string to a Unicode String
char SomeAsciiStr[] = "Ascii!";
wchar_t SomeUnicodeStr[1024];
MultiByteToWideChar(CP_ACP, 0, SomeAsciiStr, -1, SomeUnicodeStr, 1024);
// Convert a Unicode string to an ASCII string
char SomeAsciiStr[1024];
wchar_t SomeUnicodeStr[] = L"Unicode!";
WideCharToMultiByte(CP_ACP, 0, SomeUnicodeStr, -1, SomeAsciiStr, 1024, NULL, NULL);
for more details have a look on
http://en.wikibooks.org/wiki/Windows...amming/Unicode
Thanx
Last edited by humptydumpty; September 21st, 2006 at 01:33 AM.
-
September 21st, 2006, 01:34 AM
#5
Re: using std::string unicode build
Originally Posted by ranadhir nag
thats where the twist comes in ;i dont want to do a ifdef at each line where i have used std::string for the different build-types.
You can do a single typedef:
Code:
typedef basic_string<TCHAR> tstring;
Then tstring is defiend as string or wstring depending on build-type.
- petter
-
September 21st, 2006, 01:38 AM
#6
Re: using std::string unicode build
got it.Actually what i was seeknig is the A2T macro
-
September 21st, 2006, 01:45 AM
#7
Re: using std::string unicode build
Originally Posted by ranadhir nag
got it.Actually what i was seeknig is the A2T macro
IMO it's better to store the strings in their correct format than to convert them every time they're needed.
- petter
-
September 21st, 2006, 06:30 AM
#8
Re: using std::string unicode build
Sure.i also believe the same.
A couple of questions on this issue:
'typedef basic_string<TCHAR> tstring' expands to a wstring for UNICODE and string for MBCS - is that right?
So,c_str member returns a LPCTSTR(char* in MBCS and wchar* in UNICODE).
is that correct?
-
September 21st, 2006, 06:36 AM
#9
Re: using std::string unicode build
-
September 21st, 2006, 06:41 AM
#10
Re: using std::string unicode build
That's correct.
Example of code that should work in both UNICODE and non-UNICODE build.
Code:
#include <windows.h>
#include <tchar.h>
#include <string>
#pragma comment(lib, "user32.lib")
typedef std::basic_string<TCHAR> tstring;
int main()
{
tstring str = _T("hello");
::MessageBox(0, str.c_str(), _T("log"), 0);
}
- petter
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
|