What programmers should know about Windows API, CRT, and STL

Source: Internet
Author: User
Tags mathematical functions
1. Purpose of writing this article

This article aims to help some people understand some basic concepts about Windows API, C Runtime Library (CRT), and standard C ++ Library (STL. Many people, even experienced programmers, are vague or even have incorrect ideas about these concepts. If you want to know what they are based on and have no time to understand these concepts, please take some time to read this article.

2. Basic Concepts

The following figure shows the relationship between winapi, CRT, and STL.

Table 1: Relationship between Windows API, CRT, and C ++ standard Libraries

Adjacent modules can communicate with each other. Why? The following is a description from bottom to top based on this figure.

2.2. Hardware Layer

Each hardware part has its own command set through which the operating system controls the hardware and communicates with the hardware. The number and complexity of command sets vary greatly from hardware. Normally, if the same hardware is implemented by different hardware vendors, they usually provide some extensions out of the standard command set, and these extensions can usually take advantage of these hardware features. If every programmer rewrites the program against these hardware, and every hardware manufacturer has a lot, the coders will be exhausted! Therefore, God said that an operating system was required, so humans had a unified platform for convenient access to hardware.

2.3 Operating System

One of the purposes of the operating system is to encapsulate the features of the underlying hardware and then provide a unified interface for computer operators to control the hardware. The operating system of the modern version no longer allows applications to directly access the hardware. When the operating system can directly access the hardware layer, the operating system is in the kernel mode ).

Some older operating systems, such as MS-DOS, allow programmers to access the hardware directly. Although this can enable the program they write to apply some advanced features of the hardware in a short period of time, it is harmful to the program in the long run, because this program will not run on the new version of this hardware.

2.4 Application Programming Interface)

If modern applications want to access hardware, they must use the operating system, specifically the API provided by the operating system. An API is a unified interface for some functions. It abstracts and unifies these functions of hardware and enables programmers to concentrate on implementing the functions they want. It is no longer possible for applications to directly access hardware without passing through modern operating systems. A more common saying is that applications run in user mode. The APIS provided by MS windows are a collection of C functions. The minimum development language for Windows is C language.

2.4.1 platform software development kit)

Ms released a free platform software development kit (simply called Platform SDK or psdk) to encourage software developers to develop software on Windows platforms. Psdk includes the following:

1. header files containing the APIS

2. Corresponding library files used for connection (through which these API functions can be located in the corresponding DLL)

3. related instructions

4. Various help tools

For example, if you want to open or create a file, you need to call the createfile function, which is in the header file "WINBASE. H "declares that the function is located in the DLL file through the library file" kernel32.lib.

The function names of Windows APIs are named according to the camel naming method, so different functions are easy to distinguish. Constants and macros are generally named in uppercase. In the relevant documents, the header files, library files, and relevant platforms that can be run for each function are described in detail.

A Windows application can call any Windows API function, provided that the application can correctly find the function signature and link it to the corresponding library file, alternatively, you can use the getprocaddress function and function signature (usually the function name) to directly connect to its address in the dll library file.

2.5 C Runtime Library

Based on the API functions of the operating system, the software vendor implements the C Runtime Library (CRT ). CRT consists of some header files and corresponding source files, which implement some basic common operations, such as string operations, some mathematical functions and basic input/output operations. Generally, if a vendor releases a C compiler, it will include some CRT libraries. Some international standardization organizations are responsible for developing C language standards and implementing some runtime libraries.

2.5.1 standards and extensions

Theoretically, if a program is developed using standard C language and a platform supports its corresponding standard C compiler and dynamic library, then the program can run on this platform. But in fact, every C compiler development manufacturer will make some extensions for the C language, and programmers can easily use these extensions to develop programs, but the price is that the program is no longer movable.

The function names of CRT are generally in lower case, and the macros and constants are in upper case. Some extensions usually have the following dashes as the marker at the beginning, for example, function _ mkdir. For these extensions, the relevant documents will be described in detail.

2.6 Unicode Cognition

2.6.1 psdk supports Unicode

