RSS订阅 | 匿名投稿
您的位置:网站首页 > 程序 > 正文

delphi 程序重新启动自身

作者:admin 来源: 日期:2018/2/11 16:55:37 人气: 标签:

=======================
用于重启动的进程,等到传入的进程确实退出后,再执行传入的命令行
=======================
program tmRestart;

//{$APPTYPE CONSOLE}

uses
  Windows,
  Dialogs,
  Classes,
  SysUtils;

procedure StopProcess(ProcessID : DWORD);
var
  Handle: THandle;
begin
  Handle := OpenProcess(PROCESS_TERMINATE or PROCESS_VM_READ, False, ProcessID);
  if Handle <> 0 then
    try
      TerminateProcess(Handle,0);
      WaitForSingleObject(Handle,INFINITE);
    finally
      CloseHandle(Handle);
    end;
end;

var
  vProcessID:integer;
  vCommandLine:string;
  vList:TStringList;
  vHandle:THandle;
begin
  vList:=TStringList.Create;
  try
    vList.Delimiter:=' ';
    vList.CommaText:=Windows.GetCommandLine;
    if vList.Count<3 then exit;
    vProcessID:=StrToInt(vList[1]);
    vCommandLine:=vList[2];
  finally
    vList.Free;
  end;
  //ParamStr函数有bug,会去掉任何的双引号,当文件夹有空格会出问题
  //如'"D:\Shanghai China\A.exe" "D:\Shanhai China\A.ini"',会得到'D:\Shanghai China\A.exe D:\Shanhai China\A.ini'
  //vProcessID:=StrToInt(ParamStr(1));
  //vCommandLine:=ParamStr(2);
  repeat
    Sleep(100);
    vHandle := Windows.OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, vProcessID);
    if vHandle<>0 then CloseHandle(vHandle);
  until vHandle=0;

  //不要强制终止,会导致单元的finialization执行不到
  //StopProcess(vProcessID);
  Windows.WinExec(PChar(vCommandLine),SW_Show);
end.





=====================================
主程序
ExitProgram是一个boolean变量,主窗体的Logout功能把它设为false
=====================================
program ERP2009;
{$R *.RES}
function WinExecAndWait32(AExeFile,AParams:WideString;AIsShowGUI:Boolean;AIsWaitingResult:Boolean):cardinal;
var
  ExecInfo:   TShellExecuteInfoW;
  ucmdShow:integer;
begin
  Result:=$FFFF;
  if AIsShowGUI then ucmdShow:=SW_Show
  else ucmdShow:=SW_Hide;
  ZeroMemory(@ExecInfo,SizeOf(ExecInfo));
  ExecInfo.cbSize   :=   SizeOf(ExecInfo);
  ExecInfo.fMask    :=   SEE_MASK_NOCLOSEPROCESS;
  ExecInfo.lpVerb   :=   'open';
  ExecInfo.lpFile   :=   PWideChar(AExeFile);
  ExecInfo.lpParameters:=PWideChar(AParams);
  ExecInfo.Wnd      :=   0;
  ExecInfo.nShow    :=   ucmdShow;
  if ShellExecuteExW(@ExecInfo) then
  begin
    Result:=0;
    if AIsWaitingResult then
    begin
      if WaitforSingleObject(ExecInfo.hProcess,INFINITE)=WAIT_OBJECT_0 then
        GetExitCodeProcess(ExecInfo.hProcess,Result);
    end;
  end;
end;

procedure RestartProgramme(AMonitorFileName:WideString);
var
  i:integer;
  //S:string;
  CommandLine:WideString;
  vExeFile,vParam:WideString;
begin
  CommandLine:='';
  for i:=0 to ParamCount do
  begin
    if CommandLine<>'' then CommandLine:=CommandLine+' ';
    CommandLine:=CommandLine+AnsiQuotedStr(ParamStr(i),'"');
  end;
  {S:=AnsiQuotedStr(ExtractFilePath(Application.ExeName)+'KMonitor.exe','"')+' '+
     InttoStr(Windows.GetCurrentProcessID)+' '+
     AnsiQuotedStr(CommandLine,'"');
  }
  vExeFile:=AnsiQuotedStr(ExtractFilePath(Application.ExeName)+AMonitorFileName,'"');
  vParam:=InttoStr(Windows.GetCurrentProcessID)+' '+AnsiQuotedStr(CommandLine,'"');

  //用WinExec会感觉卡
  //Windows.WinExecW(PWideChar(S),SW_Hide);
  WinExecAndWait32(vExeFile,vParam,false,false);
end;

begin
  //也许这里有检查只能启动一个进程的代码
  //........
  ExitProgram:=true;
  Application.Initialize;
  Application.CreateForm(TIDEMainForm, IDEMainForm);
  Application.Run;
  if not ExitProgram then
    RestartProgramme('tmRestart.exe');
end.

读完这篇文章后,您心情如何?
0
0
0
0
0
0
0
0
本文网址:
下一篇:没有资料