请问:如何監控所控管的程式是否已經中斷,如果中斷要再叫起來?(50分)

  • 主题发起人 主题发起人 huangqunzhe
  • 开始时间 开始时间
H

huangqunzhe

Unregistered / Unconfirmed
GUEST, unregistred user!
  例如:要對c:/XXX/aa.exe &nbsp;; &nbsp; d:/XXX/bb.exe &nbsp; ; &nbsp; e:/XXX/cc.exe<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 等三個可執行程序進行監控。隻知道它們的路徑和文件名。
 
随手写了一个简单的线程函数能实现你的要求, 不过没有判断出错与监视文件不存在的情况, 另外线程没有设置出口 [:D][:D][:D][:D], 不过我想你只要稍微改改就能用了。<br><br>调用方式: ExeMonitor('c:/xxx/aa.exe')即可。<br><br>uses<br>&nbsp; sysutils, windows, tlHelp32;<br><br>procedure MonitorThrd(const filename: string); stdcall;<br>var<br>&nbsp; h, h1: Cardinal;<br>&nbsp; pe: PROCESSENTRY32;<br>&nbsp; me: MODULEENTRY32;<br>&nbsp; Startup: _STARTUPINFOA;<br>&nbsp; PInfo: _PROCESS_INFORMATION;<br>&nbsp; s: string;<br>&nbsp; ps, fn: string;<br>label<br>&nbsp; l1;<br>begin<br>&nbsp; {分解文件名与路径}<br>&nbsp; fn := lowercase(extractfilename(filename));<br>&nbsp; ps := lowercase(extractfilepath(filename));<br>&nbsp; if ps[length(ps)]='/' then ps := copy(ps, 1, length(ps)-1);<br>&nbsp; PInfo.dwProcessId := 0;<br>&nbsp; {查询被监视程序是否已启动}<br>&nbsp; h := createtoolhelp32snapshot(TH32CS_SNAPPROCESS, 0);<br>&nbsp; pe.dwSize := sizeof(pe);<br>&nbsp; if process32first(h, pe) then<br>&nbsp; &nbsp; repeat<br>&nbsp; &nbsp; &nbsp; s := pe.szExeFile;<br>&nbsp; &nbsp; &nbsp; if lowercase(s)=fn then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; h1 := createtoolhelp32snapshot(TH32CS_SNAPMODULE, pe.th32ProcessID);<br>&nbsp; &nbsp; &nbsp; &nbsp; me.dwSize := sizeof(me);<br>&nbsp; &nbsp; &nbsp; &nbsp; if module32first(h1, me) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; repeat<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s := me.szExePath;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if lowercase(s)=ps then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PInfo.dwProcessId := pe.th32ProcessID;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; goto l1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; until not module32next(h1, me);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; until not process32next(h, pe);<br>&nbsp; closehandle(h);<br>l1:<br>&nbsp; {监控程序运行,如果关闭则自动重起}<br>&nbsp; while true do<br>&nbsp; begin<br>&nbsp; &nbsp; if pinfo.dwProcessId &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp; pinfo.hProcess := openprocess(PROCESS_ALL_ACCESS, false, pinfo.dwProcessId)<br>&nbsp; &nbsp; else begin<br>&nbsp; &nbsp; &nbsp; fillchar(pinfo, sizeof(pinfo), 0);<br>&nbsp; &nbsp; &nbsp; fillchar(startup, sizeof(startup), 0);<br>&nbsp; &nbsp; &nbsp; startup.cb := sizeof(startup);<br>&nbsp; &nbsp; &nbsp; createprocess(pchar(filename), nil, nil, nil, false, 0, nil, pchar(ps), startup, pinfo);<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; waitforsingleobject(pinfo.hProcess, INFINITE);<br>&nbsp; &nbsp; closehandle(pinfo.hProcess);<br>&nbsp; &nbsp; pinfo.dwProcessId := 0;<br>&nbsp; end;<br>end;<br><br>function ExeMonitor(FileName: string): Cardinal;<br>begin<br>&nbsp; {修改字符串引用计数,欺骗delphi,防止传递的参数FileName被自动释放}<br>&nbsp; if pinteger(integer(filename)-8)^ &gt;= 0 then<br>&nbsp; &nbsp; inc(pinteger(integer(filename)-8)^);<br>&nbsp; {创建线程监视程序运行, 返回值为线程ThreadID, 你可以通过TerminateThread终止该线程}<br>&nbsp; closehandle(createthread(nil, 0, @MonitorThrd, pointer(FileName), 0, result));<br>end;<br><br>
 
不知是否可以監控可程序喚起失敗的次數?謝謝了。。。
 
当然可以。上述线程代码中把计数代码加到循环体内最后就可以了
 
5555,我还是菜鸟一只啊,能不能写的详细点,,,这里先把分送上了 。
 
后退
顶部