How Windows programs run internally

Source: Internet
Author: User
The windows program running process was reviewed due to the new access to VC ++. The following is a brief description of the Windows program running process:
The relationship between Windows applications, operating systems, and computer hardware:

1. About APIs
The Down Arrow ③ indicates that the application can notify the operating system to perform a specific action. For example, the operating system can control the sound card, but it does not know when to make the sound, the application needs to tell the operating system what sound to make. This relationship is like a robot capable of walking. However, if people do not tell the robot in which direction it is going, the robot will not walk on its own initiative. Here, robots are operating systems, and people are applications. So how does an application notify the operating system to execute a function? Readers who have programming experience should know that to complete a function in an application, all functions are implemented in the form of function calls. Similarly, applications also use function calls to notify the operating system to execute corresponding functions. Each special function that the operating system can accomplish usually corresponds to a function. That is to say, the operating system provides the function it can accomplish to the application in the form of a function, the call of these functions is called a system call. The collection of these functions is the application programming interface provided by the Windows operating system for application programming. For example, createwindow is an API function. When this function is called in an application, the operating system generates a window based on the parameter information provided by this function.
2. About messages and message queues
The up arrow ④ indicates that the operating system can upload the changes of the input device to the application. If a user presses the keyboard during a program activity, the operating system can immediately detect the event and know which key the user presses, the operating system does not decide how to respond to this event. Instead, it transfers the event to the application and the application determines how to respond to the event. For example, when a mosquito bites us, our nerve ends (equivalent to the operating system) immediately perceive this event and pass it to our brain (equivalent to applications ), our brain eventually decides how to respond to this event, such as driving mosquitoes away or killing them. The process of responding to an event is a message response.
How does the Operating System pass perceived events to applications? This is achieved through the message mechanism. The operating system packs each event into a message struct MSG to pass to the application. For more information, see msdn.
The MSG structure is defined as follows:
Typedef struct tagmsg {
Hwnd;
Uint message;
Wparam;
Lparam;
DWORD time;
Point pt;
} MSG;
3. Handle
Handle, the resource ID.
The operating system needs to manage and operate these resources by using handles to find the corresponding resources. By resource type, you can also subdivided the handle into the icon handle (hicon), the cursor handle (hcursor), the window handle (hwnd), and the application instance handle (hinstance) and other types of handles. The operating system specifies a unique identification number for each window, that is, the window handle.
4. Differentiate the usage of a variable from its type

Int X, Y;
X = 30;
Y = 30;
// X and Y can be used to represent coordinate points, width and height, and height and weight.

Typedef int width
Typedef int height
Width x;
Height y;
// Benefit: we can know from the type of the variable that X and Y are used to indicate the width and height.
5. winmain Function
Entry functions of Windows programs
Int winapi winmain (
Hinstance, // handle to current instance
Hinstance hprevinstance, // handle to previous instance
Lpstr lpcmdline, // command line
Int ncmdshow // show state
);
6. Create a form
To create a complete window, follow these four steps:
1. Design a window class;
2. Registration window class;
3. Create a window;
4. Display and update window.
Design form class
Typedef struct _ wndclass {
Uint style;
Wndproc lpfnwndproc;
Int cbclsextra;
Int cbwndextra;
Handle hinstance;
Hicon;
Hcursor;
Hbrush hbrbackground;
Lpctstr lpszmenuname;
Lpctstr lpszclassname;
} Wndclass;
7. Type of Form class
A Class of variables are often used in our programs. Each bit in this variable corresponds to a certain feature. When the value of a variable is 1, it indicates that there is a feature corresponding to this bit. When this bit is 0, there is no feature corresponding to this bit. When a certain digit in a variable is 1 at the same time, it indicates a combination of several features at the same time. Which of the variables represents the meaning and is not easy to remember. Therefore, we often define some macros Based on the uppercase spelling of the features, only the bit corresponding to the feature is 1 in the value corresponding to the macro, and the rest are 0. We can use goto definition to find cs_vredraw = 0x0001, cs_hredraw = 0x0002, cs_dblclks = 0x0008, cs_noclose = 0x0200. What they have in common is that only one digit is 1, and the other digits are 0. If we want the values of a variable to have both the cs_vredraw and cs_hredraw features, we only need to use the binary or (|) operator to combine them for or operations, for example, style = cs_vredraw | cs_hredraw | cs_noclose. If we want to remove one of the features from the original features of a variable, use the inverse (~) Then perform the (&) operation. For example, remove the cs_noclose feature on the basis of the style. You can use the Style &~ Cs_noclose implementation.
8. Form Process Functions
The second member variable lpfnwndproc specifies the process function for this type of window, also known as the callback function. The principle of the callback function is as follows. When an application receives a message to a window (Do you still remember that the message previously mentioned is usually related to the window ?), You should call a function to process the message. This call process is not implemented by the application itself, but by the operating system. However, the code of the callback function must be completed by the application itself. Which function (callback function) in the application is called by the operating system to process a message? The operating system calls the function specified by the lpfnwndproc member in the type of the window to which the message is received. Each type of window has its own dedicated callback function, which is specified by the lpfnwndproc member.
For example, a car manufacturer creates a window like an application. A user uses a car like an operating system management window. A car has designated a repair station before it is sold (similar to a callback function ), when a user's car fails (similar to the window to receive a message), the car user (similar to the operating system) directly finds the repair station for repair, without the manufacturer (similar applications) I personally sent the car to the repair station for repair, but the repair station had to be built by the manufacturer in advance.

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.