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 afx_stdafx_h0000a9db83db_a9fd_11d0_bfd000044455354000000000000ded _
# 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 it here
// Windows header files:
# Include <windows. h>
// C Runtime header files
# Include <stdlib. h>
# Include <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 termination sion of this module for _ usrdll
# Ifdef _ x86 _
Extern "C" {int _ afxforceusrdll ;}
# Else
Extern "C" {int _ afxforceusrdll ;}
# Endif