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];
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.
An LPCWSTR is a 32-bit pointer to a constant string of 16-bit Unicode characters, which MAY be null-terminated.
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.
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.
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
};
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With