473,406 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 36345
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

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

Similar topics

3
by: Michael Breidenstein | last post by:
Hi, anybody can help me to convert a "char *" to LPCWSTR with the following function: LPCWSTR charP2LPCWSTR(char *szText){ LPCWSTR tmp; tmp=szText; // At this point I need help
3
by: Gee | last post by:
Hi Can anyone help me convert this to C# please? Structure NETRESOURCE Public dwScope As Int32 Public dwType As Int32 Public dwDisplayType As Int32 Public dwUsage As Int32 Public...
8
by: ppcdev | last post by:
Here's what I try : LPCTSTR tst = (LPCTSTR) (LPCWSTR) Marshal::StringToHGlobalUni(str); c:\MyNetPrj\Prj0001\stunt.cpp(244): error C2440: 'type cast' : cannot convert from 'System::IntPtr' to...
1
by: Lewap | last post by:
Hi I use in my project GetWindowText function which return LPSTR value i want input this value (string represented by LPSTR) to TextBox field. How to convert from LPSTR to String * __gc? --...
5
by: Paul | last post by:
Hi, Any one knows how to convert CString to LPCWSTR? Please advice, thanks! -P
3
by: Maileen | last post by:
Hi, How can we convert string^ to String or to LPCWSTR ? thx, Maileen
2
by: Neo | last post by:
how convert LPCSTR to LPCWSTR? regards, Mohammad Omer Nasir.
1
by: asenthil | last post by:
Hai this is senthil... I had tried to write a string which fetched from a database. into a file... when i tried to compile the solution the following error occurs like this error C2664:...
2
by: dehi | last post by:
Hello, My problem ist that I don't know how to convert a variable from LPWSTR to LPCWSTR. Anyone an idea? Regards, Denis
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.