The process simply provides an address space and a kernel object, which is represented by the main thread within its address space. When the entry point function of the main thread returns, the process ends. The way this process is terminated is the normal exit of the process, and all thread resources in the process can be properly purged. In addition to the normal exit mode of this process, it is sometimes necessary to force the end of the process or other process through code in the program. The prototype of the ExitProcess () function is:
void ExitProcess (UINT uexitcode);
Its parameter Uexitcode sets the exit code for the process. The function is mandatory and the process ends after execution, so any code that is behind it will not be executed. Although the ExitProcess () function can notify the dynamic-link library associated with it at the end of the process, the ExitProcess () function will have a security risk in use because of its enforcement. For example, if a space has been requested by the new operator before the program calls the ExitProcess () function, it will not be freed by the delete operator due to the mandatory exitprocess () function, resulting in a memory leak. Given the mandatory and unsafe nature of the exitprocess () function, it must be noticed when used.
2. End process with terminateprocess ()
ExitProcess () can only force the exit of this process, and if you want to force the end of another process in one process, it will be done with terminateprocess (). Unlike ExitProcess (), after the TerminateProcess () function executes, the terminated process is not notified of any program exits. In other words, the terminated process is not able to close before the end of the operation before exiting. Therefore, it is common to consider using terminateprocess () to force an end process only if no other method can force the process to exit. The following is a function prototype for terminateprocess ():
BOOL terminateprocess (HANDLE hprocess,uint uexitcode);
The parameters hprocess and Uexitcode are process handles and exit codes, respectively. If this is the end of the process, you can get to the handle by GetCurrentProcess (). TerminateProcess () is executed asynchronously, and after the call returns, it is not determined whether the terminated process has really exited, and if the process calling TerminateProcess () cares about this detail, it can be passed WaitForSingleObject () To wait for the real end of the process.
In the VC program How to end the system is running other processes (the process must have a window interface), in fact, very simple, as follows:
1. Get the handle to the process (using the FindWindow function);
2. Get the Process ID number (obtained with the GETWINDOWTHREADPROCESSID function);
3. Open the process, the first parameter in the OpenProcess function is set to Process_terminate, you can get a handle to handle the process;
4. Use the TerminateProcess function to end the process and set the second parameter of the function to 4.
The code is as follows:
End Process
int cstaticfunc::killprocess (LPCSTR pszclassname, LPCSTR
Pszwindowtitle)
{
HANDLE Hprocesshandle;
ULONG Nprocessid;
HWND Thewindow;
Thewindow =:: FindWindow (NULL, pszwindowtitle);
:: GetWindowThreadProcessId (Thewindow, &nprocessid);
Hprocesshandle =:: OpenProcess (Process_terminate, FALSE,
NPROCESSID);
Return:: TerminateProcess (Hprocesshandle, 4);
}
While the start-up process requires only the CreateProcess function, it is important to note that the function has several input parameters, the first parameter is
Start a new process
int cstaticfunc::createnewprocess (LPCSTR pszexename)
{
Process_information Piprocinfogps;
Startupinfo Sistartupinfo;
Security_attributes saprocess, Sathread;
ZeroMemory (&sistartupinfo, sizeof (Sistartupinfo));
SISTARTUPINFO.CB = sizeof (Sistartupinfo);
saprocess.nlength = sizeof (saprocess);
Saprocess.lpsecuritydescriptor = NULL;
Saprocess.binherithandle = true;
sathread.nlength = sizeof (Sathread);
Sathread.lpsecuritydescriptor = NULL;
Sathread.binherithandle = true;
Return:: CreateProcess (NULL, (LPTSTR) Pszexename, &saprocess,
&sathread, False,
Create_default_error_mode, NULL, NULL,
&sistartupinfo, &piprocinfogps);
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
End this process with ExitProcess (), terminateprocess () End Process