VC Create new process, end process (open EXE and other executable files)

Source: Internet
Author: User
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 the following steps can be:
1. Obtain the handle of the process (obtained by using the FindWindow function);
2. Get process ID number (obtained by GetWindowThreadProcessId function);
3. Open the process, the first parameter in the OpenProcess function is set to Process_terminate, you can get the handle that handles the process;
4. Using the TerminateProcess function to end the process, 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);

Method One:

:: GetWindowThreadProcessId (Thewindow, &nprocessid);
Hprocesshandle =:: OpenProcess (Process_terminate, FALSE,

NPROCESSID);
Return:: TerminateProcess (Hprocesshandle, 4);

Method Two:

return::P ostmessage (Thewindow, wm_close, NULL, NULL);

}


The START process only needs the CreateProcess function to complete, you need to pay attention to the function of several input parameters, the first parameter is

Start a new process

Method One:

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 Two:

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.