Delphi-在服务中正常关闭创建的进程。 (使用tprocess / createProcess) [英] Delphi - Gracefully Closing Created Process in Service. (using tprocess / createProcess)

查看:101
本文介绍了Delphi-在服务中正常关闭创建的进程。 (使用tprocess / createProcess)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用Delphi编写的Windows服务,该服务运行许多程序。

I have a Windows Service written in Delphi which runs a number of programs.

在停止服务时,我也要关闭这些程序。最初编写服务时,此方法运行良好,但我想我已经更新了tProcess组件,现在-下级程序没有关闭。

On Stopping the service, I want to also close these programs. When the service was originally written, this worked fine, but I think I've updated the tProcess component and now - The subordinate programs are not being closed.

在tProcess中-这是启动新流程的代码。

in tProcess - Here's the code which starts the new processes.

  if CreateProcess( nil , PChar( FProcess.Command ) , nil , nil , False ,
    NORMAL_PRIORITY_CLASS , nil , Directory ,
    StartupInfo , ProcessInfo ) then
    begin
      if FProcess.Wait then
        begin
          WaitForSingleObject( ProcessInfo.hProcess , Infinite );
          GetExitCodeProcess( ProcessInfo.hProcess , ExitCode );
          if Assigned( FProcess.FOnFinished ) then
            FProcess.FOnFinished( FProcess , ExitCode );
        end;
      CloseHandle( ProcessInfo.hProcess );
      CloseHandle( ProcessInfo.hThread );
    end;

此调用的每个可执行文件都是Windows GUI程序(顶部带有关闭按钮)。

Each of the executables called by this are Windows GUI Programs (With a close button at the top).

当我停止服务时,我也想停止(而不是终止)通过createProcess过程启动的程序。

When I stop the service, I also want to stop (not kill) the programs I've started up via the createProcess procedure.

您将如何做?

推荐答案

您想枚举与已启动的ProcessId匹配的打开的窗口,并告诉这些窗口关闭。这是一些示例代码:

You want to enumerate open windows that match your launched ProcessId and tell those windows to close. Here's some sample code for that:

uses Windows;

interface   

function MyTerminateAppEnum(hHwnd:HWND; dwData:LPARAM):Boolean; stdcall;

implementation

function MyTerminateAppEnum(hHwnd:HWND; dwData:LPARAM):Boolean; 
var   
  vID:DWORD; 
begin   
  GetWindowThreadProcessID(hHwnd, @vID);   
  if vID = dwData then   
  begin
    PostMessage(hHwnd, WM_CLOSE, 0, 0); //tell window to close gracefully
    Result := False;  //can stop enumerating    
  end   
  else   
  begin
    Result := TRUE;  //keep enumerating until you find your id   
  end; 
end;

然后,当您想关闭启动的应用程序时,将在代码中使用它:

Then you'll want to utilize this in your code when you want to shut down the launched applications:

Procedure TerminateMe(YourSavedProcessInfo:TProcessInformation);
var
  vExitCode:UINT;
begin
  GetExitCodeProcess(YourSavedProcessInfo.hProcess, vExitCode);
  if (vExitCode = STILL_ACTIVE) then  //launched app still running..
  begin
    //tell it to close
    EnumWindows(@MyTerminateAppEnum, YourSavedProcessInfo.dwProcessId);

    if WaitForSingleObject(YourSavedProcessInfo.hProcess, TIMEOUT_VAL) <> WAIT_OBJECT_0 then
    begin
      if not TerminateProcess(YourSavedProcessInfo.hProcess, 0) then  //didn't close, try to terminate
      begin
         //uh-oh   Didn't close, didn't terminate..
      end;
    end;
  end;
  CloseHandle(YourSavedProcessInfo.hProcess);
  CloseHandle(YourSavedProcessInfo.hThread);
end;

这篇关于Delphi-在服务中正常关闭创建的进程。 (使用tprocess / createProcess)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