Solved

Convert from LPSTR to LPCWSTR and LPSTR to char*

Posted on 2004-04-12
8
5,905 Views
Last Modified: 2012-06-21
Hi,
Could somebody provide me an URL or a sample code to convert a LPSTR string to LPCWSTR and from LPSTR string to char*.

Thanks in advance,
Pampa
0
Comment
Question by:pampa34
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
8 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 10804438
windows project can use the MultiByteToWideChar API function to convert an ANSI string to a UNICODE string.
0
 
LVL 30

Expert Comment

by:Axter
ID: 10804448
No conversion is required for converting a LPSTR to char*.

Example:

      LPSTR str1 = "Hello World";
      char* str2 = str1;
      char  str3[33];
      strcpy(str3, str1);
0
 
LVL 30

Accepted Solution

by:
Axter earned 75 total points
ID: 10804467
Example:
void Function(void)
{
   char dataBuff[] = "abcdefghijklmnopq";

   wchar_t pwsz[sizeof(dataBuff)*2];
   MultiByteToWideChar(CP_ACP, 0, dataBuff, strlen(dataBuff), pwsz, sizeof(pwsz));
   LPCWSTR Data = pwsz;
}

0
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 75 total points
ID: 10805403
0
 

Expert Comment

by:Hyperon
ID: 10808972
you should be able to simply typecast them eg.

LPCWSTR mylpcwstr = (LPCWSTR) mylpstr;
char *mypchar = (char *) mylpstr;

this has always worked for me :)
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question