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