VC creates a new process and ends the process (Open executable files such as exe)

Source: Internet
Author: User
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 the process
Int cstaticfunc: killprocess (lpcstr pszclassname, lpcstr

Pszwindowtitle)
{
Handle hprocesshandle;
Ulong nprocessid;
Hwnd thewindow;

Thewindow =: findwindow (null, pszwindowtitle );

/// Method 1:

: Getwindowthreadprocessid (thewindow, & nprocessid );
Hprocesshandle =: OpenProcess (process_terminate, false,

Nprocessid );
Return: terminateprocess (hprocesshandle, 4 );

/// Method 2:

Return: postmessage (thewindow, wm_close, null, null );

}


To start a process, you only need the CreateProcess function to complete the process. Note the input parameters of this function. The first parameter is

// Start a new process

/// Method 1:

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 );
}

/// Method 2:

Winexec (Lpcmdline,Ucmdshow);

 

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.