First one.
How to close a process:
int Terminateprocessfromid () //{ BOOL bRet=FALSE; if (hprocess!=NULL) { bRet=::terminateprocess (hprocess,0); } :: CloseHandle (hprocess); return 0 ; }
Event for Start button
voidCchendlg::onbnclickedbtnstart () {//TODO: Add control notification handler code hereCString Currpid; DWORD dwId;//Current PIDM_CURRPID.GETWINDOWTEXTW (currpid); DwId=_ttoi (currpid); Hprocess=:: OpenProcess (PROCESS_ALL_ACCESS,FALSE,DWID); if(hprocess==NULL) {MessageBox (L"No program found, please enter the correct PID"); } Else{SetTimer (1, +, NULL); MessageBox (L"operation successful, start monitoring"); }}
Operation in the OnTimer
voidCchendlg::ontimer (uint_ptr nidevent) {//TODO: Add Message Handler code and/or call default values hereCdialogex::ontimer (nidevent); CString localtime; TM=Ctime::getcurrenttime (); inthour =TM. Gethour (); CTime Closetm; //Set the end time hereClosetm =ctime (tm. GetYear (), TM. GetMonth (), TM. GetDay (),4,0,0,-1); CTimeSpan s; if(hour>4) { //description to span the number of days//get the time of the last second of the dayCTime finallytime =ctime (tm. GetYear (), TM. GetMonth (), TM. GetDay (), at, -, -,-1); CTimeSpan Finallyspan= finallytime-TM; //Closetm = CLOSETM+FINALLYSPAN+HOUR4;s = Finallyspan + CTimeSpan (0,4,0,1);//4 hours } Else{s= closetm-TM; if(s<=0) { //that means it's time.Terminateprocessfromid (); M_time. SETWINDOWTEXTW (L"(0 seconds) program is turned off"); KillTimer (1); } } intM_hour=s.gethours (); intM_min =s.getminutes (); intm_s =s.getseconds (); CString Timestr; Timestr.format (_t ("%d minutes%d seconds"), m_hour,m_min,m_s); LocalTime=tm. Format ("%y year%m month%d day%x"); M_CURRTIME.SETWINDOWTEXTW (localtime); M_time. SETWINDOWTEXTW (TIMESTR);}
MFC-timed off a software