How to end other processes running in the VC program and the program itself

Source: Internet
Author: User

 

In VCProgramHow to end other processes that are running in the system (the process must have a window interface) is actually very simple, 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.

 

 CodeAs 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 process still exists in the task manager after your program exits. This function can be called to end.

Int cstaticfunc: killprocess (lpcstr pszwindowtitle)
{

/////////////// Pszwindowtitle: your own program name

////////////// Call this function directly when you end your program (lpcstr pname ("your program name "))
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 to complete the process. Note the input parameters of this function. The first parameter is

// start a new process
int cstaticfunc: createnewprocess (lpcstr pszexename)
{< br> process_information piprocinfogps;
startupinfo sistartupinfo;
specify 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.