snipt

Ctrl+h for KB shortcuts

C++

Convert LPCWSTR to char string in VC++

1
2
3
4
5
6
7
#include <ole2.h> 
#define BUFFER_SIZE 100

size_t   i;
LPCWSTR strLPCWSTR = L"sometext";
char *strChar = new char[BUFFER_SIZE];
wcstombs_s(&i, strChar, (size_t)BUFFER_SIZE, strLPCWSTR, (size_t)BUFFER_SIZE);
https://snipt.net/embed/9b150d11d50e3dcc44ce471eb8058378/
/raw/9b150d11d50e3dcc44ce471eb8058378/
9b150d11d50e3dcc44ce471eb8058378
cpp
C++
8
2019-05-26T19:58:46
True
False
False
/api/public/snipt/50878/
convert-lpcwstr-to-char-string-in-vc
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="cp">#include</span> <span class="cpf">&lt;ole2.h&gt; </span><span class="cp"></span> </span><span id="L-2"><a name="L-2"></a><span class="cp">#define BUFFER_SIZE 100</span> </span><span id="L-3"><a name="L-3"></a> </span><span id="L-4"><a name="L-4"></a><span class="kt">size_t</span> <span class="n">i</span><span class="p">;</span> </span><span id="L-5"><a name="L-5"></a><span class="n">LPCWSTR</span> <span class="n">strLPCWSTR</span> <span class="o">=</span> <span class="s">L&quot;sometext&quot;</span><span class="p">;</span> </span><span id="L-6"><a name="L-6"></a><span class="kt">char</span> <span class="o">*</span><span class="n">strChar</span> <span class="o">=</span> <span class="k">new</span> <span class="kt">char</span><span class="p">[</span><span class="n">BUFFER_SIZE</span><span class="p">];</span> </span><span id="L-7"><a name="L-7"></a><span class="n">wcstombs_s</span><span class="p">(</span><span class="o">&amp;</span><span class="n">i</span><span class="p">,</span> <span class="n">strChar</span><span class="p">,</span> <span class="p">(</span><span class="kt">size_t</span><span class="p">)</span><span class="n">BUFFER_SIZE</span><span class="p">,</span> <span class="n">strLPCWSTR</span><span class="p">,</span> <span class="p">(</span><span class="kt">size_t</span><span class="p">)</span><span class="n">BUFFER_SIZE</span><span class="p">);</span> </span></pre></div> </td></tr></table>
C++