CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    MFC: How to pass CString to Windows API functions?

    Q: Many Windows API functions require LPCTSTR (pointer to a constant string) or LPTSTR (pointer to a modifiable string buffer) as parameters. How to pass CString type to these functions?

    A:
    • To get a pointer to const buffer (LPCTSTR) we can use CString::GetString or CString::opertor LPCTSTR.
    • To get a pointer to modifiable buffer (LPTSTR) we can use CString::GetBuffer or CString::GetBufferSetLength.


    Examples
    1. Code:
         // GlobalAddAtom gets LPCTSTR
         CString strAtom = _T("demo atom text");
         // ...
         ATOM atom = ::GlobalAddAtom(strAtom.GetString());
    2. Code:
         // RegisterWindowMessage gets LPCTSTR
         CString strMsg = _T("demo message text");
         // ...
         UINT uMsg = ::RegisterWindowMessage((LPCTSTR)strMsg);
    3. Code:
         // CreateDirectory gets LPCTSTR
         CString strDir = _T("demo directory"); 
         // ...
         BOOL bRet = ::CreateDirectory(strDir, NULL);
    4. Code:
         // GetCurrentDirectory gets LPTSTR
         CString strCurDir;
         ::GetCurrentDirectory(MAX_PATH, strCurDir.GetBuffer(MAX_PATH));
         strCurDir.ReleaseBuffer();


    Notes
    • If need LPCTSTR, it's not absolutely necessary an explicit cast (see example #3 vs. #2).
    • After a call of GetBuffer, don't forget to call ReleaseBuffer (see example #4).
    • Important: an often made mistake is to use double cast (LPTSTR)(LPCTSTR). This is not OK and can lead in troubles:
      Code:
         // GetSystemDirectory gets LPTSTR
         CString strSysDir;
         ::GetSystemDirectory((LPTSTR)(LPCTSTR)strSysDir, MAX_PATH);
         // ...
         // Bang!!! The program can crash, sooner or later.
      So, never do that! Call GetBuffer instead, as shown in example #4.
    • CString::GetString is not available in MFC6.0 and older. Use opertor LPCTSTR which at last does the same.
    • In newer MFC versions, CString is implemented using templates, then you can find the methods and operators docummented at CStringT and CSimpleStringT. Also, you can find operator PCXSTR instead of LPCTSTR.
      Anyway, you can still use the "old syntax", as shown in the above examples.


    Resources [MSDN]
    Last edited by ovidiucucu; December 6th, 2013 at 10:03 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  2. #2
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: MFC: How to pass CString to Windows API functions?

    Instead of CString::GetBuffer or CString::GetBufferSetLength plus CString::ReleaseBuffer, we can use shared MFC/ATL CStrBuf class.
    That way, the above example #4 can be re-written a little bit simpler, as follows:

    Code:
        CString strCurDir;
        ::GetCurrentDirectory(MAX_PATH, CStrBuf(strCurDir, MAX_PATH));
    Resources
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

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
  •  





Click Here to Expand Forum to Full Width

Featured