-
February 5th, 2014, 11:43 AM
#1
How to use ATL 7.0 string conversion macros?
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 the http://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?
-
February 6th, 2014, 07:34 AM
#2
Re: How to use ATL 7.0 string conversion macros?
Save the converted text in the CStringA and CStringW variables respectively.
Victor Nijegorodov
-
February 6th, 2014, 07:57 AM
#3
Re: How to use ATL 7.0 string conversion macros?
you need to keep the conversion objects "alive" for the duration that you need the converted value to be available.
so you need to scope the objects to ensure this is the case.
makes sense since the converted string is stored in the conversion object, and destroyed when the object is destroyed.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|