如何使用 inno setup 结束进程?

发布于 2024-12-12 05:41:05 字数 105 浏览 0 评论 0原文

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

我可以通过搜索 Windows 名称来做到这一点吗? 请帮我提供一些示例代码来终止该进程

I want to kill a process using inno setup.i want to check whether the window is open before i start installing the setup.

can i do this by searching windows name?
please help me with some sample code to kill the process

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

轮廓§ 2024-12-19 05:41:05

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

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

in the [UninstallRun] section, you can add the following code:

Filename: {sys}\taskkill.exe; Parameters: "/f /im MyProcess.exe"; Flags: skipifdoesntexist runhidden
梦言归人 2024-12-19 05:41:05

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

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;

This is something I did to prompt the user to close a program if it was found running.

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;

I used it in the following special code sections as follows:

// 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;
┼── 2024-12-19 05:41:05

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

还有其他选项,例如 正在使用的文件扩展PSVince

另请参阅本文了解更多信息。

The best option is to use a mutex to see if it's still running using AppMutex. One way to close it is to find the window handle then just post a simple WM_CLOSE message.

There are other options like the Files in use extension and PSVince

Also see this article for a bit more information.

季末如歌 2024-12-19 05:41:05

一个不太复杂的方法是运行一个使用任务终止的 批处理文件

即:如果您想在卸载之前杀死记事本

taskkill /IM notepad.exe

A less complicated method would be to run a batch file which uses task kill

ie: if you wanted to kill notepad before uninstalling

taskkill /IM notepad.exe
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文