How to use ATL 7.0 string conversion macros?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: How to use ATL 7.0 string conversion macros?

  1. #1
    Join Date
    Nov 2010
    Posts
    49

    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?

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,524

    Re: How to use ATL 7.0 string conversion macros?

    Save the converted text in the CStringA and CStringW variables respectively.
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,056

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center