[Tasks]
Name: "RestartPC"; Description: "Restart the Computer"; GroupDescription: "Post Installation Options"; Flags: unchecked;
[Code]
function NeedRestart(): Boolean;
begin
if IsTaskSelected('RestartPC') then
Result := False;
end;
procedure CurStepChanged(CurStep: TSetupStep);
var
intResultCode: Integer;
begin
if CurStep = ssPostInstall then
if IsTaskSelected('RestartPC') then
NeedRestart;
if CurStep = ssDone then
begin
if IsTaskSelected('RestartPC') then
if SuppressibleMsgBox('A restart was selected, or Setup must restart your computer to complete the installation.' + #13#10 + #13#10 +
'Would you like to restart now?',
mbConfirmation, MB_YESNO, IDYES) = IDYES then
Exec('shutdown.exe', '-r -t 0', '', SW_HIDE,
ewNoWait, intResultCode);
end;
end;
完整代码如下
; 脚本由 Inno Setup 脚本向导 生成!
; 有关创建 Inno Setup 脚本文件的详细资料请查阅帮助文档!
#define MyAppName "访问系统"
#define MyAppVersion "3.1.0"
#define MyAppPublisher "所"
#define MyAppExeName "wapp.exe"
[Setup]
; 注: AppId的值为单独标识该应用程序。
; 不要为其他安装程序使用相同的AppId值。
; (若要生成新的 GUID,可在菜单中点击 "工具|生成 GUID"。)
AppId={{1FCF8430-B8DA-4C6D-9C4D-50D94C50F015}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
DefaultDirName=C:\Program Files (x86)\{#MyAppName}
DisableProgramGroupPage=yes
; 以下行取消注释,以在非管理安装模式下运行(仅为当前用户安装)。
;PrivilegesRequired=lowest
OutputDir=setup
OutputBaseFilename={#MyAppName}{#MyAppVersion}
SetupIconFile=..\..\..\picture\logo_hz.ico
Compression=lzma
SolidCompression=yes
WizardStyle=modern
[Languages]
Name: "chinesesimp"; MessagesFile: "compiler:Default.isl"
[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked
Name: "RestartPC"; Description: "Restart the Computer"; GroupDescription: "Post Installation Options"; Flags: unchecked;
[Code]
function NeedRestart(): Boolean;
begin
if IsTaskSelected('RestartPC') then
Result := False;
end;
procedure CurStepChanged(CurStep: TSetupStep);
var
intResultCode: Integer;
begin
if CurStep = ssPostInstall then
if IsTaskSelected('RestartPC') then
NeedRestart;
if CurStep = ssDone then
begin
if IsTaskSelected('RestartPC') then
if SuppressibleMsgBox('A restart was selected, or Setup must restart your computer to complete the installation.' + #13#10 + #13#10 +
'Would you like to restart now?',
mbConfirmation, MB_YESNO, IDYES) = IDYES then
Exec('shutdown.exe', '-r -t 0', '', SW_HIDE,
ewNoWait, intResultCode);
end;
end;
[Files]
Source: "..\..\..\build\msvc\run\wapp.exe"; DestDir: "{app}/run"; Flags: ignoreversion
Source: "package\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs
; 注意: 不要在任何共享系统文件上使用“Flags: ignoreversion”
[Icons]
Name: "{autoprograms}\{#MyAppName}"; Filename: "{app}\run\{#MyAppExeName}"
Name: "{autodesktop}\{#MyAppName}"; Filename: "{app}\run\{#MyAppExeName}"; Tasks: desktopicon
[Run]
Filename: "{app}\run\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent
[Code]
// 自定义函数,判断软件是否运行,参数为需要判断的软件的exe名称
function KDetectSoft(strExeName: String): Boolean;
// 变量定义
var ErrorCode: Integer;
var bRes: Boolean;
var strFileContent: AnsiString;
var strTmpPath: String; // 临时目录
var strTmpFile: String; // 临时文件,保存查找软件数据结果
var strCmdFind: String; // 查找软件命令
var strCmdKill: String; // 终止软件命令
begin
strTmpPath := GetTempDir();
strTmpFile := Format('%sfindSoftRes.txt', [strTmpPath]);
strCmdFind := Format('/c tasklist /nh|find /c /i "%s" > "%s"', [strExeName, strTmpFile]);
strCmdKill := Format('/c taskkill /f /t /im %s', [strExeName]);
//ShellExec('open', ExpandConstant('{cmd}'), '/c taskkill /f /t /im 你的软件名.exe', '', SW_HIDE, ewNoWait, ErrorCode);
//bRes := ShellExec('open', ExpandConstant('{cmd}'), '/c tasklist /nh|find /c /i "你的软件名.exe" > 0.txt', '', SW_HIDE, ewWaitUntilTerminated, ErrorCode);
bRes := ShellExec('open', ExpandConstant('{cmd}'), strCmdFind, '', SW_HIDE, ewWaitUntilTerminated, ErrorCode);
if bRes then begin
bRes := LoadStringFromFile(strTmpFile, strFileContent);
strFileContent := Trim(strFileContent);
if bRes then begin
if StrToInt(strFileContent) > 0 then begin
if MsgBox(ExpandConstant('{cm:checkSoftTip}'), mbConfirmation, MB_OKCANCEL) = IDOK then begin
// 终止程序
ShellExec('open', ExpandConstant('{cmd}'), strCmdKill, '', SW_HIDE, ewNoWait, ErrorCode);
Result:= true;// 继续安装
end else begin
Result:= false;// 安装程序退出
Exit;
end;
end else begin
//MsgBox('软件没在运行', mbInformation, MB_OK);
Result:= true;
Exit;
end;
end;
end;
Result :=true;
end;
// 开始页下一步时判断软件是否运行
function NextButtonClick(CurPageID: Integer): Boolean;
begin
if 1=CurPageID then begin
Result := KDetectSoft('wapp.exe');
Exit;
end;
Result:= true;
end;
// 卸载时关闭软件
function InitializeUninstall(): Boolean;
begin
Result := KDetectSoft('wapp.exe');
end;
[Setup]
;no为不重启Yes为重启
AlwaysRestart=yes
// 自定义不同语言文本
[CustomMessages]
chinesesimp.checkSoftTip=卸载程序检测到即将卸载的软件正在运行!%n%n点击"确定"终止软件后继续卸载,否则点击"取消"。