Convert std::string to LPCWSTR (best way in c++)<p>Hi!</p> <p>How can I convert an std::string to a LPCWSTR? What is the best way to do it in C++?</p> <p>Thanks.</p> <p>&quot;Ciao guagliò!&quot;</p>© 2009 Microsoft Corporation. All rights reserved.Wed, 16 Sep 2009 10:25:46 Z0f749fd8-8a43-4580-b54b-fbf964d68375http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvc/thread/0f749fd8-8a43-4580-b54b-fbf964d68375#0f749fd8-8a43-4580-b54b-fbf964d68375http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvc/thread/0f749fd8-8a43-4580-b54b-fbf964d68375#0f749fd8-8a43-4580-b54b-fbf964d68375FabioDeSantishttp://social.msdn.microsoft.com/Profile/en-US/?user=FabioDeSantisConvert std::string to LPCWSTR (best way in c++)<p>Hi!</p> <p>How can I convert an std::string to a LPCWSTR? What is the best way to do it in C++?</p> <p>Thanks.</p> <p>&quot;Ciao guagliò!&quot;</p>Thu, 06 Apr 2006 12:58:40 Z2006-04-06T14:58:12Zhttp://social.msdn.microsoft.com/Forums/en-US/Vsexpressvc/thread/0f749fd8-8a43-4580-b54b-fbf964d68375#5844009e-8a3d-4060-b65f-476d68e0abe2http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvc/thread/0f749fd8-8a43-4580-b54b-fbf964d68375#5844009e-8a3d-4060-b65f-476d68e0abe2OShahhttp://social.msdn.microsoft.com/Profile/en-US/?user=OShahConvert std::string to LPCWSTR (best way in c++)<p><div class=quote><table width="85%"><tr><td class=txt4> <strong>FabioDeSantis wrote:</strong></td></tr><tr><td class=quoteTable><table width="100%"><tr><td width="100%" valign=top class=txt4></p> <p>How can I convert an std::string to a LPCWSTR? What is the best way to do it in C++?</p> <p></td></tr></table></td></tr></table></div></p> <p>Instead of using a std::string, use a std::wstring (also called a std::basic_string&lt;wchar_t&gt;).</p> <p>I get the feeling you want to pass a std::string type to a Win32 API. Those APIs don't take LPCWSTRs (or even LPCSTRs), they take a LPCTSTR (long pointer to a tchar-string). In this case, your question should have been: &quot;How do I convert a std::string to a LPCTSTR?&quot;</p> <p>Instead of using a std::string use a std::basic_string&lt;TCHAR&gt;.</p>Thu, 06 Apr 2006 14:11:01 Z2006-04-06T14:58:12Zhttp://social.msdn.microsoft.com/Forums/en-US/Vsexpressvc/thread/0f749fd8-8a43-4580-b54b-fbf964d68375#f59dfd52-580d-43a7-849f-9519d858a8e9http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvc/thread/0f749fd8-8a43-4580-b54b-fbf964d68375#f59dfd52-580d-43a7-849f-9519d858a8e9Andrew Revvohttp://social.msdn.microsoft.com/Profile/en-US/?user=Andrew%20RevvoConvert std::string to LPCWSTR (best way in c++)<P>std::wstring s2ws(const std::string&amp; s)<BR>{<BR>&nbsp;int len;<BR>&nbsp;int slength = (int)s.length() + 1;<BR>&nbsp;len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0); <BR>&nbsp;wchar_t* buf = new wchar_t[len];<BR>&nbsp;MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);<BR>&nbsp;std::wstring r(buf);<BR>&nbsp;delete[] buf;<BR>&nbsp;return r;<BR>}<BR></P> <P>std::string s;</P> <P>#ifdef UNICODE<BR>std::wstring stemp = s2ws(s); // Temporary buffer is required<BR>LPCWSTR&nbsp;result = stemp.c_str();<BR>#else<BR>LPCWSTR&nbsp;result = s.c_str();<BR>#endif</P>Sun, 23 Apr 2006 19:10:56 Z2006-04-23T19:10:56Zhttp://social.msdn.microsoft.com/Forums/en-US/Vsexpressvc/thread/0f749fd8-8a43-4580-b54b-fbf964d68375#ab7d7a63-35d0-4797-bef4-23444657f928http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvc/thread/0f749fd8-8a43-4580-b54b-fbf964d68375#ab7d7a63-35d0-4797-bef4-23444657f928FabioDeSantishttp://social.msdn.microsoft.com/Profile/en-US/?user=FabioDeSantisConvert std::string to LPCWSTR (best way in c++)<p>Perfect! Thank you so much!</p> <p>&quot;Ciao guagliò!&quot;</p>Mon, 24 Apr 2006 11:23:52 Z2006-04-24T11:23:52Zhttp://social.msdn.microsoft.com/Forums/en-US/Vsexpressvc/thread/0f749fd8-8a43-4580-b54b-fbf964d68375#a3412f4d-0cab-487b-9085-c41396da5357http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvc/thread/0f749fd8-8a43-4580-b54b-fbf964d68375#a3412f4d-0cab-487b-9085-c41396da5357OShahhttp://social.msdn.microsoft.com/Profile/en-US/?user=OShahConvert std::string to LPCWSTR (best way in c++)<p>Although you can use this function to convert a ANSI string to wide characters (or if you're comfortable with C++ iostreams:</p> <p> <hr id="[object]"> std::wstring ts2ws(const std::basic_string&lt;TCHAR&gt; &amp;s)<br>{<br>  /* exception safe */<br>  std::basic_ostringstream&lt;TCHAR&gt; buf;<br>  buf &lt;&lt; s;<br>  return buf.str();</p> <p>} <hr id="[object]"> </p> <p>), (or if you have the CRT wcstombs), (or if you're using the secure CRT wcstombs_s), (or if you're using ATL A2W), (or if you're using MFC CString), (or if you're using .NET PtrToStringChars), (or whatever the libraries your API provides), etc etc.</p> <p>IMO, you shouldn't need to make use of these conversions at all.</p> <p>Why?</p> <p>Ideally, your strings should all be of one datatype (either all char, or all wchar_t, or all TCHAR). This promotes consistency (a vital facet of reusable architectures). Use of these conversion macros are an indication of a flaw in your design. In the best case scenario, character conversion functions represent <a title="http://blogs.msdn.com/ricom/archive/2005/05/19/420158.aspx" href="http://blogs.msdn.com/ricom/archive/2005/05/19/420158.aspx">performance bottlenecks</a>. In the worst case scenario, they are the source of <a title="http://www.cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2006-1189" href="http://www.cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2006-1189">security bugs</a>.</p> <p>If you need to make use of a library that uses one of the other character datatypes, then either rewrite all your code to be the same as that datatype (eg. instead of using string, use wstring, or a &quot;tstring&quot;), or rewrite that library to fit with your datatype (replace char with wchar_t / TCHAR).</p> <p>If this means you have to rewrite your entire program from scratch, then rewrite your program from scratch (if you leave it till later, it will just get harder to port). If you don't perform the upgrade, you will leave your app slower and less secure than it can be.</p> <p>One of my pet peeves occurs when I need to make use a library that doesn't use wchar_t / TCHARs. I have to waste the rest of the day / week rewriting that library to use the wide character functions. Then I end up ditching that library, when I find out out that ignorance of unicode is the least of the problems plaguing the library.</p>Mon, 24 Apr 2006 12:18:44 Z2006-04-24T12:18:44Zhttp://social.msdn.microsoft.com/Forums/en-US/Vsexpressvc/thread/0f749fd8-8a43-4580-b54b-fbf964d68375#f2249eb7-5e97-45cf-b951-2bb29d67c5efhttp://social.msdn.microsoft.com/Forums/en-US/Vsexpressvc/thread/0f749fd8-8a43-4580-b54b-fbf964d68375#f2249eb7-5e97-45cf-b951-2bb29d67c5efAndrew Revvohttp://social.msdn.microsoft.com/Profile/en-US/?user=Andrew%20RevvoConvert std::string to LPCWSTR (best way in c++)<p>I recommend make new software, using std:wstring only, because all new Microsoft systems are UNICODE internally and there is only a small count of working Windows9x systems.</p> <p>Using of TCHAR is a poor design in Windows, unfortunately, because we should create two different exe files for unicode and ansi environment.</p> <p>Using of a std::wstring is simple. There is a fastest way to convert to it from Windows API functions or use it for Windows API calling.</p> <p>For compatible conversions use this code:</p> <p>std::string ws2s(const std::wstring&amp; s)<br>{<br> int len;<br> int slength = (int)s.length() + 1;<br> len = WideCharToMultiByte(CP_ACP, 0, s.c_str(), slength, 0, 0, 0, 0); <br> char* buf = new char[len];<br> WideCharToMultiByte(CP_ACP, 0, s.c_str(), slength, buf, len, 0, 0); <br> std::string r(buf);<br> delete[] buf;<br> return r;<br>}</p> <p>This function and s2ws() are checked for bugs. Only one problem may be available, if you will use these functions in expressions with c_str(). You should create a local variable in some cases, because C++ may call a string destructor and destroy string object before API calling, so this API function may get a pointer to a destructed memory.</p>Mon, 24 Apr 2006 14:55:58 Z2006-04-24T14:55:58Zhttp://social.msdn.microsoft.com/Forums/en-US/Vsexpressvc/thread/0f749fd8-8a43-4580-b54b-fbf964d68375#b74c969e-e21c-4a20-a801-47913d4271dbhttp://social.msdn.microsoft.com/Forums/en-US/Vsexpressvc/thread/0f749fd8-8a43-4580-b54b-fbf964d68375#b74c969e-e21c-4a20-a801-47913d4271dbOShahhttp://social.msdn.microsoft.com/Profile/en-US/?user=OShahConvert std::string to LPCWSTR (best way in c++)<p><div class=quote><table width="85%"><tr><td class=txt4> <strong>Andrew Revvo wrote:</strong></td></tr><tr><td class=quoteTable><table width="100%"><tr><td width="100%" valign=top class=txt4>I recommend make new software, using std:wstring only, because all new Microsoft systems are UNICODE internally</td></tr></table></td></tr></table></div></p> <p>The Windows API is TCHAR, and regardless of whether it uses unicode or ansi internally, the API is the only thing you access. Therefore, if you use the Windows API, you need to access it with TCHAR.</p> <p><div class=quote><table width="85%"><tr><td class=txt4> <strong>Andrew Revvo wrote:</strong></td></tr><tr><td class=quoteTable><table width="100%"><tr><td width="100%" valign=top class=txt4>and there is only a small count of working Windows9x systems.</td></tr></table></td></tr></table></div></p> <p>For the remaining Windows9x systems, you always have the Microsoft Layer for Unicode, which allows your unicode app to run on these systems.</p> <p><div class=quote><table width="85%"><tr><td class=txt4> <strong>Andrew Revvo wrote:</strong></td></tr><tr><td class=quoteTable><table width="100%"><tr><td width="100%" valign=top class=txt4>Using of TCHAR is a poor design in Windows, unfortunately, because we should create two different exe files for unicode and ansi environment.</td></tr></table></td></tr></table></div></p> <p>Although TCHAR's primary use is cited for developing an ansi and unicode EXE, its use extends to more than just that. You can use it to create libraries that work with both unicode and ansi projects with minimal pain. Whereas with ANSI apps, you have to rewrite the entire project to convert it to unicode, with TCHAR apps, you just need to define UNICODE.</p> <p>Having said that, DLLs should be developed Petzold-Windows style (ie. have both a W entry point and A entry point).</p> <p> <hr id="[object]"> std::string ws2s(const std::wstring&amp; s)<br>{<br> int slength = (int)s.length() + 1;<br> int len = WideCharToMultiByte(CP_ACP, 0, s.c_str(), slength, 0, 0, 0, 0); <br> char* buf = new char[len];<br> WideCharToMultiByte(CP_ACP, 0, s.c_str(), slength, buf, len, 0, 0);<br><font color="#ff0000"> std::string r(buf); </font><font color="#008000">// If this throws an exception, you'll leak memory. It will be slightly easier to use a std::vector instead, which does self checking.</font><br> delete[] buf;<br> return r;<br>} <hr id="[object]"> </p> <p> </p>Tue, 25 Apr 2006 12:17:24 Z2006-04-25T12:17:24Zhttp://social.msdn.microsoft.com/Forums/en-US/Vsexpressvc/thread/0f749fd8-8a43-4580-b54b-fbf964d68375#82da59e7-4cf9-44ea-9495-2fe21a25c6bchttp://social.msdn.microsoft.com/Forums/en-US/Vsexpressvc/thread/0f749fd8-8a43-4580-b54b-fbf964d68375#82da59e7-4cf9-44ea-9495-2fe21a25c6bcCaesiumhttp://social.msdn.microsoft.com/Profile/en-US/?user=CaesiumConvert std::string to LPCWSTR (best way in c++)<p align=left><font face=Arial size=2>Very helpful. In case this is any use, here's an example I developed (with help!) for a sort of reverse operation:</font></p><font color="#0000ff" size=2> <p>#define</font><font size=2> BUFSIZE MAX_PATH</p> <p>TCHAR Buffer[BUFSIZE];</p> <p>DWORD dwRet;</p> <p>dwRet = GetCurrentDirectory(BUFSIZE, Buffer);</p> <p>std:<img alt="Tongue Tied" src="http://forums.microsoft.com/MSDN/emoticons/emotion-7.gif">tring strcurpath;</p></font><font color="#0000ff" size=2> <p>#ifdef</font><font size=2> UNICODE</p> <p>size_t i;</p> <p></font><font color="#0000ff" size=2>char</font><font size=2> *pMBBuffer = (</font><font color="#0000ff" size=2>char</font><font size=2> *)malloc( BUFSIZE );</p></font><font size=2> <p>wcstombs_s(&amp;i, pMBBuffer, (size_t)BUFSIZE, Buffer, (size_t)BUFSIZE );</p> <p>strcurpath = pMBBuffer;</p></font><font color="#0000ff" size=2> <p>#else</p></font><font color="#808080" size=2> <p>strcurpath = Buffer;</p></font><font color="#0000ff" size=2> <p>#endif</p></font>Sat, 22 Mar 2008 15:59:02 Z2008-03-22T15:59:02Zhttp://social.msdn.microsoft.com/Forums/en-US/Vsexpressvc/thread/0f749fd8-8a43-4580-b54b-fbf964d68375#4efa2a81-2183-4081-81aa-91f908870981http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvc/thread/0f749fd8-8a43-4580-b54b-fbf964d68375#4efa2a81-2183-4081-81aa-91f908870981Vegan Fanatichttp://social.msdn.microsoft.com/Profile/en-US/?user=Vegan%20FanaticConvert std::string to LPCWSTR (best way in c++)at the end of the day std:<img alt="Tongue Tied" src="http://forums.microsoft.com/MSDN/emoticons/emotion-7.gif">tring will do the job, you can use UTF-8 encoding if you are in need of 3rd part language support. This simplifies things to a degree, to bad the standards group was unwilling to deal with the fact that not everyone speaks C++ fluently. <p align=left><font face=Arial size=2></font> </p>Sun, 23 Mar 2008 23:48:12 Z2008-03-23T23:48:12Zhttp://social.msdn.microsoft.com/Forums/en-US/Vsexpressvc/thread/0f749fd8-8a43-4580-b54b-fbf964d68375#5d7129ce-73a9-48cc-a818-92e1de4dee9dhttp://social.msdn.microsoft.com/Forums/en-US/Vsexpressvc/thread/0f749fd8-8a43-4580-b54b-fbf964d68375#5d7129ce-73a9-48cc-a818-92e1de4dee9dfloppy22http://social.msdn.microsoft.com/Profile/en-US/?user=floppy22Convert std::string to LPCWSTR (best way in c++)Perhaps a more simple way :<br/> <br/> std::wstring s2ws (const std::string&amp; s)<br/> {<br/>     std::wstring ws;<br/>     ws.assign (s.begin (), s.end ());<br/>     return ws;<br/> }<br/> <br/> ... and so <br/> <br/> std::string ws2s (const std::wstring&amp; ws)<br/> {<br/>     std::wstring s;<br/>     s.assign (ws.begin (), ws.end ());<br/>     return s;<br/> }<br/> <br/>Sat, 30 May 2009 21:14:20 Z2009-05-30T21:14:20Zhttp://social.msdn.microsoft.com/Forums/en-US/Vsexpressvc/thread/0f749fd8-8a43-4580-b54b-fbf964d68375#42de57f4-eb7b-40c8-a35f-7fedc858497ahttp://social.msdn.microsoft.com/Forums/en-US/Vsexpressvc/thread/0f749fd8-8a43-4580-b54b-fbf964d68375#42de57f4-eb7b-40c8-a35f-7fedc858497anobugzhttp://social.msdn.microsoft.com/Profile/en-US/?user=nobugzConvert std::string to LPCWSTR (best way in c++)It is simple but it is wrong.  You cannot ignore character encoding, you must use MultiByteToWideChar().<br/><hr class="sig">Hans Passant.Sun, 31 May 2009 12:03:05 Z2009-05-31T12:03:05Zhttp://social.msdn.microsoft.com/Forums/en-US/Vsexpressvc/thread/0f749fd8-8a43-4580-b54b-fbf964d68375#880beaed-7399-4a30-a196-983993c213fdhttp://social.msdn.microsoft.com/Forums/en-US/Vsexpressvc/thread/0f749fd8-8a43-4580-b54b-fbf964d68375#880beaed-7399-4a30-a196-983993c213fdAnadi Kumarhttp://social.msdn.microsoft.com/Profile/en-US/?user=Anadi%20KumarConvert std::string to LPCWSTR (best way in c++)MultiByteToWideChar<br/> <br/> Char * to Wide char ptr <br/> that is<br/> Char *  wchar_t *<br/> <br/> Code:<br/> int len = lenOfchPtr+1;<br/>                  wchar_t *wText = new wchar_t[len];<br/>                  memset(wText,0,len);<br/>                  MultiByteToWideChar(  CP_ACP, NULL,chPtr, -1, wText,len );<br/> <br/> <br/><hr class="sig">Anadi Kumar, Indo-Nepal Border Jogbani, Araria BIhar, PIN-854328Mon, 22 Jun 2009 10:58:59 Z2009-06-22T10:58:59Zhttp://social.msdn.microsoft.com/Forums/en-US/Vsexpressvc/thread/0f749fd8-8a43-4580-b54b-fbf964d68375#d095d183-297c-4722-b919-d04ddea8347ahttp://social.msdn.microsoft.com/Forums/en-US/Vsexpressvc/thread/0f749fd8-8a43-4580-b54b-fbf964d68375#d095d183-297c-4722-b919-d04ddea8347a0xDEAD BEEFhttp://social.msdn.microsoft.com/Profile/en-US/?user=0xDEAD%20BEEFConvert std::string to LPCWSTR (best way in c++)I suggest using TCHAR instead of wchar_t. It will be either char or wchar depending on UNICODE define.<br/><br/>please pay attention to \0 in string::format!!!<br/><br/><span style="font-size:x-small"><font size=2> <p> </p> </font></span> <p><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small">array</span></span><span style="font-size:x-small">&lt;TCHAR&gt;^ str = String::Format(</span><span style="color:#a31515;font-size:x-small"><span style="color:#a31515;font-size:x-small">&quot;&lt;YOUR STRING HERE&gt;\0&quot;</span></span><span style="font-size:x-small">, i)-&gt;ToCharArray();<br/></span><span style="font-size:x-small">TCHAR *tr = </span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small">new</span></span><span style="font-size:x-small"> TCHAR[str-&gt;Length];<br/></span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small">for</span></span><span style="font-size:x-small">(</span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small">int</span></span><span style="font-size:x-small"> n = 0; n &lt; str-&gt;Length; tr[n] = str[n], n++);<br/>handle = CreateFile(tr, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);<br/></span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small">delete</span></span><span style="font-size:x-small">[] tr;<br/><br/>Beef</span></p>Wed, 16 Sep 2009 10:25:45 Z2009-09-16T10:25:45Z