Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 670 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert char * to LPWSTR

#1
I am trying to convert a program for multibyte character to Unicode.

I have gone through the program and preceded the string literals with `L` so they look like `L"string"`.

This has worked but I am now left with a C style string that won't conform. I have tried the `L` and putting it in `TEXT()` but the `L` gets added to the variable name -- not the string -- if I use `TEXT()`.

I have tried making it a `TCHAR` but then it complains that it cannot convert a `TCHAR` to a `char *`.

What options am I left with?

I know C and C++ are different. It is an old in-house C library that has been used in C++ projects for several years now.
Reply

#2
This version, using the Windows API function `MultiByteToWideChar()`, handles the memory allocation for arbitrarily long input strings.


int lenA = lstrlenA(input);
int lenW = ::MultiByteToWideChar(CP_ACP, 0, input, lenA, NULL, 0);
if (lenW>0)
{
output = new wchar_t[lenW];
::MultiByteToWideChar(CP_ACP, 0, input, lenA, output, lenW);
}

Reply

#3
The clean way to use `mbstowcs` is to call it twice to find the length of the result:

const char * cs = <your input char*>
size_t wn = mbsrtowcs(NULL, &cs, 0, NULL);

// error if wn == size_t(-1)

wchar_t * buf = new wchar_t[wn + 1](); // value-initialize to 0 (see below)

wn = mbsrtowcs(buf, &cs, wn + 1, NULL);

// error if wn == size_t(-1)

assert(cs == NULL); // successful conversion

// result now in buf, return e.g. as std::wstring

delete[] buf;

Don't forget to call `setlocale(LC_CTYPE, "");` at the beginning of your program!

The advantage over the Windows `MultiByteToWideChar` is that this is entirely standard C, although on Windows you might prefer the Windows API function anyway.

I usually wrap this method, along with the opposite one, in two conversion functions `string`->`wstring` and `wstring`->`string`. If you also add trivial overloads `string`->`string` and `wstring`->`wstring`, you can easily write code that compiles with the Winapi `TCHAR` typedef in any setting.

[*Edit:*] I added zero-initialization to `buf`, in case you plan to use the C array directly. I would usually return the result as `std::wstring(buf, wn)`, though, but do beware if you plan on using C-style null-terminated arrays.[/]

In a multithreaded environment you should pass a thread-local conversion state to the function as its final (currently invisible) parameter.

Here is a [small rant](

[To see links please register here]

) of mine on this topic.
Reply

#4
You may use `CString`, `CStringA`, `CStringW` to do automatic conversions and convert between these types. Further, you may also use `CStrBuf`, `CStrBufA`, `CStrBufW` to get RAII pattern modifiable strings
Reply

#5
I'm using the following in VC++ and it works like a charm for me.

CA2CT(charText)
Reply

#6
The [`std::mbstowcs`][1] function is what you are looking for:

char text[] = "something";
wchar_t wtext[20];
mbstowcs(wtext, text, strlen(text)+1);//Plus null
LPWSTR ptr = wtext;

for `string`s,

string text = "something";
wchar_t wtext[20];
mbstowcs(wtext, text.c_str(), text.length());//includes null
LPWSTR ptr = wtext;

--> ED: The "L" prefix only works on string literals, not variables. <--

[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through