Reference cstring in a non-MFC Program

Source: Internet
Author: User

Reference cstring in a non-MFC Program

Cstring is still famous in today's software design field. It is the most widely used class in MFC. However, when using sdks to compile Windows programs, you cannot use the cstring class. You can only use the SDK Runtime Library, such as strlen, strcpy, and strstr. This article describes how to use the cstring class method in a non-MFC program, if you have a better implementation method, you are welcome to discuss it.
To use the cstring class, the source file must end with CPP. This is because VC uses different compilation methods and error checks for Different Extensions by default. MFC supports file afx. h. Only files with the extension CPP can be compiled normally. The EXE and library should adopt different methods. The following are discussed respectively:

1. In non-DLL or lib projects, it is very easy to use cstring, as long as two steps:

1. For <windows. h> stdafx. h, as long as it contains afx. h. h> stdafx. h. Ensure afx. h in windows. H is included before. In addition, because the default single-thread Runtime library used by the console program, we need to change it to a multi-threaded library. All this work is required in stdafx. h. the example of H is as follows (this is the stdafx of a Win32 GUI program generated from the wizard. h modified ):

// Stdafx. H # If! Defined (afx_stdafx_h1_a9db83db_a9fd_11d0_bfd1_444553541_1_encoded _) # define plugin _ # If _ msc_ver> 1000 # pragma once # endif // _ msc_ver> 1000 # define win32_lean_and_mean // exclude rarely-used stuff from Windows headers # include <afx. h> // Add here // windows header files: # include <windows. h> // C Runtime header files # include <stdlib. h> # inclu De <malloc. h> # include <memory. h> # include <tchar. h> # ifdef _ debug # pragma comment (Lib, "libcmtd. lib ") # else # pragma comment (Lib," libcmt. lib ") # endif // local header files # endif //! Defined (afx_stdafx_h1_a9db83db_a9fd_11d0_bfd1_444553541_1_encoded _)

In this way, the cstring class can be used normally in the program.

2. In the DLL or lib project, afx. h Includes a dllmain by default, which requires several steps to use the cstring class.

1. First, like the console program, if a single-threaded library is set in the compiling environment, you need to change it to a multi-threaded library, which can be modified from the project properties, for more information, see <MFC introduction>. the following is a common method. You can copy it directly to the project for use:

   #ifdef _DEBUG   #pragma comment(lib, "libcmtd.lib")   #else   #pragma comment(lib, "libcmt.lib")   #endif      

2. Create a dllmodul. cpp file under the project directory and add it to the current project.
3. Open the dllmodul. cpp file and edit the file as follows:

   #include "stdafx.h"   #ifdef _DEBUG   #undef THIS_FILE   static char THIS_FILE[] = __FILE__;   #endif      #define new DEBUG_NEW      /////////////////////////////////////////////////////////////////////////////   // global data      // The following symbol used to force inclusion of this module for _USRDLL   #ifdef _X86_   extern "C" { int _afxForceUSRDLL; }   #else   extern "C" { int __afxForceUSRDLL; }   #endif  

4. Open stdafx. h and include afx. h In front of windows. h. Now you can use cstring normally.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.