I have a problem in using ATL 7.0 string conversion macros.
My codes looks like this, which uses ATL 3.0 string conversion macros in the past:
Void Myfunc()
{
USES_CONVERSION;
LPSTR lpszA;
LPWSTR lpszW;
If (…) {
CString strText;
If (…) {
If (bUnicode)
{
lpszA = T2A((LPTSTR)(LPCTSTR)strText);
…
}
Else
{
lpszW = T2W((LPTSTR)(LPCTSTR)strText);
…
}
}
}
// codes using lpszA or lpszW based on the bUnicode flag
…
}
But since 3.0 macros do not support large strings, I want to switch to 7.0 macros, but have problems. Based on thehttp://msdn.microsoft.com/en-us/library/87zae4a3.aspx samples, I should declare CT2A pszA(strText) or CT2W pszW(strText) within the if and else bodies, as below:
Void Myfunc()
{
USES_CONVERSION;
LPSTR lpszA;
LPWSTR lpszW;
If (…) {
CString strText;
If (…) {
If (bUnicode)
{
CT2A pszA(strText);
lpszA = (LPSTR)pszA;
…
}
Else
{
CT2W pszW(strText);
lpszW = (LPSWSTR)pszW;
…
}
}
}
// codes using lpszA or lpszW based on the bUnicode flag
…
}
However, in such a case, after running to the codes using lpszA or lpszW, both CT2A and CT2W will be destructed so lpszA and lpszW will be invalid. How to solve this problem?