如何使用inno设置结束流程?

时间:2011-10-27 13:51:04

标签: install inno-setup uninstaller windows-applications kill-process

我想使用inno setup终止进程。我想在开始安装设置之前检查窗口是否已打开。

我可以通过搜索Windows名称来做到这一点吗? 请帮我一些示例代码来杀死进程

4 个答案:

答案 0 :(得分:16)

在[UninstallRun]部分中,您可以添加以下代码:

Filename: {sys}\taskkill.exe; Parameters: "/f /im MyProcess.exe"; Flags: skipifdoesntexist runhidden

答案 1 :(得分:5)

我这样做是为了提示用户在程序运行时关闭程序。

function CheckProcessRunning( aProcName,
                              aProcDesc: string ): boolean;
var
  ShellResult: boolean;
  ResultCode: integer;
  cmd: string;
  sl: TStringList;
  f: string;
  d: string;
begin
  cmd := 'for /f "delims=," %%i ' + 
         'in (''tasklist /FI "IMAGENAME eq ' + aProcName + '" /FO CSV'') ' + 
         'do if "%%~i"=="' + aProcName + '" exit 1'; 
  f := 'CheckProc.cmd';
  d := AddBackSlash( ExpandConstant( '{tmp}' ));
  sl := TStringList.Create;
  sl.Add( cmd );
  sl.Add( 'exit /0' );
  sl.SaveToFile( d + f );
  sl.Free;
  Result := true;
  while ( Result ) do
  begin
    ResultCode := 1;
    ShellResult := Exec( f,
                         '',
                         d, 
                         SW_HIDE, 
                         ewWaitUntilTerminated, 
                         ResultCode );
    Result := ResultCode > 0;
    if Result and 
       ( MsgBox( aProcDesc + ' is running. This program must be closed to continue.'#13#10 + 
                 'Please close it and click Yes to retry or click No to cancel this installer.', 
                 mbConfirmation, 
                 MB_YESNO ) <> IDYES ) then
      Break;
  end;
  DeleteFile( d + f );
end;

我在以下特殊代码部分中使用它如下:

// Perform some initializations.  Return False to abort setup
function InitializeSetup: Boolean;
begin
  // Do not use any user defined vars in here such as {app}
  InitializeGlobals;
  Result := not ( CheckProcessRunning( 'bcb.exe',      'C++ Builder' ) or
                  CheckProcessRunning( 'delphi32.exe', 'Delphi'      ) or
                  CheckProcessRunning( 'bds.exe',      'Rad Studio'  ));
end;


function InitializeUninstall: Boolean;
begin
  InitializeGlobals;
  Result := not ( CheckProcessRunning( 'bcb.exe',      'C++ Builder' ) or
                  CheckProcessRunning( 'delphi32.exe', 'Delphi'      ) or
                  CheckProcessRunning( 'bds.exe',      'Rad Studio'  ));
end;

答案 2 :(得分:1)

最好的选择是使用互斥锁查看它是否仍在使用AppMutex运行。关闭它的一种方法是找到窗口句柄然后发布一个简单的WM_CLOSE消息。

还有其他选项,例如Files in use extensionPSVince

另请参阅this article了解更多信息。

答案 3 :(得分:1)

一个不太复杂的方法是运行使用任务终止的batch file

ie:如果你想在卸载之前杀死记事本

taskkill /IM notepad.exe