Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

initializing a LPCTSTR /LPCWSTR [duplicate]

Tags:

c++

winapi

I am having crazy difficulty understanding and getting this to work properly. Basically what it comes down to is that I cannot successfully initialize a variable of this type. It needs to have the contents of say 7&2E25DC9D&0&USB003.

Can anybody please explain/show a proper initialization of this type w/ a similar value? I have looked at all the help on this site and changing my project properties from unicode to multibyte is not solving the issue, and the other solutions aren't working for me either.

As my code sits, here is how I am starting the initialization:

LPCTSTR portvalue = new TCHAR[100];
like image 986
N3rdB0mber Avatar asked Jul 19 '13 19:07

N3rdB0mber


People also ask

How do you initialize Lpcwstr?

So for instance, you could use: LPCTSTR lpszTest = _T("Test String"); LPCTSTR lpszTest2 = TEXT("Test String 2"); And this will compile under either unicode or multibyte project settings.

How is Lpcwstr defined?

An LPCWSTR is a 32-bit pointer to a constant string of 16-bit Unicode characters, which MAY be null-terminated.

How do you convert Lpcwstr to Lpwstr?

LPCWSTR is a pointer to a const string buffer. LPWSTR is a pointer to a non-const string buffer. Just create a new array of wchar_t and copy the contents of the LPCWSTR to it and use it in the function taking a LPWSTR.

What is Lpctstr in C++ MSDN?

LPCTSTR is a pointer to a const TCHAR string, ( TCHAR being either a wide char or char depending on whether UNICODE is defined in your project) LPTSTR is a pointer to a (non-const) TCHAR string.


2 Answers

As you seem to have gathered, LPCTSTR and TCHAR are basically defined as following (LPCTSTR would be read long pointer to constant TCHAR):

#ifdef _UNICODE
typedef wchar_t TCHAR;
#else
typedef char TCHAR;
#endif // _UNICODE

typedef const TCHAR* LPCTSTR;

So you can initialize a LPCTSTR variable as you would a const wchar_t* or a const char* variable, e.g. for unicode:

LPCTSTR lpszUnicode = L"Test String";

And for ASCII/MultiByte:

LPCTSTR lpszMultibyte = "Test String";

There are however, useful macros when working with the WinAPI: _T("x") and TEXT("x"), which both expand to L"x" (if your project is set for Unicode) or "x" (if your project properties are set for Multibyte). So for instance, you could use:

LPCTSTR lpszTest = _T("Test String");
LPCTSTR lpszTest2 = TEXT("Test String 2");

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 this blog post.


You can also do what you are doing by dynamically allocating memory as you have done, so for instance:

LPTSTR lpszDynamic = new TCHAR[100];
// Do something with lpszDynamic
delete[] lpszDynamic;

However, if you are finding yourself dynamically allocating memory often like this, it might be better to use string classes, such as CString or std::string/std::wstring (I often have the following in my MFC/WinAPI projects):

namespace std {
#ifdef _UNICODE
typedef wstring tstring;
#else
typedef string tstring;
#endif // _UNICODE
};
like image 123
Thomas Russell Avatar answered Sep 23 '22 20:09

Thomas Russell


From this answer, you have to prefix the literal with L

LPCWSTR a = L"TestWindow";

As Frederic pointed out, you can initialize a LPCTSTR in this manner:

LPCTSTR s = _T("TestWindow");
like image 31
Cory Klein Avatar answered Sep 22 '22 20:09

Cory Klein