<p>I'm looking for a method, or a code snippet for converting std::string to LPCWSTR</p>

[{'answer_id': 27291, 'author': '17 of 26', 'author_id': 2284, 'author_profile': 'https://Stackoverflow.com/users/2284', 'pm_score': 4, 'selected': False, 'text': '<p>If you are in an ATL/MFC environment, You can use the ATL conversion macro:</p>\n\n<pre><code>#include &lt;atlbase.h&gt;\n#include &lt;atlconv.h&gt;\n\n. . .\n\nstring myStr("My string");\nCA2W unicodeStr(myStr);\n</code></pre>\n\n<p>You can then use unicodeStr as an LPCWSTR.  The memory for the unicode string is created on the stack and released then the destructor for unicodeStr executes.</p>\n'}, {'answer_id': 4139684, 'author': 'Benny Hilfiger', 'author_id': 502542, 'author_profile': 'https://Stackoverflow.com/users/502542', 'pm_score': 7, 'selected': False, 'text': "<p>The solution is actually a lot easier than any of the other suggestions:</p>\n<pre><code>std::wstring stemp = std::wstring(s.begin(), s.end());\nLPCWSTR sw = stemp.c_str();\n</code></pre>\n<p>Best of all, it's platform independent.</p>\n"}, {'answer_id': 64532484, 'author': 'Ashi', 'author_id': 592651, 'author_profile': 'https://Stackoverflow.com/users/592651', 'pm_score': 2, 'selected': False, 'text': '<p>I prefer using standard converters:</p>\n<pre><code>#include &lt;codecvt&gt;\n\nstd::string s = &quot;Hi&quot;;\nstd::wstring_convert&lt;std::codecvt_utf8_utf16&lt;wchar_t&gt;&gt; converter;\nstd::wstring wide = converter.from_bytes(s);\nLPCWSTR result = wide.c_str();\n</code></pre>\n<p>Please find more details in this answer: <a href="https://stackoverflow.com/a/18597384/592651">https://stackoverflow.com/a/18597384/592651</a></p>\n<hr />\n<p>Update 12/21/2020 : My answer was commented on by @Andreas H .  I thought his comment is valuable, so I updated my answer accordingly:</p>\n<blockquote>\n<ol>\n<li><code>codecvt_utf8_utf16</code> is deprecated in C++17.</li>\n<li>Also the code implies that source encoding is UTF-8 which it usually isn\'t.</li>\n<li>In C++20 there is a separate type std::u8string for UTF-8 because of that.</li>\n</ol>\n</blockquote>\n<p>But it worked for me because I am still using an old version of C++ and it happened that my source encoding was UTF-8 .</p>\n'}, {'answer_id': 66507740, 'author': 'Top-Master', 'author_id': 8740349, 'author_profile': 'https://Stackoverflow.com/users/8740349', 'pm_score': 1, 'selected': False, 'text': '<ul>\n<li>After knowing that, the C++11-standard has rules for <code>.c_str()</code> method (maybe <code>.data()</code> too), which allows us to use <code>const_cast</code>,</li>\n</ul>\n<blockquote>\n<p>(I mean, normally using <code>const_cast</code> may be dangerous)</p>\n</blockquote>\n<ul>\n<li>we can safely take &quot;<code>const</code>&quot; input parameter.</li>\n<li>Then finally, use &quot;<code>const_cast</code>&quot; instead of any unnecessary allocation and deletion.</li>\n</ul>\n<pre class="lang-cpp prettyprint-override"><code>std::wstring s2ws(const std::string &amp;s, bool isUtf8 = true)\n{\n    int len;\n    int slength = (int)s.length() + 1;\n    len = MultiByteToWideChar(isUtf8 ? CP_UTF8 : CP_ACP, 0, s.c_str(), slength, 0, 0);\n    std::wstring buf;\n    buf.resize(len);\n    MultiByteToWideChar(isUtf8 ? CP_UTF8 : CP_ACP, 0, s.c_str(), slength,\n           const_cast&lt;wchar_t *&gt;(buf.c_str()), len);\n    return buf;\n}\n\nstd::wstring wrapper = s2ws(u8&quot;My UTF8 string!&quot;);\nLPCWSTR result = wrapper.c_str();\n</code></pre>\n<blockquote>\n<p><strong>Note</strong> that we should use <code>CP_UTF8</code> for C++\'s string-literal, but in some cases you may need to instead use <code>CP_ACP</code> (by setting second parameter to <code>false</code>).</p>\n</blockquote>\n'}, {'answer_id': 67597861, 'author': 'Sabbir Pulak', 'author_id': 7134879, 'author_profile': 'https://Stackoverflow.com/users/7134879', 'pm_score': -1, 'selected': False, 'text': "<p>It's so easy, no need to apply any custom method. Try with this:</p>\n<pre><code>string s = &quot;So Easy Bro&quot;\nLPCWSTR wide_string;\n\nwide_string = CA2T(s.c_str());\n</code></pre>\n<p>I think, it will works.</p>\n"}, {'answer_id': 68465921, 'author': 'Ryan', 'author_id': 16493633, 'author_profile': 'https://Stackoverflow.com/users/16493633', 'pm_score': 1, 'selected': False, 'text': '<p>If you are using QT then you can convert to QString and then myqstring.toStdWString() will do the trick.</p>\n'}]
['https://Stackoverflow.com/questions/27220', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2701/']