Ive just cooked up this little PB/CC proggy that uses DebugActiveProcess to 'monitor' a process ID to detect the exact millisecond that any monitored process terminates. Yes there are thousands of ways to detect when a process has closed, but this seems to be the fastest in that it 'kicks in' virtually immediately as soon as the monitored process terminates and there isnt any polling as such, just sitting in a WaitForDebugEvent loop.
To try it out, start a program like calc.exe, then from command prompt run "procmon.exe 500", where 500 is the PID of calc.exe. Then, when you feel like it close Calculator, and the PB/CC monitoring program should tell you immediately that the process has closed.
NOW, my question!... are there any ways I can improve this? Does being attached to a process in this way slow down the target program? Does sitting in the WaitForDebugEvent hinder system performance at all?
All feedback is very much appreciated
------------------
[This message has been edited by Wayne Diamond (edited July 10, 2001).]
To try it out, start a program like calc.exe, then from command prompt run "procmon.exe 500", where 500 is the PID of calc.exe. Then, when you feel like it close Calculator, and the PB/CC monitoring program should tell you immediately that the process has closed.
NOW, my question!... are there any ways I can improve this? Does being attached to a process in this way slow down the target program? Does sitting in the WaitForDebugEvent hinder system performance at all?
All feedback is very much appreciated
Code:
'[b]PROCMON.BAS[/b] #COMPILE EXE #INCLUDE "win32api.inc" SUB DoEventsAPI ON ERROR RESUME NEXT STATIC Msg AS tagMsg IF PeekMessage(Msg, %NULL, 0, 0, %PM_REMOVE) THEN TranslateMessage Msg DispatchMessage Msg END IF END SUB FUNCTION PBMAIN() AS LONG DIM DE AS DEBUG_EVENT DIM SI AS STARTUPINFO, PI AS PROCESS_INFORMATION DIM lRes AS LONG, lStatus AS LONG DIM ProcID AS LONG, DebugRes AS LONG ProcID = VAL(TRIM$(COMMAND$)) IF ProcID = 0 OR ProcID = -1 THEN STDOUT "Waynes Process Termination Monitor Test" STDOUT "Usage: procmon <process ID>" EXIT FUNCTION END IF DebugRes = DebugActiveProcess(ProcID) IF DebugRes = 0 THEN STDOUT "Failed to attach to process " & TRIM$(STR$(ProcID)) EXIT FUNCTION END IF STDOUT "Attached to process " & TRIM$(STR$(ProcID)) & " - waiting for termination..."; DO ' Wait 1ms for a debug event lRes = WaitForDebugEvent(DE, 1) ' If lRes <> 0 there's a debug event in DE IF lRes THEN ' Set the default continue status lStatus = %DBG_CONTINUE SELECT CASE DE.dwDebugEventCode CASE %EXIT_PROCESS_DEBUG_EVENT STDOUT "" STDOUT "Process has terminated!" END SELECT ' Continue the process execution ContinueDebugEvent DE.dwProcessId, DE.dwThreadId, lStatus END IF ' Allow this process to process messages DoEventsAPI LOOP UNTIL DE.dwDebugEventCode = %EXIT_PROCESS_DEBUG_EVENT END FUNCTION
------------------
[This message has been edited by Wayne Diamond (edited July 10, 2001).]
Comment