web123456

c/c++ method to end the process

1. Use ExitProcess() to end the process
The process only provides a piece of address space and kernel objects, and its runtime is reflected by the main thread in its address space. When the entry point function of the main thread returns, the process ends. This way of terminating the process is the normal exit of the process, and all thread resources in the process can be correctly cleared. In addition to the normal exit method of this process, sometimes it is necessary to force the operation of this or other processes through code in the program.

The prototype of the ExitProcess() function is: void ExitProcess(UINT uExitCode);

Its parameter uExitCode sets an exit code for the process.ExitProcess() can only enforce the exit of this process., This function is mandatory, and the process is terminated after execution, so any code located thereafter will not be executed. Although the ExitProcess() function can notify the associated dynamic link library while ending the process, due to the mandatory nature of its execution, the ExitProcess() function will have security risks in use. For example, if a space is applied for using the new operator before the program calls the ExitProcess() function, it will not be able to be released through the delete operator due to the mandatory nature of the ExitProcess() function, resulting in memory leakage. In view of the mandatory and insecurity of the ExitProcess() function, you must pay attention when using it.

2. Use TerminateProcess() to end the process

If you want to be in a processTo force end other processes, use TerminateProcess()To achieve it. Unlike ExitProcess(), after the TerminateProcess() function is executed, the terminated process will not receive any notification about the program exit. In other words, the terminated process cannot perform the final work before exiting before ending the run. Therefore, usually, TerminateProcess() is considered to force the process to end the process only if no other method can force the process to exit.
Function prototype of TerminateProcess(): BOOL TerminateProcess(HANDLE hProcess,UINT uExitCode);

The parameters hProcess and uExitCode are the process handle and exit code respectively. If the process is ended, the handle can be obtained through GetCurrentProcess().

TerminateProcess() is executed asynchronously. After the call returns, it is not certain whether the terminated process has really exited. If the process calling TerminateProcess() is concerned about this details, you can wait for the process to truly end through WaitForSingleObject().

3. Application

How to end other processes running in the system in the VC program (the process must have a window interface), is actually very simple, just follow the following steps:
1. Get the handle of the process (get it using the FindWindow function);
2. Get the process ID number (use GetWindowThreadProcessId function to get);
3. Open the process and set the first parameter in the OpenProcess function to PROCESS_TERMINATE to get the handle to process 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 the 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 );
}

//The startup process only requires the CreateProcess function to complete. It is important to note that several input parameters of this function are

//Start a new process
int CStaticFunc::CreateNewProcess(LPCSTR pszExeName)
{
    PROCESS_INFORMATION piProcInfoGPS;
    STARTUPINFO siStartupInfo;
    SECURITY_ATTRIBUTES saProcess, saThread;
    ZeroMemory( &siStartupInfo, sizeof(siStartupInfo) );
    = sizeof(siStartupInfo);
    = sizeof(saProcess);
    = NULL;
    = true;
    = sizeof(saThread);
    = NULL;
    = true;
    return ::CreateProcess( NULL, (LPTSTR)pszExeName, &saProcess,&saThread, false,
           Create_DEFAULT_ERROR_MODE, NULL, NULL, &siStartupInfo,&piProcInfoGPS );
}