In fact, the names of the Win32 APIs mentioned above are not their real names. These names are just some macros. You can find the function names used by these macro pairs in the header file of the psdk. Therefore, if the psdk document mentions a function, such as createfile, developers should realize that it is just a macro. Its real names are createfilea and createfilew. Yes, it represents the "two" function names, rather than one. It is two different versions of the same function in different Win32 functions. The function ending with 'A' accepts an ANSI string, that is, a unicode string, that is, a wchar_ts string. Both versions of functions are implemented in the module kernel32.dll. If your programming environment is Unicode, the macro createfile will be replaced by createfilew in compilation; otherwise, it will be replaced by createfilea.

Windows operating system has three families: MS-DOS/9x-based, Windows CE, Windows NT.

1. MS-DOS/9x-based series, including windows 1.0-3.11, 95, 98 and Windows ME are given to MS-DOS operating systems. Earlier versions of Windows: 1.0-2.0 and 16-bit. In the subsequent operating systems, 3.0, and me are a mixture of 16-bit and 32-bit operating systems. They actually support a minimum operating environment of 16 bits and can also run some limited 32-bit applications. The limitation is that they only support Win32 functions of the ANSI version. The operating systems of the modern series have been extinct and Microsoft no longer maintains these systems.

2. the Windows NT series started from Windows NT 1990s in 3.1 and included Windows NT 4, Windows 2000, Windows XP, Windows Vista, and the server versions of these systems. The Windows NT series is a real 32-bit operating system. They support both the Win32 function of the Unicode version and the Win32 function of the ANSI version. The character strings in the operating system of the NT series are Unicode characters. The Win32 API functions of the ANSI Type are actually packaged by functions of the Unicode version.

3. Windows CE series are systems developed for mobile and embedded devices. They are 32-bit operating systems. Windows CE only supports Win32 APIs for Unicode.

2.6.2 psdk string solution: tchars

To avoid developing different versions of psdk for different Windows operating systems, Microsoft has developed a unified string type tchars. Tchar and other corresponding macros are defined in the header file winnt. h. Programmers do not need to tangle with Char or wchar_t in the program. They only need to use macro tchar. The compiler automatically performs the Conversion Based on whether the Unicode environment exists. In the same way, programmers do not need to worry about using the 'A' or 'W' Win32 API functions.

// Generic code


Lpctstr psz = text ("Hello world! ");

Tchar szdir [max_path] = {0 };

Getcurrentdirectory (max_path, szdir );

// If the Unicode symbol is not defined


Const char * psz = "Hello world! ";

Char szdir [max_path] = {0 };

Getcurrentdirectorya (max_path, szdir );

// Getcurrentdirectorya is only an external package on a 32-bit operating system. The actual workflow is as follows:

// 1. assign a temporary fixed-size wchar_t buffer.

// 2. Call the actual worker: getcurrentdirectoryw.

// 3. Based on the active code page of the calling thread, call the widechartomultibyte function to convert wchar_t to a char string.

// If a string cannot find the corresponding symbol, use the symbol '? .

// If macro Unicode is defined


Const wchar_t * psz = l "Hello world! ";

Wchar_t szdir [max_path] = {0 };

Getcurrentdirectoryw (max_path, szdir );

// Directly call the actual work. You do not need to find the contractor's intermediary first.

Using tchar allows programmers to write a piece of code for both ANSI and Unicode builds compiling environments. In today's era, you cannot write programs for the old Operating System Windows 9x/me. You can safely use Unicode strings. One advantage of this is that Unicode applications can forget the code pages hustle.

A simple method to remember the psdk string declaration is as follows:

L p c t STR = const tchar *



Long ------- + |

Pointer to --- + |

Constant ------- + |

Tchar ------------ + |

String ------------- +

Sometimes l (that is, "long") is ignored, because the difference between Long and Short pointers on the Win32 platform is outdated. Therefore, ptstr = "pointer to tchar string", that is, tchar *.

The following is the running result of the same program in two different runtime environments. The first is the running result in the ANSI environment, and the second is the running result in the Unicode environment.

The 20-century naive ANSI program converts all non-English characters '? '.

Modern Unicode programs can recognize strings in other languages.

2.6.3 CRT string solution: _ tchars

Psdk adopts a generic text conversion technology, which can realize text type conversion according to the specific CRT environment. CRT uses an additional header file "tchar. H" to implement this generic technology. To be compatible with the C language standard, all non-standard names must start with an underscore. Therefore, CRT uses macro _ t to replace the macro text () defined in the file "winnt. H (). To enable this library to be widely used, CRT developers can recognize three character sets:

