VC uses TerminateProcess to end process instance _c language

Source: Internet
Author: User

This article describes the VC using TerminateProcess to end the process of the method to share for everyone to reference. The implementation methods are as follows:

Copy Code code as follows:
int 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);
}

void Czxdlg::onok ()
{
Todo:add Extra Validation here
Cdialog::onok ();
KillProcess ("Radmin.exe", "Radmin Viewer")
}

void Czxdlg::onbutton1 ()
{
HANDLE hSnapShot =:: CreateToolhelp32Snapshot (th32cs_snapprocess,0);
if (hsnapshot = 0)
Return
PROCESSENTRY32 Thepe;
thepe.dwsize = sizeof (PROCESSENTRY32);
Traversing the first system process that is running
BOOL Status = Process32First (Hsnapshot,&thepe);
BOOL Bhaveflag = false;
DWORD ProcessID = 0;
while (Status)
{
Traversing the next system process that is running
Status = Process32Next (Hsnapshot,&thepe);
CString Jincheng = "Qq.exe";
Jincheng. Makelower ();
Find the appropriate process **.exe
if (0 = = _tcscmp (thepe.szexefile,_t (Jincheng))
{
Bhaveflag = true;
ProcessID = Thepe.th32processid;
Ends the specified process ProcessID
if (! TerminateProcess (openprocess, process_terminate| | PROCESS_QUERY_INFORMATION,FALSE,PROCESSID), 0)
{
AfxMessageBox ("Unable to terminate the specified process!") ", Mb_iconwarning| | MB_OK);
}
Break
}
}
CloseHandle (hSnapShot);
}




Your own improved code is as follows:
Copy Code code as follows:
void Kill (CString Jin)
{
HANDLE hSnapShot =:: CreateToolhelp32Snapshot (th32cs_snapprocess,0);
if (hsnapshot = 0)
Return
PROCESSENTRY32 Thepe;
thepe.dwsize = sizeof (PROCESSENTRY32);
Traversing the first system process that is running
BOOL Status = Process32First (Hsnapshot,&thepe);
BOOL Bhaveflag = false;
DWORD ProcessID = 0;
while (Status)
{
Traversing the next system process that is running
Status = Process32Next (Hsnapshot,&thepe);
CString Jincheng;
Jincheng=thepe.szexefile;
Jincheng. Makelower ();
Find the appropriate process **.exe
if (Jincheng = Jin)
{
Bhaveflag = true;
ProcessID = Thepe.th32processid;
AfxMessageBox (Thepe.szexefile,mb_iconwarning| | MB_OK);
Ends the specified process ProcessID
if (! TerminateProcess (openprocess, process_terminate| | PROCESS_QUERY_INFORMATION,FALSE,PROCESSID), 0)
{
AfxMessageBox ("Unable to terminate the specified process!") ", Mb_iconwarning| | MB_OK);
}
Break
}
}
CloseHandle (hSnapShot);
}

I hope this article on the VC program for everyone to help.

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.