From the previous articles, we have come to some conclusions.
The program can customize the entry, but if some CRT-related functions are used in the entry and Initialization is not performed correctly, even if the compilation fails, memory Errors may also occur during running.
Can we write programs that do not need to go to the CRT library? Of course. Since the CRT library is also written by Microsoft, we can even use another CRT library to replace it. But this is another topic. Let's take a look at the hello world that does not use the CRT library.
Test code of the C Runtime Library is not used
View sourceprint? 01 # include <windows. h>
02
03 DWORD StringLen (const char * str)
04 {
05 const char * eos = str;
06 while (* eos ++ );
07 return (DWORD) (eos-str-1 ));
08}
09
10 void WriteString (const char * str)
11 {
12 HANDLE hOut;
13 DWORD Length;
14 DWORD result;
15 hOut = GetStdHandle (STD_OUTPUT_HANDLE );
16 Length = StringLen (str );
17 WriteFile (hOut, str, Length, & result, 0 );
18}
19
20 int myentry (void)
21 {
22 WriteString ("Hello World! ");
23 ExitProcess (0 );
24 return 0;
25}
Compile:
Cl/c/Zl myentrytest. c
Link:
Link/entry: myentry/subsystem: console myentrytest. obj kernel32.lib
The ultimate executable file is myentrytest.exe.
The execution effect is the same as that of printf, but the principle used is quite different.
Here, we have access to three functions, which can be directly used without using the CRT library and without causing any problems. These three functions are called Windows APIs. These three APIs are provided by kernel32. By directly calling windows APIs, you can use the functions of the operating system and bypass the CRT library. Although we do not usually use such a technique, it is also possible to have such requirements when we come into contact with such code or self-coding. Of course, to completely bypass the CRT library, you need to extract all the functions provided by the CRT library used by the program and use windows APIs to replace them, the time it takes is no different from the time it takes to repeat the wheel. However, if you really want to have a certain understanding of the system, it makes some sense.
Here we will introduce the windows API:
Windows operating system application interface (Windows API), which is short for WinAPI, is Microsoft's name for the Core Application Programming Interface available in Windows operating system. It is designed to be called by programs in various languages and is also the most direct interaction between applications and Windows systems. Most drivers require a lower-level access interface to the Windows system, which is provided by the Native API of the Windows version used.
Windows has a software development kit (SDK, software development kit) that provides relevant documentation and tools for programmers to develop and use Windows API software and use Windows technology.
The functions provided by Windows APIs can be categorized into seven categories: [4]
Base Services [5] provides access interfaces for basic resources available in Windows. For example, file system, external device, process, thread, and Windows registry) and error handling ). These functional interfaces are located in the kernel.exe1_krnl286.exeor krnl386.exe system files under Windows 16; and In kernel32.dll and advapi32.dll under Windows 32-bit.
Graphic device interface (GDI) [6] provides the function to output the graphic content to the display, printer, and other external output devices. Gdi.exe located in 16-bit Windows and gdi32.dll in 32-bit Windows.
GUI [7] provides functions to create and manage screens and most basic controls, such as buttons and scroll bars. Receives mouse and keyboard input, and other GUI-related functions. These call interfaces are located at user.exe under 16-bit Windows and user32.dll under 32-bit Windows. After Windows XP, call interfaces of basic controls and Common Control Library are placed in comctl32.dll.
The Common Dialog Box Library [8] provides a standard Dialog Box for applications, such as the open/save file Dialog Box, color-to-image Box, and font Dialog Box. This link library is located in: commdlg. dll under 16-bit Windows, and comdlg32.dll under 32-bit Windows. It is classified under the User Interface API.
Common Control Library [9] provides interfaces for applications to access some advanced controls provided by the operating system. Such as status bar, progress bar, toolbar, and tab ). The Link Library is located in commctrl. dll under 16-bit Windows and comctl32.dll under 32-bit Windows .. It is classified under the User Interface API.
Windows Shell [10] [11], as an integral part of Windows APIs, not only allows applications to access the functions provided by the operating system shell, but also improves and enhances it. It is located in shell. dll under 16-bit Windows and shell32.dll under 32-bit Windows (Windows 95 is in shlwapi. dll ). It is classified under the User Interface API.
Network Services [12] provides interfaces for accessing multiple Network functions provided by the operating system. It includes NetBIOS, Winsock, NetDDE, and RPC.