2013/10/31 Ruben Van Boxem <[email protected]>

> Use this to get a wstring, then call c_str() and you have a const wchar_t*:
>
> const wstring convert_to_utf16(const string& utf8_string)
> {
>   // get length
>   int length = MultiByteToWideChar(CP_UTF8, 0, utf8_string.c_str(), 
> static_cast<int>(utf8_string.size()), nullptr, 0);
>   if(!(length > 0))
>     return wstring();
>   else
>   {
>     wstring result;
>     result.resize(static_cast<string::size_type>(length));
>
>     if(MultiByteToWideChar(CP_UTF8, 0, utf8_string.c_str(), 
> static_cast<int>(utf8_string.size()),
>                            &result[0], static_cast<int>(result.size())) == 0 )
>       throw runtime_error("Failure to execute toUTF16: conversion failed.");
>     else
>       return result;
>   }
> }
>
>
Or much simpler if you need no Unicode correctness and the string is indeed
plain ASCII:

std::wstring f(const std::string& oldstring)
{
  std::wstring result;
  result.reserve(oldstring.size());
  std::copy(oldstring.begin(), oldstring.end(), std::back_inserter(result));

  return result;
}

And then use either c_str() or data() on the resulting wstring if you need
a (const) wchar_t*.

Ruben


>
>
>
> 2013/10/31 Incongruous <[email protected]>
>
>> Does anyone know of a way to convert a string to a 'const wchar_t*'
>> I need this conversion for the 3rd parameter in MessageBox(...).
>> MessageBox(NULL,
>>               apstr.c_str(), // Thius works
>>               (const wchar_t*)this->getMessage().c_str(),// getMessage()
>> returns a std:string, it does not work!!
>>               MB_OK);
>>
>> TIA
>>
>>
>>
>> ------------------------------------------------------------------------------
>> Android is increasing in popularity, but the open development platform
>> that
>> developers love is also attractive to malware creators. Download this
>> white
>> paper to learn more about secure code signing practices that can help keep
>> Android apps secure.
>>
>> http://pubads.g.doubleclick.net/gampad/clk?id=65839951&iu=/4140/ostg.clktrk
>> _______________________________________________
>> Mingw-w64-public mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
>>
>
>
------------------------------------------------------------------------------
Android is increasing in popularity, but the open development platform that
developers love is also attractive to malware creators. Download this white
paper to learn more about secure code signing practices that can help keep
Android apps secure.
http://pubads.g.doubleclick.net/gampad/clk?id=65839951&iu=/4140/ostg.clktrk
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to