VC ++ ends the program process

Source: Internet
Author: User

1. Use exitprocess () to end the process

A process only provides an address space and a kernel object, which is embodied by the main thread in the address space during running. When the entry point function of the main thread returns, the process ends. The termination method of this process is the normal exit of the process, and all the thread Resources in the process can be cleared correctly. In addition to the normal exit mode of this process, you also needCodeTo forcibly end the running of this process or other processes. The prototype of the exitprocess () function is:

Void exitprocess (uint uexitcode );

The uexitcode parameter sets the exit code for the process. This function is mandatory. After execution, the process is terminated. Therefore, no code is executed after execution. Although the exitprocess () function can notify the dynamic link library associated with the process when the process is terminated, the execution of this function is mandatory, so that exitprocess () functions have security risks. For example, if you have applied for a space using the new operator before the program calls the exitprocess () function, the exitprocess () function cannot be released through the delete operator because it is mandatory, this results in Memory leakage. Note that exitprocess () functions are mandatory and insecure.

2. Use terminateprocess () to end the process

Exitprocess () can only force the exit of this process. If you want to force the termination of other processes in a process, use terminateprocess. Unlike exitprocess (), after the terminateprocess () function is executed, the terminated process will not receive any notification about program exit. That is to say, the terminated process cannot finish the work before exiting before the end of the operation. Therefore, terminateprocess () is usually considered to force the process to end only when no other method can force the process to exit. The following is a prototype of the terminateprocess () function:

Bool terminateprocess (handle hprocess, uint uexitcode );

The hprocess and uexitcode parameters are process handles and exit codes respectively. If the process ends, you can get the handle through getcurrentprocess. Terminateprocess () is executed asynchronously. After a call is returned, you cannot determine whether the terminated process has actually exited. If the process that calls terminateprocess () cares about this details, you can use waitforsingleobject () to wait for the real end of the process.

 

 

In the VC program, how to end other processes that are running in the system (the process must have a window interface) is actually very simple, just follow the steps below:
1. Obtain the Process Handle (obtained using the findwindow function );
2. Get the process ID (obtained using the getwindowthreadprocessid function );
3. Open the process and set the first parameter in the OpenProcess function to process_terminate to obtain 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 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 );
}

To start a process, you only need the CreateProcess function. // 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 );
}

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.