Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I use the following two routines when converting back and forth between wchar and char-based strings:</p> <pre><code>include &lt;string&gt; // std::string, std::wstring include &lt;algorithm&gt; // std::copy std::wstring StringToWString(const std::string&amp; s) { std::wstring temp(s.length(),L' '); std::copy(s.begin(), s.end(), temp.begin()); return temp; } std::string WStringToString(const std::wstring&amp; s) { std::string temp(s.length(), ' '); std::copy(s.begin(), s.end(), temp.begin()); return temp; } </code></pre> <p>You can now solve your problem by the following assuming 'existingstr' is a wide-char string:</p> <pre><code>std::string narrow_str(WStringToString(existingstr)); LPCSTR win_str = narrow_str.c_str(); </code></pre> <p>You can go the other way just as easily given some 'narrowstr':</p> <pre><code>std::wstring wide_str(StringToWString(narrowstr)); LPWCSTR win_wstr = wide_str.c_str(); </code></pre> <p>Clearly, this is not efficient as you are both allocating a local and then copying it back through the stack (I mitigate that a bit by using the constructor initialization instead of an assignment...some compilers optimize away the extra copy that way).</p> <p>If possible, it would be better to work with the appropriate version of the Windows API by using the UNICODE macro (see <a href="http://msdn.microsoft.com/en-us/library/ff381407%28v=vs.85%29.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ff381407%28v=vs.85%29.aspx</a> - Working With Strings (Windows)).</p> <p>However, assuming that is not possible for compatibility reasons, it does give you some insight as to what needs to happen.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload