Self-deletion Implementation of MFC

Source: Internet
Author: User

1. Add the member function bool deleteselft () to the DLG class. The Code is as follows:

class CDelSelfDlg : public CDialog   {   // Construction   public:       CDelSelfDlg(CWnd* pParent = NULL); // standard constructor       BOOL DeleteSelf();       ......   } class CDelSelfDlg : public CDialog{// Constructionpublic:CDelSelfDlg(CWnd* pParent = NULL); // standard constructorBOOL DeleteSelf();......}

2. The function implementation code is as follows:

/*************************************** * ******************************** // * Function description: when a process exits, it deletes itself/* parameter: NONE/* return value: TRUE is returned for success, FALSE is returned for failure, and *: koma 2009.08.06 /************************************ * **********************************/BOOL CDelSelfDlg:: DeleteSelf () {TCHAR szModule [MAX_PATH]; TCHAR szComspec [MAX_PATH]; TCHAR szParams [MAX_PATH]; // get file path names: if (GetModuleFileName (0, szModu Le, MAX_PATH )! = 0) & (getequalpathname (szModule, szModule, MAX_PATH )! = 0) & (GetEnvironmentVariable ("COMSPEC", szComspec, MAX_PATH )! = 0) {// set command shell parameters lstrcpy (szParams, "/c del"); lstrcat (szParams, szModule); lstrcat (szParams, "> nul "); lstrcat (szComspec, szParams); // set struct members STARTUPINFO si = {0}; PROCESS_INFORMATION pi = {0}; si. cb = sizeof (si); si. dwFlags = STARTF_USESHOWWINDOW; si. wShowWindow = SW_HIDE; // increase resource allocation to program SetPriorityClass (GetCurrentProcess (), REALTIME_PRI ORITY_CLASS); SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_TIME_CRITICAL); // invoke command shell if (CreateProcess (0, szComspec, 0, 0, 0, create_suincluded | DETACHED_PROCESS, 0, 0, & si, & pi) {// suppress command shell process until program exits SetPriorityClass (pi. hProcess, IDLE_PRIORITY_CLASS); SetThreadPriority (pi. hThread, THREAD_PRIORITY_IDLE); // resume shell process with new low prio Rity ResumeThread (pi. hThread); // everything seemed to work return TRUE;} else // if error, normalize allocation {SetPriorityClass (GetCurrentProcess (), NORMAL_PRIORITY_CLASS); SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_NORMAL) ;}} return FALSE ;} /*************************************** * ******************************** // * function description: when a process exits, it deletes itself/* parameter: NONE/* return value: TRUE is returned for success, FALSE is returned for failure, and * By: Kom A 2009.08.06 /************************************ * **********************************/BOOL CDelSelfDlg:: DeleteSelf () {TCHAR szModule [MAX_PATH]; TCHAR szComspec [MAX_PATH]; TCHAR szParams [MAX_PATH]; // get file path names: if (GetModuleFileName (0, szModule, MAX_PATH )! = 0) & (getequalpathname (szModule, szModule, MAX_PATH )! = 0) & (GetEnvironmentVariable ("COMSPEC", szComspec, MAX_PATH )! = 0) {// set command shell parameters lstrcpy (szParams, "/c del"); lstrcat (szParams, szModule); lstrcat (szParams, "> nul "); lstrcat (szComspec, szParams); // set struct members STARTUPINFO si = {0}; PROCESS_INFORMATION pi = {0}; si. cb = sizeof (si); si. dwFlags = STARTF_USESHOWWINDOW; si. wShowWindow = SW_HIDE; // increase resource allocation to program SetPriorityClass (GetCurrentProcess (), REALTIME_PRIORITY_CLASS); SetThreadPriority (GetCurrentThread (), success); // invoke command shell if (CreateProcess (0, szComspec, 0, 0, 0, create_suincluded | DETACHED_PROCESS, 0, 0, & si, & pi) {// suppress command shell process until program exits SetPriorityClass (pi. hProcess, IDLE_PRIORITY_CLASS); SetThreadPriority (pi. hThread, THREAD_PRIORITY_IDLE); // resume shell process with new low priority ResumeThread (pi. hThread); // everything seemed to work return TRUE;} else // if error, normalize allocation {SetPriorityClass (GetCurrentProcess (), NORMAL_PRIORITY_CLASS); SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_NORMAL) ;}} return FALSE ;}

3. Right-click the DLG class, add windows message handler, find wm_destroy, and call the member function:

/*************************************** * ******************************** // * Function description: window destruction message/* parameter: NONE/* return value: NONE/*: koma 2009.08.06 /************************************ * **********************************/void cdelselfdlg:: ondestroy () {cdialog: ondestroy (); // todo: add your message handler code here deleteself ();}

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.