; 脚本由 Inno Setup 脚本向导 生成！
; 有关创建 Inno Setup 脚本文件的详细资料请查阅帮助文档！

#define MyAppName "星火微课"  
#define MyAppDir "xhwk"
#define MyAppVersion "3.2.0"
#define MyAppPublisher "河南星火燎原软件科技有限公司"
#define MyAppURL "http://www.xhkjedu.com/"
#define MySourcePath "D:\Project\CSharp\xhwkclient\XHWK.WKTool\bin\x86\Debug\"
#define MyAppExeName "星火微课.exe"
#define MyIcoName "256.ico"
#define MyTargetPath "D:\程序打包\星火微课-测试\"

[Setup]
; 注: AppId的值为单独标识该应用程序。
; 不要为其他安装程序使用相同的AppId值。
; (若要生成新的 GUID，可在菜单中点击 "工具|生成 GUID"。)
AppId={{5869900A-3BE4-463E-B0A6-462AA7454AE2}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={autopf}\{#MyAppDir}
DisableProgramGroupPage=yes
; [Icons] 的“quicklaunchicon”条目使用 {userappdata}，而其 [Tasks] 条目具有适合 IsAdminInstallMode 的检查。
UsedUserAreasWarning=no
; 以下行取消注释，以在非管理安装模式下运行（仅为当前用户安装）。
;PrivilegesRequired=lowest
OutputDir={#MyTargetPath}
OutputBaseFilename="{#MyAppName}v{#MyAppVersion}"
SetupIconFile="{#MySourcePath}{#MyIcoName}"
Compression=lzma
SolidCompression=yes
WizardStyle=modern

[Languages]
Name: "chinesesimp"; MessagesFile: "compiler:Default.isl"

[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: checkablealone
Name: "quicklaunchicon"; Description: "{cm:CreateQuickLaunchIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: checkablealone; OnlyBelowVersion: 6.1; Check: not IsAdminInstallMode

[Files]
Source: "{#MySourcePath}{#MyAppExeName}"; DestDir: "{app}"; Flags: ignoreversion
Source: "{#MySourcePath}*"; Excludes: "ffmpeg.exe,\Log\*,\Temp,\Data";DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs

[Icons]
Name: "{autoprograms}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"
Name: "{autodesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon
Name: "{userappdata}\Microsoft\Internet Explorer\Quick Launch\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: quicklaunchicon

[Run]
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent

[Code]  
// 自定义函数，判断软件是否运行，参数为需要判断的软件的exe名称
function CheckSoftRun(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]);
  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
            // 软件没在运行
            Result:= true;
            Exit;
         end;
      end;
  end;
  Result :=true;
end;

// 开始页下一步时判断软件是否运行
function NextButtonClick(CurPageID: Integer): Boolean;
begin
  if 1=CurPageID then begin
      Result := CheckSoftRun('{#MyAppExeName}');
      Exit;
  end; 
  Result:= true;
end;

// 卸载时关闭软件
function InitializeUninstall(): Boolean;
begin
  Result := CheckSoftRun('{#MyAppExeName}');
end;

function CheckVC():boolean;
var Path:string; 
      ResultCode: Integer;  
begin
  if RegValueExists(HKLM, 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{01FAEC41-B3BC-44F4-B185-5E8475AEB855}', 'Version') 
  or RegValueExists(HKLM, 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{77EB1EA9-8E1B-459D-8CDC-1984D0FF15B6}', 'Version') 
  then  
    begin  
      Result := true;   
    end  
  else  
  begin  
    if MsgBox('系统检测到您没有安装VC++环境，是否立刻下载并安装？', mbConfirmation, MB_YESNO) = idYes then  
      begin  
        Path := ExpandConstant('{pf}/Internet Explorer/iexplore.exe');   
        Exec(Path, 'https://xhkjedu.oss-cn-huhehaote.aliyuncs.com/runtime/vc_redist.x86.exe', '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode);   
        MsgBox('请安装好VC++环境后，再运行本安装包程序！',mbInformation,MB_OK);   
        Result := false;   
      end  
    else  
      begin  
        MsgBox('取消后请手动安装VC2015-2019环境！',mbInformation,MB_OK);   
        Result := true;   
      end;   
  end;
end;


function IsInstallDotNet(version: string; service: cardinal): boolean;
var
    key, versionKey: string;
    install, release, serviceCount, versionRelease: cardinal;
    success: boolean;
begin
    versionKey := version;
    versionRelease := 0;

    // .NET 1.1 and 2.0 embed release number in version key
    if version = 'v1.1' then begin
        versionKey := 'v1.1.4322';
    end
    else if version = 'v2.0' then begin
        versionKey := 'v2.0.50727';
    end

    // .NET 4.5 and newer install as update to .NET 4.0 Full
    else if Pos('v4.', version) = 1 then begin
        versionKey := 'v4\Full';
        case version of
          'v4.5':   versionRelease := 378389;
          'v4.5.1': versionRelease := 378675; // 378758 on Windows 8 and older
          'v4.5.2': versionRelease := 379893;
          'v4.6':   versionRelease := 393295; // 393297 on Windows 8.1 and older
          'v4.6.1': versionRelease := 394254; // 394271 on Windows 8.1 and older
          'v4.6.2': versionRelease := 394802; // 394806 on Windows 8.1 and older
          'v4.7':   versionRelease := 460798; // Windows 10
          'v4.7.1': versionRelease := 461308; // Windows 10
          'v4.7.2': versionRelease := 461808; // Windows 10
          'v4.8'  : versionRelease := 528040; // Windows 10
        end;
    end;

    // installation key group for all .NET versions
    key := 'SOFTWARE\Microsoft\NET Framework Setup\NDP\' + versionKey;

    // .NET 3.0 uses value InstallSuccess in subkey Setup
    if Pos('v3.0', version) = 1 then begin
        success := RegQueryDWordValue(HKLM, key + '\Setup', 'InstallSuccess', install);
    end else begin
        success := RegQueryDWordValue(HKLM, key, 'Install', install);
    end;

    // .NET 4.0 and newer use value Servicing instead of SP
    if Pos('v4', version) = 1 then begin
        success := success and RegQueryDWordValue(HKLM, key, 'Servicing', serviceCount);
    end else begin
        success := success and RegQueryDWordValue(HKLM, key, 'SP', serviceCount);
    end;

    // .NET 4.5 and newer use additional value Release
    if versionRelease > 0 then begin
        success := success and RegQueryDWordValue(HKLM, key, 'Release', release);
        success := success and (release >= versionRelease);
    end;

    result := success and (install = 1) and (serviceCount >= service);
end;

// 检查.Net是否安装
function CheckDotNet : Boolean;
var Path:string;   
    ResultCode: Integer;   
begin  
  if IsInstallDotNet('v4.5.2', 0) then  
    begin  
        Result := true; 
    end  
  else  
    begin  
      if MsgBox('系统检测到您没有安装.Net Framework4.5.2，是否立刻下载并安装？', mbConfirmation, MB_YESNO) = idYes then  
        begin  
          Path := ExpandConstant('{pf}/Internet Explorer/iexplore.exe');   
          Exec(Path, 'https://xhkjedu.oss-cn-huhehaote.aliyuncs.com/runtime/NDP452-KB2901907-x86-x64-AllOS-ENU.exe', '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode);   
          MsgBox('请安装好.Net Framework环境后，再运行本安装包程序！',mbInformation,MB_OK);   
          Result := false;   
        end  
      else  
        begin  
          MsgBox('没有安装.Net Framework环境，无法运行程序，本安装程序即将退出！',mbInformation,MB_OK);   
          Result := false;   
        end;   
    end;
end;

function InitializeSetup: Boolean;
begin
  Result := CheckDotNet();
  Result := CheckVC();
end; 


[CustomMessages]
chinesesimp.checkSoftTip=安装程序检测到将安装的软件正在运行！%n%n点击"确定"终止软件后继续操作，否则点击"取消"。