Windows core programming note 3-Process

Source: Internet
Author: User
Connect .......

2. Process
2. Create a process
Use the Process Creation function to create a process:

Bool CreateProcess (
Pctstr pszapplicationname,
Ptstr pszcommandline,
Psecurity_attributes psaprocess,
Psecurity_attributes psathread,
Bool binherithandles,
DWORD fdwcreate,
Pvoid pvenvironment,
Pctstr pszcurdir,
Pstartupinfo psistartinfo,
Pprocess_information ppiprocinfo );

Note the following points: (1) Calling the create process function must be completed in the thread of a process. (2) The system will create a kernel object for the process. (3) After a process is created, a main thread of the process is automatically created. Therefore, the system also creates a kernel object of the thread. (4) The system allocates the virtual address memory space for the process, and distributes executable files, corresponding DLL files, andCodeLoad it to the virtual memory space.

After the main thread of the process is created, the main thread starts to run. If a process is successfully created, CreateProcess returns true.

Parameters:
Pszapplicationname and pszcommandline indicate the names of executable files used by the new process and the command line Character Set passed to the new process. For example, open a file in Notepad:Tchar path []=Text ("Mytext.txt");
CreateProcess ("C: \ windows \ system32 \ notepad.exe", Path ,.....);

The following psecurity_attributes psaprocess and psecurity_attributes psathread set the security of the process and the main thread of the process. If you set the default security, set null. Binherithandles sets the inheritance of the kernel objects owned by the process. For example, if you create a process processb in processa and set binherithandles to true, processb has the ability to access processa's kernel objects. In processb, you only need to open * to open the corresponding process to call the corresponding kernel object.

The fdwcreate parameter is used to identify a flag to specify how to create a new process. For details, see msdn or Windows core programming.

Pvenvironment is used to set the environment parameters of a process. Each process has an environment block, which is a piece of memory allocated in the process address space. Each environment block has a string, which is similar to the environment variable in the system. Generally, null is set to indicate the environment parameters that inherit the parent process. You can use pvoid getenvironmentstrings () to obtain the address of the Environment string data block being used by the calling process.

Pszcurdir is used to set the drive and directory of the new process. If it is null, the working directory will be in the same directory as the process.

Psistartinfo records the information of the startupinfo structure. For most processes, you only need to use the default value. We only need to define the structure and then initialize and clear it:Startupinfo Si= {Sizeof(SI )}
CreateProcess (..&Si ,..)

For details about other parameters, see msdn or books.
Of course, inProgramTo obtain its value, such:

Startupinfo Si =   {Sizeof(SI )} ;
Getstartupinfo ( & Si );

The ppiprocinfo parameter is used to point to the process_information structure that you must specify. Before returning the result, CreateProcess initializes the members of the structure. The structure is shown below:

Typedef Struct _ Process_information
{
Handle hprocess;
Handle hthread;
DWORD dwprocessid;
DWORD dwthreadid;
} Process_information;

As described above, creating a new process allows the system to establish a process kernel object and a thread kernel object. When creating a process, the system assigns each object an initial count value of 1. Then, before CreateProcess returns, this function opens the process object and thread object, and puts the process-related handles of each object into the hprocess and hthread members in the process_information structure. When CreateProcess opens these objects internally, the Count of each object changes to 2.
Later, you can use the hprocess and hthread attributes of the variable to obtain the process or main thread handle.

3. Terminate the process
1. The return and exit (BEST) of the main thread)
2. Use the function exitprocess (not use)
3. Use the terminateprocess (not use) Function)
4. All threads in the process terminate the operation (impossibility)
Why is the first method the best, because: (1) the destructor of C ++ code can be correctly executed. (2) the system correctly releases the memory used by the thread stack. (3) The system will set the exit code of the process to the return value of the entry point function. (4) The Kernel Object of the process is reduced.
1.

If exitprocess is used to exit the process, the code after the function will not be executed. For example, if exitprocess is called in C ++ code, after many objects are created, the process is exited before being destructed, which is prone to memory leakage. In addition, resources used in the process are also leaked.
Usage: Use exitprocess (0) in the code to terminate the process.

The terminateprocess function can terminate a process, but the difference between it and exitprocess is that it can not only terminate its own process, but also terminate the running of other processes. The function prototype is as follows:

Bool terminateprocess (handle hprocess, uint fuexitcode );

It requires a process handle. To structure the process.

Terminateprocess is an asynchronous function. What is an asynchronous function? That is to say, when terminateprocess returns, we cannot guarantee that the process has been terminated. To determine whether a process stops running, call the waitforsingleobject function or similar function.

4. Create a sub-process
Why do we need to create a sub-process? It says:
To process complex tasks, we can also create a new thread in the process to execute such operations. However, all threads in the process share the address space of the process, this brings about a problem. The thread may change the content in the address space. We need to protect the content of the address space from being changed. Then, we can use the method of creating a new process to implement it.

Data transmission between Windows processes. The system provides many methods to transfer data in a Windows process. For example, Dynamic Data Exchange (DDE), Ole, mailbox, and pipeline. Among them, the most useful non-memory ing files are none.

This example uses waitforsingleobject to obtain the sub-process exit code. Process_information PI;
DWORD dwexitcode;

// Spawn the child process.
Boolfsuccess = CreateProcess (, π );

If (Fsuccess)
{
// Close the thread handle as soon as it is no longer needed!
Closehandle (PI. hthread );

// Suspend our execution until the child has terminated.
Waitforsingleobject (PI. hprocess, infinite );

// The child process terminated; get its exit code.
Getexitcodeprocess (PI. hprocess,
& Dwexitcode );

// Close the Process Handle as soon as it is no longer needed.
Closehandle (PI. hprocess );
}

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.