473,841 Members | 1,792 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

convert LPSTR to LPCWSTR

15 New Member
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.

Mar 15 '09 #1
9 36552
9,208 Recognized Expert Moderator Expert
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
X Enterprises
15 New Member
that doesn't work.

I get this error:
error C2065: 'LpString' : undeclared identifier
Mar 15 '09 #3
9,208 Recognized Expert Moderator Expert
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
X Enterprises
15 New Member
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
9,208 Recognized Expert Moderator Expert
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
X Enterprises
15 New Member
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
9,208 Recognized Expert Moderator Expert
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 MultuByteToWide Char conversion to Unicode.
Mar 18 '09 #8
X Enterprises
15 New Member
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
9,208 Recognized Expert Moderator Expert
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

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
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 lpLocalName As String
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 'LPCWSTR' I really get mad with that !!!
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? -- Thanks in advance for any help or suggestions Best regards, Pawel.
by: Paul | last post by:
Hi, Any one knows how to convert CString to LPCWSTR? Please advice, thanks! -P
by: Maileen | last post by:
Hi, How can we convert string^ to String or to LPCWSTR ? thx, Maileen
by: Neo | last post by:
how convert LPCSTR to LPCWSTR? regards, Mohammad Omer Nasir.
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: 'CreateFileW' : cannot convert parameter 1 from 'const char ' to 'LPCWSTR' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
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
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.