我有一个带有Pascal代码的安装脚本,以确定要安装的应用程序当前是否正在运行:
; 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: .ReleaseMyApp.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%的情况下都有效,但用户偶尔会报告误报,无法继续安装。
用户报告在命令行tasklist /FI "IMAGENAME eq MyApp.exe" /FO CSV
(Pascal脚本使用的)中没有返回任何内容。
脚本中是否存在错误,可能会给出误报,或者是否有更好的方法来确定应用程序是否正在运行,而不是tasklist
?
脚本中是否存在可能给出误报的错误?
没有错误。
你知道,tasklist
可能不可用吗?想想"XP Home"(是的,它正在淡出),但仍然在使用,您的解决方案将无法在那里工作,因为tasklist
根本不可用。
或者是否有比"任务列表"更好的方法来确定应用程序是否正在运行?
是的,还有其他一些可能更可靠的方法来做到这一点。例如,在安装程序中包含psvince
并使用它进行进程检测是很常见的。基于WMI
的解决方案也相当不错。
下面是一些使用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
- 不含外部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