Discuss the C language runtime library included with Visual C
First part
Microsoft contains a set of C language runtime libraries in its Visual C products, which are mostly based on this set of libraries (such as MFC). In special occasions, we may need to use your own runtime library to replace it. For example, some games for the comprehensive performance of the system. At that time, we only need to implement some of the functions in the runtime library, or even naming according to the standard (because it is your own running library, and you don't plan to release her). For example, the memory allocation function of C language is often used, which is commonplace. Malloc, Calloc, Free, Realloc, there is no need to comply with the above name to name our corresponding functions.
What must be recognized before the run library is that many run libraries will not be reused, such as the (MFC) library just mentioned, and many libraries you have written before, this means you Maybe a white hand. (The ATL library must not be used in the C language runtime library, so you can continue to use, the premise is not to connect to the MFC).
1.1. Basic Concept
When we usually contact VC , the first contact, I am afraid WinMain and Main, corresponding to the Windows window system and console of the Win32 subsystem, and the most, the most books are talked about the corresponding multi-byte character set. Several variants. In fact, these entry point functions are entry points for the C runtime requirements with MSVC . The entry point function requested by the real MSVC connector is specified when compiling the compiler compiler of the MSVC . It can be a function that conforms to any name in the form of the following call:
Void __cdecl youRenTryProc (void);
If you like, you can play a more artist.
Speaking here, give a sample program better understanding this entry point function and what we usually contact C operations. This is a procedure nothing.
// vc Entry Point
Void MyEntry (Void);
{
}
Type these characters in a text file, save to: d: /test0.c
Then in the VC command prompt environment, type the following steps to compile, connect this program (in the previous version, I miss this part, which may make many people watching this article but I don't know how to implement):
l Find and run the 'Visual Studio .Net 2003 command line' menu item in the Microsoft Visual Studio .NET 2003 subkey in the Windows Start menu item:
l Enter the following instruction compile code
C: / Document and settings / MZ> D:
D: /> CL / C TEST0.C / NOLOGO
If there is no prompt and a command prompt appears, it means that the compilation is successful.
l Enter the following instructions to connect
D: /> LINK / Entry: "myentry" /out:test0.exe / subsystem: windows / nodefaultlib test0.obj / NOLOGO
If you don't do anything, there should be a TEST0.EXE executable file in the root of the D disk, double-click to run it and find anything. However, it is actually a unclosed Win32 application. You can use the corresponding tool to test it, you can find a few stacks, data transfer, and outlink instructions that match the C function call rules at the entry point.
Please refer to MSDN for some of the Some of the CL and LINK program switch options used. It is worth mentioning: Discussing the non-dynamic connection library, that is, a general executable image). Which is specifically used is the subsystem specified in the LINK command line. A more detailed description can be referred to with the MSDN.
1.2. Microsoft C / C Runtime Library
With these foundations, we went to see what Microsoft C / C Runtime Library was made at the entrance point. The code given here is screening, just to illustrate problems, these codes are not installed in CRT / SRC in the CRT / SRC in the VC installation directory.
#undef _Uncode
Void WinMainCrtStartup (Void)
{
Int mainret;
Startupinfo Startupinfo;
_SVER = GetVersion ();
_winminor = (OSVER >> 8) & 0x00FF;
_winmajor = _osver & 0x00ff;
_winver = (Winmajor << 8) _winminor;
Osver = (OSVER >> 16) & 0x00FFFF;
IF (! _heap_init (1))
FAST_ERROR_EXIT (_RT_HEAPINIT);
_ACMDLN = (char *) getcommandlinea ();
_AENVPTR = (char *) __CRTGETENVIRONMENTSTRINGSA ();
_setargv ();
_setenvp ();
_cinit ();
STARTUPINFO.DWFLAGS = 0;
GetStartupInfo (& Startupinfo);
MainRet = WinMain (getModuleHandlea (NULL),
NULL,
PSZCommandline,
Startupinfo.dwflags & startf_useshowwindows?
Startupinfo.wshowwindow: sw_showdefault;
exit (mainret);
}
#ENDIF
The above code is filtered, which is used in multithreading, and the ordinary multi-character set C runs. I explain a little explanation of the code, it completes the following tasks:
l Get version information of the operating system for future operations;
l then initialize the process stack;
l Get the command line, get and set environment variables;
Initialization of internal variables during runtime;
l Call the standard WIN32 window program entry point function (it should be defined and implemented in your application);
l Call the EXITPROCESS function to exit the application and exit the code is the return value of WinMain.
For specific code, see the source code for the running library.
1.3. Not using the run library to write their own application logic
Then, let's try, do not use the C run library, and make our apps do something. Please see the following code:
// Program INIT.C
#pragma overce
#include
Void Entry (Void)
{
Char ** P;
Char * palloc;
Char * psznames [] = {
"Snk",
"CAPCOM",
"Nintido",
"EA",
"3DO",
NULL
}
For (p = psznames; * p! = null; p )
{
MessageBox (0, * p, 0, mb_ok);
}
Palloc = Virtualalloc (0, 4096, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE);
IF (Palloc)
{
Const char * ptext = "Hello, World!";
Char * ptemp = (char *) PTEXT, * PSTR = Palloc;
For (; * ptemp! = '/ 0';) * PSTR = * Ptemp ;
* PSTR = * PTEMP;
Messagebox (0, Palloc, 0, MB_OK);
VirtualFree (Palloc, 4096, MEM_RELEASE);
}
}
Use the following command line to compile it
D: /> CL Init.c / c
D: /> link init.Obj / subsystem: windows /out:init.exe / entry: "entry" / nodefaultlib kernel32.lib user32.lib
The running interface of the generated init.exe program is as follows:
After printing some nouns, call the virtual memory allocation function in the basic service, assign and retain 4096 bytes, and then copy a string that everyone familiar with to it, then use the MessageBox function output screen in the window management function. The dialog is displayed. It connects the kernel32.lib library and the USER32.LIB library. There will be painful days without the Calence library, no malloc, copyword icon to be guilty. However, this is a clean and refreshing program without dragging water. Friends who have a budget that MFC dragging is asking for self-sufficiency, using this framework to write programs (-_-).
1.4. Use the integrated development environment to compile the above procedures
Every time you compile the above applications, you must type a lengthy order in the command prompt. To this end, I said to compile them in the VC integrated environment.
Use the Win32 Console Application item in the Win32 Console Application item to generate an empty project, then add the init.c file into it (don't say the method).
Open Engineering Settings dialog box: Delete all of the text boxes in the bottom of the C / C label except / NOLOGO / C. Delete everything in the Link Tag Bottom text box, add the contents of the above command line to in addition to init.obj. Change the output directory to the desired directory. Then you can compile it with F7.
MZ02005 Modified to 2007-4-27