请问在Delphi程序中,如何监视某一个程序的运行状态?
若该程序正在运行或已经运行,程序就做出提示或关闭该程序我用下边两种方法都不是很有效,有时不起做用。求用别的方法实现。谢谢……第一种方法:
var
  h: THandle;
  text: array[0..255] of char;
begin
  h := getWindow(handle, GW_HWNDFIRST);
  while h <> 0 do
  begin
    if GetWindowText(h, @text, 255) > 0 then
      if StrPas(text) = '被监视程序在运行时的标题'then
          PostMessage(h, WM_SYSCOMMAND, SC_CLOSE, 0);//关闭该程序
    h := GetWindow(h, GW_HWNDNEXT);
  end;
end;
第二种方法:
var
  hwnd:THandle;
begin
   hwnd:= FindWindow(nil,'被监视程序在进程中的名字');
  if hwnd<>0 then 
 //PostMessage(hwnd,WM_Close,0,0);//关闭该程序
end;

解决方案 »

  1.   

    const
      CM_RESTORE = WM_USER + $1000;
    var
      RvHandle : hWnd;
    begin
       RvHandle := FindWindow('你的程序名字', NIL);
      if RvHandle > 0 then
      begin
        Application.MessageBox('您的系统中已经有程序正在运行!','提示',MB_OK+MB_ICONERROR+MB_SYSTEMMODAL);
        PostMessage(RvHandle, CM_RESTORE, 0, 0);
      end;
      

  2.   

    hsmserver(撒哈拉之雨的悲伤):
    谢谢!但这种方法和我上边的第二种方法是相同的,这种方法有时不起作用。求别的方法!!!
      

  3.   

    function Killpro(ExeFileName: string): integer;  //杀死进程的函数.
    const
      PROCESS_TERMINATE=$0001;var
      ContinueLoop: BOOL;
      FSnapshotHandle: THandle;
      FProcessEntry32: TProcessEntry32;
    begin
      result:= 0;
      FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
      FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
      ContinueLoop := Process32First(FSnapshotHandle,FProcessEntry32);
      while integer(ContinueLoop) <> 0 do
      //循环枚举快照中所有进程信息
      begin
        if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile))=UpperCase(ExeFileName))
            or (UpperCase(FProcessEntry32.szExeFile)=UpperCase(ExeFileName))) then
        //找到要中止的进程名
        //若进程在运行则一定能找到,找到就关闭
           Result := Integer(TerminateProcess(OpenProcess(PROCESS_TERMINATE, BOOL(0),
                     FProcessEntry32.th32ProcessID), 0));
         //中止进程
           ContinueLoop := Process32Next(FSnapshotHandle,FProcessEntry32);
         //查找下一个符合条件进程
      end;
    end;Killpro('QQ.EXE');
      

  4.   

    谢谢 hottey(孤独的探索者)(我是一闲人) ,这个方法可以解决问题!接分!