Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As you seem to have gathered, <code>LPCTSTR</code> and <code>TCHAR</code> are basically defined as following (<code>LPCTSTR</code> would be read <em>long pointer to constant <code>TCHAR</code></em>):</p> <pre><code>#ifdef _UNICODE typedef wchar_t TCHAR; #else typedef char TCHAR; #endif // _UNICODE typedef const TCHAR* LPCTSTR; </code></pre> <p>So you can initialize a <code>LPCTSTR</code> variable as you would a <code>const wchar_t*</code> or a <code>const char*</code> variable, e.g. for unicode:</p> <pre><code>LPCTSTR lpszUnicode = L"Test String"; </code></pre> <p>And for ASCII/MultiByte:</p> <pre><code>LPCTSTR lpszMultibyte = "Test String"; </code></pre> <p>There are however, useful macros when working with the WinAPI: <code>_T("x")</code> and <code>TEXT("x")</code>, which both expand to <code>L"x"</code> (if your project is set for Unicode) or <code>"x"</code> (if your project properties are set for Multibyte). So for instance, you could use:</p> <pre><code>LPCTSTR lpszTest = _T("Test String"); LPCTSTR lpszTest2 = TEXT("Test String 2"); </code></pre> <p>And this will compile under either unicode or multibyte project settings. As for the reason that there are multiple macros expanding to the same thing, check out <a href="http://blogs.msdn.com/b/oldnewthing/archive/2004/02/12/71851.aspx" rel="noreferrer">this blog post</a>.</p> <hr> <p>You can also do what you are doing by dynamically allocating memory as you have done, so for instance:</p> <pre><code>LPTSTR lpszDynamic = new TCHAR[100]; // Do something with lpszDynamic delete[] lpszDynamic; </code></pre> <p>However, if you are finding yourself dynamically allocating memory often like this, it might be better to use string classes, such as <code>CString</code> or <code>std::string</code>/<code>std::wstring</code> (I often have the following in my MFC/WinAPI projects):</p> <pre><code>namespace std { #ifdef _UNICODE typedef wstring tstring; #else typedef string tstring; #endif // _UNICODE }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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