CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443

    C++ String: How to use 'CString' in non-MFC applications?

    Users of Visual Studio 2003 and newer may directly go to the next post#2 and skip this one.

    - - -

    Q:
    How to use 'CString' in non-MFC applications?

    A: In most cases, you don't need to do that. In order to use 'CString' you have to statically or dynamically link your application to the entire MFC. This would not only increase the size of your executable file, the number of its dependencies, but also makes your program non-portable (especially if it is a Console application).

    The recommended solution is to use the Standard C++ Class 'std::string'. It is as powerful as 'CString', is portable, using it does not imply adding a huge amount of things you don't need to your project and last, but not least, it is part of the programming language.

    This being said, if you still want to use 'CString' in your non-MFC application, here it is whar you have to do:
    • Include 'afx.h' in one of your main headers
    • Open the menu 'Project -> Settings'. On the 'General' register of the settings dialog box choose 'Use MFC in a Shared DLL' or 'Use MFC in a Static Library' from the dropdown box called 'Microsoft Foundation Classes'.
    • Rebuild your project.
    A simple sample of a console application using 'CString' looks like this:

    Code:
    #include <afx.h>
    #include <iostream>
    
    int main()
    {
      CString s("Hello");
      std::cout << s.GetBuffer(0) << std::endl;
      return 0;
    }
    Last edited by JeffB; June 17th, 2009 at 02:35 PM. Reason: Fixed include using html characters

  2. #2
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: C++ String: How to use 'CString' in non-MFC applications?

    Starting VS 2003, you can use CString in non-MFC applications by including header atlstr.h:
    Code:
    #include <atlstr.h>
    A sample console application with CString:
    Code:
    #include <atlstr.h>
    #include <iostream>
    
    int main ()
    {
    CString strTest (_T("This is a CString in a console application!"));
     
    #ifdef UNICODE
       std::wcout << (LPCTSTR)strTest;
    #else
       std::cout << (LPCTSTR)strTest;
    #endif
     
    return 0;
    }
    You can also use CStringA as a ANSI string class, and CStringW as a wide-character string class.
    Last edited by JeffB; June 17th, 2009 at 02:50 PM. Reason: Fixed include using html characters

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