Solution for converting char * To lpcwstr

Source: Internet
Author: User

In Windows programming, conversion between strings is often encountered. Conversion from char * To lpcwstr is also a common conversion. The following lists several commonly used conversion methods.

1. multibytetowidechar Function Conversion

The multibytetowidechar function is an API function that converts multiple bytes into a wide byte. Its prototype is as follows:

int MultiByteToWideChar(  UINT CodePage,         // code page  DWORD dwFlags,         // character-type options  LPCSTR lpMultiByteStr, // string to map  int cbMultiByte,       // number of bytes in string  LPWSTR lpWideCharStr,  // wide-character buffer  int cchWideChar        // size of buffer);

The lpcwstr type is actually const wchar *.


Char * szstr = "test string"; wchar wszclassname [256]; memset (wszclassname, 0, sizeof (wszclassname); multibytetowidechar (cp_acp, 0, szstr, strlen (szstr) + 1, wszclassname, sizeof (wszclassname)/sizeof (wszclassname [0]);

2. Convert macros with T2W

Char * szstr = "test string"; cstring STR = cstring (szstr); uses_conversion; lpcwstr wszclassname = new wchar [Str. getlength () + 1]; wcscpy (lptstr) wszclassname, T2W (lptstr) Str. getbuffer (null); Str. releasebuffer ();


3. a2cw Conversion

Char * szstr = "test string"; cstring STR = cstring (szstr); uses_conversion; lpcwstr wszclassname = a2cw (w2a (STR); Str. releasebuffer ();

The above methods are all tested in the Unicode environment.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.