LPTSTR
means TCHAR*
(expanding those Win32 acronyms typedefs can make it easier to understand them). TCHAR
expands to char
in ANSI/MBCS builds, and to wchar_t
in Unicode builds (which should be the default in these days for better internationalization support).
This table summarizes the TCHAR
expansions in ANSI/MBCS and Unicode builds:
| ANSI/MBCS | Unicode
--------+----------------+-----------------
TCHAR | char | wchar_t
LPTSTR | char* | wchar_t*
LPCTSTR | const char* | const wchar_t*
So, in ANSI/MBCS builds, LPTSTR
expands to char*
; in Unicode builds it expands to wchar_t*
.
char ch[MAX_PATH]
is an array of char
's in both ANSI and Unicode builds.
If you want to convert from a TCHAR
string (LPTSTR
) to an ANSI/MBCS string (char
-based), you can use ATL string conversion helpers, e.g.:
LPTSTR psz; // TCHAR* pointing to something valid
CT2A ch(psz); // convert from TCHAR string to char string
(Note also that in your original code you should call CString::ReleaseBuffer()
which is the symmetric of CString::GetBuffer()
.)
Sample code follows:
// Include ATL headers to use string conversion helpers
#include <atlbase.h>
#include <atlconv.h>
...
LPTSTR psz = path.GetBuffer(MAX_PATH);
HRESULT hr = SHGetFolderPath(NULL,CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, psz);
path.ReleaseBuffer();
if (FAILED(hr))
{
// handle error
...
}
// Convert from TCHAR string (CString path) to char string.
CT2A ch(path);
// Use ch...
cout << static_cast<const char*>(ch) << endl;
Note also that the conversion from Unicode to ANSI can be lossy.