C++: convert LPTSTR to char array
Solution 1
LPTSTR is a (non-const) TCHAR string. Depends if it is Unicode or not it appears. LPTSTR is char* if not Unicode, or w_char* if so.
If you are using non-Unicode strings LPTSTR is just a char*, otherwise do:
size_t size = wcstombs(NULL, p, 0);
char* CharStr = new char[size + 1];
wcstombs( CharStr, p, size + 1 );
Also, this link can help:
Solution 2
First, you defined char* ch[MAX_PATH]
instead of char ch[MAX_PATH]
.
Regarding your question, LPTSTR (Long Pointer to TCHAR String) is equivalent to LPWSTR
(which is w_char*
) if it's unicode, or just LPSTR (char*
) if it is not. You can use this link for reference about conversion in each case.
EDIT: To cut to the chase, here's some code:
if (sizeof(TCHAR) == sizeof(char)) // String is non-unicode
strcpy(ch, (char*)(p));
else // String is unicode
wcstombs(ch, p, MAX_PATH);
EDIT 2: In windows I would recommend using TCHAR
instead of char
. It will save you some headache.
EDIT 3: As a side note, if you want to prevent Visual Studio from flooding you with warnings about unsafe functions, you can add something like the following to the very beginning of your code:
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
Comments
-
Oleksandr Fentsyk almost 2 years
Possible Duplicate:
Convert lptstr to char*I need to convert an
LPTSTR p
toCHAR ch[]
. I am new to C++.#include "stdafx.h" #define _WIN32_IE 0x500 #include <shlobj.h> #include <atlstr.h> #include <iostream> #include <Strsafe.h> using namespace std; int main(){ int a; string res; CString path; char ch[MAX_PATH]; LPTSTR p = path.GetBuffer(MAX_PATH); HRESULT hr = SHGetFolderPath(NULL,CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, p); /* some operation with P and CH */ if(SUCCEEDED(hr)) { /* succeeded */ cout << ch; } /* succeeded */ else { /* failed */ cout << "error"; } /* failed */ cin >> a; return 0; }
Thanks in advance.
-
Oleksandr Fentsyk almost 12 yearsI have Error 23 error C2664: 'wcstombs' : cannot convert parameter 2 from 'LPTSTR' to 'const wchar_t *'
-
Oleksandr Fentsyk almost 12 yearsCan you write some code?
-
Eitan T almost 12 years@Sasha the reference I gave you should have covered it, but sure. I added sample code to the answer.
-
Admin almost 12 yearsI think ATL conversion helpers make the code easier. Moreover, if you want to do a check for Unicode vs. ANSI/MBCS builds, I'd prefer checking with the preprocessor at compile-time with
UNICODE/_UNICODE
preprocessor macros (which are#define
'd in Unicode builds), instead of checking at run-time withif(sizeof(TCHAR) == sizeof(char)) ...
. -
Eitan T almost 12 years+1 for compile-time checking :)
-
Sergey K. almost 12 yearsYou have non-Unicode strings, you don't need to use wcstombs() in this case. Just use a simple cast to char*.
-
smerlin almost 12 yearsIts usually best to try if the secure template overloads (msdn.microsoft.com/en-us/library/ms175759%28v=vs.100%29.aspx) silence enough warnings, instead of disabling the warnings altogether. After all there is a reason why these warnings exist.
-
Ben over 11 years+1
LPTSTR is char* if not Unicode, or w_char* if so.
It was a surprisingly long time until I ran across this little bit of clarity. Quick, helpful summary of the same at kerwal.com/home/charwchartwchartcharomgwtftmi.