How to convert a string from CString to LPTSTR
-
I wonder how to convert a string from CString(MFC) to LPTSTR. I want to use my string in CString as a LPTSTR but i don't know to convert it.Please help me! Thanks a lot
In case you can do with a LPCTSTR (which in most cases is safer), you can easily convert a CString to a LPCTSTR, the CString class provides an operator
CString::operator LPCTSTR
to achieve that.CString MyCString;
LPCTSTR mystring=MyCString.operator LPCTSTR();To force the conversion to a non constant string, use the
GetBuffer
andReleaseBuffer
functions. ~RaGE(); -
I wonder how to convert a string from CString(MFC) to LPTSTR. I want to use my string in CString as a LPTSTR but i don't know to convert it.Please help me! Thanks a lot
-
CString already provides such an operator...
CString str = "Hello you";
char* pszTxt = (LPTSTR)str;
TOXCCT >>> GEII power
[toxcct][VisualCalc]Thanks for your answering, but when I try doing the following code LPTSTR CMainDlg::XuLyChuoi() { char *tmp_sz = new char[1000]; ::GetWindowText(GetDlgItem(IDC_EDIT1),tmp_sz,1000); CString s = tmp_sz; int index = 1; while ((index=s.Find("x", index)) != -1) { CString s1 = s[index-1]; s1+= s[index]; CString s2 = s[index-1]; s2+= "*"; s2+= s[index]; s.Replace(s1, s2); } tmp_sz = (LPTSTR)s; return tmp_sz; } I have the error: error C2440: 'type cast' : cannot convert from 'class WTL::CString' to 'char *' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called Error executing cl.exe. - 1 error(s), 0 warning(s) Can you explain?Thank you.
-
Thanks for your answering, but when I try doing the following code LPTSTR CMainDlg::XuLyChuoi() { char *tmp_sz = new char[1000]; ::GetWindowText(GetDlgItem(IDC_EDIT1),tmp_sz,1000); CString s = tmp_sz; int index = 1; while ((index=s.Find("x", index)) != -1) { CString s1 = s[index-1]; s1+= s[index]; CString s2 = s[index-1]; s2+= "*"; s2+= s[index]; s.Replace(s1, s2); } tmp_sz = (LPTSTR)s; return tmp_sz; } I have the error: error C2440: 'type cast' : cannot convert from 'class WTL::CString' to 'char *' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called Error executing cl.exe. - 1 error(s), 0 warning(s) Can you explain?Thank you.
-
put aroud your literal string some
_T()
because you seem to use UNICODE... and also declare tmp_sz as aTCHAR*
, not a char*
TOXCCT >>> GEII power
[toxcct][VisualCalc] -
Thanks for your answering, but when I try doing the following code LPTSTR CMainDlg::XuLyChuoi() { char *tmp_sz = new char[1000]; ::GetWindowText(GetDlgItem(IDC_EDIT1),tmp_sz,1000); CString s = tmp_sz; int index = 1; while ((index=s.Find("x", index)) != -1) { CString s1 = s[index-1]; s1+= s[index]; CString s2 = s[index-1]; s2+= "*"; s2+= s[index]; s.Replace(s1, s2); } tmp_sz = (LPTSTR)s; return tmp_sz; } I have the error: error C2440: 'type cast' : cannot convert from 'class WTL::CString' to 'char *' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called Error executing cl.exe. - 1 error(s), 0 warning(s) Can you explain?Thank you.
Not relating to the use of strings or conversion etc, but it looks to me like you have a chance of memory leakage here. You allocate memory for the string on the heap, then copy this into a CString. Later on you change the pointer to point to the LPTSTR of the CString (I think it should maybe be LPCTSTR, as casting a CString will probably only be safe if cast to a const - which may explain the error you're getting). This means that you lose the memory you allocated at the start of the function, as nothing's pointing to it any more. Also, you're returning a pointer to something which is about to be deleted (the CString will be deleted when you exit the function). I think what you actually should be doing (tho' feel free to correct me) is to copy the contents of the CString into the address pointed to by tmp_sz, and then return the tmp_sz pointer. Just remember that you need to later on delete the pointer that you're returning. So your last bit of code would do something like:
strcpy(tmp_sz, (LPCTSTR)s, s.GetLength());
return tmp_sz;After making sure that the length of CString s is not > 1000 (including the NULL terminator). Does that make sense? I'm speed-typing coz it's lunchtime and I'm hungry.
-
In case you can do with a LPCTSTR (which in most cases is safer), you can easily convert a CString to a LPCTSTR, the CString class provides an operator
CString::operator LPCTSTR
to achieve that.CString MyCString;
LPCTSTR mystring=MyCString.operator LPCTSTR();To force the conversion to a non constant string, use the
GetBuffer
andReleaseBuffer
functions. ~RaGE(); -
LPTSTR CMainDlg::XuLyChuoi() {
TCHAR* tmp_sz = new TCHAR[1000];
::GetWindowText(GetDlgItem(IDC_EDIT1), tmp_sz, 1000*sizeof(TCHAR));
CString s = tmp_sz;
int index = 1;
while ((index = s.Find(_T("x"), index)) != -1) {
CString s1 = s[index-1];
s1 += s[index];
CString s2 = s[index-1];
s2 += _T("*");
s2 += s[index];
s.Replace(s1, s2);
}
::_tcscpy(tmp_sz, (LPTSTR)s); // Notice that you cannot affect a C-style string with the operator = !!!!return tmp\_sz;
}
TOXCCT >>> GEII power
[toxcct][VisualCalc] -
I wonder how to convert a string from CString(MFC) to LPTSTR. I want to use my string in CString as a LPTSTR but i don't know to convert it.Please help me! Thanks a lot
cauvang wrote: convert a string from CString(MFC) to LPTSTR Are you certain that your CString is an MFC CString? I ask this because in the more detailed answer your gave toxcct here[^] looks like your using a CString defined in a namespace called 'WTL' which could indicate that it's all about the WTL version of CString you are using. I don't know anything about WTL so I won't be able to guide you there. Check this out. If you're not using the MFC version our answers won't make sense. Perhaps you even should post your question in the WTL forum. -- Roger
-
Not relating to the use of strings or conversion etc, but it looks to me like you have a chance of memory leakage here. You allocate memory for the string on the heap, then copy this into a CString. Later on you change the pointer to point to the LPTSTR of the CString (I think it should maybe be LPCTSTR, as casting a CString will probably only be safe if cast to a const - which may explain the error you're getting). This means that you lose the memory you allocated at the start of the function, as nothing's pointing to it any more. Also, you're returning a pointer to something which is about to be deleted (the CString will be deleted when you exit the function). I think what you actually should be doing (tho' feel free to correct me) is to copy the contents of the CString into the address pointed to by tmp_sz, and then return the tmp_sz pointer. Just remember that you need to later on delete the pointer that you're returning. So your last bit of code would do something like:
strcpy(tmp_sz, (LPCTSTR)s, s.GetLength());
return tmp_sz;After making sure that the length of CString s is not > 1000 (including the NULL terminator). Does that make sense? I'm speed-typing coz it's lunchtime and I'm hungry.
-
cauvang wrote: convert a string from CString(MFC) to LPTSTR Are you certain that your CString is an MFC CString? I ask this because in the more detailed answer your gave toxcct here[^] looks like your using a CString defined in a namespace called 'WTL' which could indicate that it's all about the WTL version of CString you are using. I don't know anything about WTL so I won't be able to guide you there. Check this out. If you're not using the MFC version our answers won't make sense. Perhaps you even should post your question in the WTL forum. -- Roger
I'm really sorry! I want to convert a string from CString(MFC) to LPCTSTR but the example is really stupid( it was written in WTL !!!). Now I wonder how to use a WTL class ( written in WTL,used WTL CString ) in a MFC application. The class is so important and i need it in my MFC project.However,it was written in WTL using WTL CString.Help me!!