469,267 Members | 1,643 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,267 developers. It's quick & easy.

convert LPSTR to LPCWSTR

Hello :)

I'm not new to C++, I just don't use it as much as I used to.

But anyway, I need to convert LPSTR to LPCWSTR.

Here's the function i'm using it in:
Expand|Select|Wrap|Line Numbers
  1. MYCOMMAND void PrintText( LPSTR pString )
  2.  {
  3.     if(pString)
  4.     {
  5.         MessageBox(NULL, pString, "", MB_OK);
  6.     }
  7.  }
This gives an error.

Help is appreciated.

Thanks.
Mar 15 '09 #1
9 33136
weaknessforcats
9,208 Expert Mod 8TB
MessageBox is a macro that calls MessageBoxA or MessageBoxW depending upon the character set of your project.

Microsoft provides a set of macros called the TCHAR mappings that you are to use in your programs. You might Google TCHAR for more info.

In your specific case, use the TEXT TCHAR macro:
Expand|Select|Wrap|Line Numbers
  1. MessageBox(NULL, TEXT(pString), TEXT(""), MB_OK);
Mar 15 '09 #2
that doesn't work.

I get this error:
error C2065: 'LpString' : undeclared identifier
Mar 15 '09 #3
weaknessforcats
9,208 Expert Mod 8TB
My mistake. TEXT is used for literals to create an LPCTSTR.

This is what I should have said:

Expand|Select|Wrap|Line Numbers
  1. MessageBox(NULL, (LPCTSTR)(pString), TEXT(""), MB_OK); 
You would typcast your LPSTR to a LPCTSTR which represents an LPCSTR if you program is compiled using ASCII or an LPCWSTR is your code is compiled using Unicode. The MessageBox function needs LPCTSTR arguments.

The only difference between a LPSTR and an LPCSTR is that the LPCSTR is constant and the LPSTR is not.
Mar 16 '09 #4
Thank you. The code compiles and makes the dll and everything. :)

But, when I run the function, the main text is all garbled and in these Chinese characters and things. Any help with that? How would I convert the LPSTR to a regular string?
Mar 17 '09 #5
weaknessforcats
9,208 Expert Mod 8TB
Try this:
Expand|Select|Wrap|Line Numbers
  1. CHAR pString[] = "Hello world!";
  2. WCHAR pWideString[80];
  3. MultiByteToWideChar(CP_ACP,0,pString,-1,pWideString,80);
  4. MessageBoxW(NULL, pWideString, TEXT(""), MB_OK);  
All MessageBox does is call MessageBoxA or MessageBoxW.

All MessageBoxA does is convert your LPCTSTR string to a WCHAR string and then calls MessageBoxW.

That is, it is MessageBoxW that ios always called.

All this switching between ASCII and Unicode means switching your code between CHAR (char) and WCHAR (wchar_t) automatically. To help this out, Microsoft has developed the TCHAR system of macros. A TCHAR is a char when you are using ASCII (called multibyte by Microsoft) and a wchar_t when you are using Unicode.

The TCHAR equivalent of your code is:
Expand|Select|Wrap|Line Numbers
  1. TCHAR pString[] = TEXT("Hello world!");
  2. MessageBox(NULL, pString, TEXT(""), MB_OK);  
Here the TEXT macro has properly converted your string.

You should be using only TCHAR and the TCHAR function mappings in your code. That is, you should not be using things like LPSTR in programs that need to run both multibyte and Unicode.

Considering that all new code need only run Unicode, use only WCHAR and MessageBoxW.
Mar 17 '09 #6
I thank you so much for this explaining. I really do. :)

I can't use your example because I have to use the LPSTR argument in the function. (It's for a plugin I'm making.)

But just another question:

Expand|Select|Wrap|Line Numbers
  1. string Nstr = pString;
(I want this because I want to edit the string with string functions.)
I've compiled it and it compiles fine. How would I convert the string into the type MessageBox needs?

Thank you so much. :)
Mar 18 '09 #7
weaknessforcats
9,208 Expert Mod 8TB
You mean Nstr?

If so, you use:
Expand|Select|Wrap|Line Numbers
  1. string Nstr = "Hello world";
  2. WCHAR pWideString[80]; 
  3. MultiByteToWideChar(CP_ACP,0,Nstr.c_str(),-1,pWideString,80); 
  4. MessageBoxW(NULL, pWideString, TEXT(""), MB_OK);   
string::c_str() returns a const char* that points to a C-string representing the data in the string object. Use that in your MultuByteToWideChar conversion to Unicode.
Mar 18 '09 #8
Thank you for all of the help.

Luckily, I found a better way to accomplish what I wanted to do with a language I'm more comfortable with. (VB/C#)


Thanks again soo much. :)

I'm glad I could learn some things from this experience.
Mar 19 '09 #9
weaknessforcats
9,208 Expert Mod 8TB
That's true but VB/C# is slower because they contain code to make your life easier. Ease in programming always has a speed penalty.
Mar 19 '09 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Michael Breidenstein | last post: by
3 posts views Thread by Gee | last post: by
1 post views Thread by Lewap | last post: by
5 posts views Thread by Paul | last post: by
3 posts views Thread by Maileen | last post: by
2 posts views Thread by Neo | last post: by
2 posts views Thread by dehi | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.