本文介绍了如何确定执行InnoSetup安装脚本之前是否正在运行应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有Pascal代码的安装脚本,用于确定要安装的应用程序当前是否正在运行:

I have an install script with Pascal code to determine if the app to be installed is currently running:

; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!

[Setup]
AppName=MyApp
AppVerName=MyApp v1.0
DiskSpanning=no
AppPublisher=me
AppPublisherURL=http://www.example.com
AppSupportURL=http://www.example.com
AppUpdatesURL=http://www.example.com
DefaultDirName={pf}\MyApp
UsePreviousAppDir=yes
DefaultGroupName=MyApp
OutputBaseFilename=Setup
OutputDir=.\MyAppSetup
MinVersion=5.0

[Tasks]
Name: desktopicon; Description: Create a &desktop icon; GroupDescription: Additional icons:; MinVersion: 4,4

[Files]
Source: .\Release\MyApp.exe; DestDir: {app}; Flags: ignoreversion

[Icons]
Name: {group}\EasyCash&Tax; Filename: {app}\MyApp.exe
Name: {userdesktop}\EasyCash&Tax; Filename: {app}\MyApp.exe; MinVersion: 4,4; Tasks: desktopicon

[Run]
Filename: {app}\MyApp.exe; Description: Launch MyApp; Flags: nowait postinstall skipifsilent

[Code]

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 active and must be closed to proceed',
                 mbConfirmation,
                 MB_OKCANCEL ) <> IDOK ) 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}
  Result := not ( CheckProcessRunning( 'MyApp.exe',      'MyApp' ));
end;


function InitializeUninstall: Boolean;
begin
  Result := not ( CheckProcessRunning( 'MyApp.exe',      'MyApp' ));
end;

此方法适用于99%的情况,但用户有时会报告误报,并且无法继续进行安装.

This works for 99% of the cases but every now and then users report a false positive and are unable to proceed with installation.

用户报告在命令行tasklist /FI "IMAGENAME eq MyApp.exe" /FO CSV(由Pascal脚本使用)中没有返回任何内容.

Users report that in command line tasklist /FI "IMAGENAME eq MyApp.exe" /FO CSV (which is used by the Pascal script) is returning nothing.

脚本中是否存在错误,可能会导致误报,或者是否有比tasklist更好的方法来确定应用程序是否正在运行?

Is there an error in the script that may give false positives or is there a better way to determine if the app is running than tasklist?

推荐答案

没有错误.

您是否知道tasklist可能不可用?考虑一下"XP Home"(是的,它正在逐渐消失),但仍在使用,并且您的解决方案在那儿将无法使用,因为tasklist根本不可用.

Are you aware, that tasklist might not be available? Think of "XP Home" (yes, it's fading out), but still in use and your solution will not work there, because tasklist is simply not available.

是的,还有其他一些也许更可靠的方法可以做到这一点.例如,在安装程序中包含psvince并将其用于过程检测是很常见的.很好的也是基于WMI的解决方案.

Yes, there are some other and maybe more reliable ways to do this. For instance, it's quite common to include psvince in the installer and use it for process detection. Quite nice is also the WMI based solution.

以下是使用InnoSetup进行过程检测"的一些方法:

Here are some approaches for "process detection" with InnoSetup:

  • AppMutex - http://www.jrsoftware.org/ishelp/index.php?topic=setup_appmutex
  • PSVince http://www.vincenzo.net/isxkb/index.php?title=PSVince
  • PSVince Fork https://github.com/XhmikosR/psvince
  • without external DLL https://raw.githubusercontent.com/git-for-windows/build-extra/master/installer/modules.inc.iss
  • WMI + Win32_Process https://stackoverflow.com/a/9950718/1163786 + https://stackoverflow.com/a/25518046/1163786
  • ProcessViewer https://github.com/lextm/processviewer

这篇关于如何确定执行InnoSetup安装脚本之前是否正在运行应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 05:50