* Sbcs-single-byte character set. The character type of this type of character set is Char. A char element stores an ASCII character. You do not have to define a character set for each individual project. This character set comes from the C language in the 1970s S. The 0x00-0x7f character set is reserved for English character sets, while the remaining 0x80-0xff is reserved for non-English character sets, the meaning of each character in the specific set is determined by the current active code page.

* _ MBCS-Multi-byte character set. The multi-byte string Character Set also depends on the char character type. A multi-byte character may occupy the space of one or two char elements. If you want to use a multi-byte character set, you must define macro _ MBCS in the project. Macro _ MBCS is backward compatible with sbcs mode, and versions earlier than MS Visual C ++ 8.0 (2005) use this character set by default. This character set is generally used for East Asian languages, such as Japanese, Korean, and Chinese. Now, the multi-byte character set has been replaced by the Unicode Character Set. In the past, on Windows 9x/me platform, only the multi-byte character set can process the East Asian language.

* _ Unicode-Unified encoding character set. Wchar_t character type since this character set. A Unicode Character occupies a wchar_t space. The wchar_t type on Windows generally occupies 16 bits, so it can use about 65535 different characters on Windows. This character set is used by default after MS Visual C ++ 8.0 (2005.

CRT uses the _ MBCS and _ Unicode macros to distinguish between the multi-Byte Character Set and the Unicode Character Set.

Digoal #2: The generic text mapping in CRT

// Generic code; names are not standard, hence the leading underscore.


_ Tchar message [2, 128] = _ T ("the time is :");

_ Tchar * Now = _ tasctime (& TM );

_ Tcscat (message, now );

_ Putts (Message );

// What happens if no symbol is defined at all (sbcs ).


Char message [128] = "the time is :";

Char * Now = asctime (& TM );

Strcat (message, now );

Puts (Message );

// What happens if _ MBCS symbol is defined (Multi-Byte Character Set );

// Non-standard names are with the leading underscore.


Char message [128] = "the time is :";

Char * Now = asctime (& TM );

_ Mbscat (message, now );

Puts (Message );

// What happens if _ Unicode symbol is defined (Unicode Character Set );

// Non-standard names are with the leading underscore.


Wchar_t message [128] = l "the time is :";

Wchar_t * Now = _ wasctime (& TM );

Wcscat (message, now );

_ Putws (Message );

2.7 C ++ standard library

The C ++ programming language has its own set of standard libraries. These libraries contain a series of classes and functions that can be used in programming.

Generally, when people mention the C ++ standard library, they subconsciously think of STL directly. STL is the abbreviation of standard template library. The latest version of STL is released as a subset of the C ++ standard library. But in reality, STL is everywhere, and has become a synonym for the c ++ standard library.

The International Organization for Standardization (IOS) is responsible for defining the standards of the C ++ language and its standard library.

2.7.1 C ++ standard library content

The C ++ standard library can be divided into the following parts:

1. container. It is usually a common data structure, such as vector, set, list, map.

2. iterator. Provides a unified method for accessing standard containers.

3. algorithm. Some common algorithms are implemented. These algorithms generally access the container through the iterator instead of directly accessing the container. Therefore, an algorithm can act on different containers.

4. distributor. Allocates and recycles the memory space of each element in the container.

5. Function objects and utilities. They facilitate algorithms to act on various containers.

6. streams. The input/output stream is treated as a unified object.

7. c dynamic Runtime Library. To be backward compatible with C, CRT should appear as part of the C ++ standard library.

2.8 progress on multiple platforms

Sometimes objective requirements require a software to run on multiple computer platforms. Programmers may need to develop specific software versions for a specific platform. This method is not only outdated, but also prone to errors. Because the same function module may require many different platforms to implement different versions, it is a waste of time and development resources.

Therefore, the general method is that the platform on which the software is written can run on different platforms. It cannot call the API functions of a specific platform, in addition, the extension of the standard library provided by the developer cannot be used. This makes development very difficult, but in the long run, all platforms can benefit from new software features and corrected bugs.

3. reuse code

There are two ways to embed the code of the CRT and C ++ libraries into a project: static link and dynamic link. The following two methods are discussed only for CRT, but these concepts are synonymous or similar in the C ++ standard library.

3.1 static Link

If the CRT/C ++ library is statically linked, the code of these libraries will also be loaded into the memory when the program starts. The advantages and disadvantages of this method coexist.


1. Easy to use. In this way, it is easy to copy the program to the target computer and run it. Don't worry about the complex use steps of CRT/C ++.

2. No additional files are required. In this way, some small applications can easily put these libraries into an executable file. This type of program can be easily downloaded from the Internet without worrying about its integrity being damaged.


1. Not durable. Programs implemented through static links cannot differentiate the version of a library, that is, the old version and the new version are the same for it.

2. Static links can easily lead to domino effect. Currently, a program cannot be implemented by an organization. Modern software projects are already very complex and begin to rely heavily on third-party components and libraries. Therefore, a software is generally divided into several modules with very loose coupling relationships. Static links to a part of the CRT will seriously affect the interoperability between these modules, which will also force programmers to rely on the largest public part between these modules. The following sections detail the relevant details.

3.1.1 use CRT as a black box

One problem is that it is difficult for different CRT instances to share a built-in CRT object. The memory allocated by a CRT instance must be released by this instance, the files opened by a CRT instance can only be operated and closed by this instance. This is because the CRT can only perform internal operations on the obtained resources. If one CRT instance wants to release the memory of another CRT instance, accessing the files opened by another instance through the file * pointer will change the visitor's status and even cause the program to crash.

So programmers who use CRT statically generally need to provide additional functions to release the resources used by the modules they develop, in addition, those who call this module must remember to use these functions to release these resources. Otherwise, memory leakage will occur. If different modules are statically connected to CRT, these modules cannot share STL container objects or C ++ objects. The following table describes how to use a memory cache by calling the malloc function.

In the preceding table, Module 1 is statically connected to the CRT, while module 2 and 3 are dynamically connected to the CRT. The objects used by the CRT can be transmitted between module 2 and Module 3. For example, Module 3 can release a portion of memory space allocated by malloc by Module 2, because calls to malloc and free are implemented by the same CRT object.

However, it is difficult to release Module 1 resources from other modules. Each resource occupied by Module 1 should be released by itself. Because Module 1 is statically linked to an instance of CRT. In the preceding figure, Module 2 must call the function provided by Module 1 to release the memory space it obtains through Module 1.

3.2 Dynamic Link

If you use dynamic links to link to the CRT/C ++ library, you only need to link a small portion of the imported data to the program when running the program. These imported functions contain the specific implementation address of the corresponding functions of CRT/C ++. When the program starts, the program loads some related DLL into the program's memory space according to the instructions of these commands.


1. Easy to implement modularization. As mentioned above, a program can be modularized easily through dynamic links. A program can be easily divided into several modules, and each module can easily share more advanced object data.

2. Faster startup. The CRT dll has been loaded when the system starts, so when the program using these DLL starts, it does not need to load these DLL, and this not only saves the memory space, it saves the page switching time.


1. Deployment is complicated. These CRT libraries must be re-allocated and placed in memory in order for a program to work. This requires an additional startup project, and you should be careful when deploying it.

4. Summary

This section describes the relationship and dependency sequence between Windows APIs, CRT, and STL. For user-state programs, Windows API is the lowest level for computers that can be used. The dynamic Runtime Library of C is on top of the Windows API. It encapsulates the operating system and hides the differences between different operating systems. The standard C ++ Library provides more functions and uses CRT as part of it. A cross-platform program can be written using standard functions and related classes. This program only needs to be re-compiled and released on the new platform without any changes to the Code.

Based on the objective needs of the application, it can link the static active state to the C Runtime Library and the Standard C ++ library. Each method has its own advantages and disadvantages.

Is: http://www.codeproject.com/KB/cp... aspx? Display = Mobile

The first few paragraphs of the article are excerpted: 1. the purpose

The purpose of this article is to clear the essential points about the Windows API, the C Runtime Library (CRT), and the Standard C ++ Library (STL ). it is not uncommon that even experienced developers have confusion and hold onto misconceptions about the relationship
Between these parts. If you ever wondered what is implemented on top of what and never had a time to figure it out, then keep reading.2. Basics

The following dimo-represents the relationship between winapi, CRT, and STL.

Original article: http://www.cnblogs.com/menggu?*yuan/archive/2011/06/09/2075910.html
Related Article

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: info-contact@alibabacloud.com 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.