Solved

Convert from LPSTR to LPCWSTR and LPSTR to char*

Posted on 2004-04-12
8
5,757 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
  • 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

809 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