; ============================================================================== ; ClassNotify Windows Installer - Inno Setup 7 Native Packaging ; Version: 2.1.0 (P2.1 Size Optimization) ; Engine: Inno Setup 7 Native Windows Bootstrap + Solid LZMA2 Compression ; ============================================================================== #define MyAppName "ClassNotify" #define MyAppDisplayName "ClassNotify 班级大屏端" #define MyAppPublisher "ClassNotify Inc." #define MyAppVersion "2.2.0" #define MyAppId "{{A78C53E6-9218-47A6-9E9C-D5E75069B791}" #ifndef StageRoot #define StageRoot "staging" #endif #ifndef OutputDir #define OutputDir "output" #endif [Setup] AppId={#MyAppId} AppName={#MyAppDisplayName} AppVersion={#MyAppVersion} AppVerName={#MyAppDisplayName} {#MyAppVersion} AppPublisher={#MyAppPublisher} DefaultDirName={autopf}\ClassNotify DisableProgramGroupPage=yes PrivilegesRequired=admin SetupArchitecture=x64 ArchitecturesAllowed=x64compatible ArchitecturesInstallIn64BitMode=x64compatible OutputDir={#OutputDir} OutputBaseFilename=ClassNotifySetup ; High compression settings - LZMA2 with solid compression deduplicates identical runtime DLLs Compression=lzma2/max SolidCompression=yes LZMANumBlockThreads=1 ; Custom service & client lifecycle management CloseApplications=no RestartApplications=no UsePreviousAppDir=yes Uninstallable=yes UninstallLogging=yes WizardStyle=modern SetupIconFile=app.ico UninstallIconFile=app.ico [Tasks] Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}" [Files] ; Private payload directories - sortfilesbyextension groups identical DLLs together for optimal solid compression Source: "{#StageRoot}\Service\*"; DestDir: "{app}\Service"; Flags: ignoreversion recursesubdirs createallsubdirs sortfilesbyextension Source: "{#StageRoot}\Client\*"; DestDir: "{app}\Client"; Flags: ignoreversion recursesubdirs createallsubdirs sortfilesbyextension [Icons] Name: "{autoprograms}\ClassNotify"; Filename: "{app}\Client\ClassNotify.Client.exe"; Parameters: "--show"; IconFilename: "{app}\Client\ClassNotify.Client.exe" Name: "{autodesktop}\ClassNotify"; Filename: "{app}\Client\ClassNotify.Client.exe"; Parameters: "--show"; IconFilename: "{app}\Client\ClassNotify.Client.exe"; Tasks: desktopicon [Run] Filename: "{app}\Client\ClassNotify.Client.exe"; Parameters: "--show"; Flags: nowait runasoriginaluser [Code] const ServiceName = 'ClassNotifyService'; ServiceProcess = 'ClassNotify.Service.exe'; ClientProcess = 'ClassNotify.Client.exe'; function RunCapture( const FileName, Params: String; var ExitCode: Integer; var Text: String ): Boolean; var O: TExecOutput; I: Integer; begin Text := ''; try Result := ExecAndCaptureOutput( FileName, Params, '', SW_HIDE, ewWaitUntilTerminated, ExitCode, O ); for I := 0 to GetArrayLength(O.StdOut) - 1 do Text := Text + O.StdOut[I] + #13#10; for I := 0 to GetArrayLength(O.StdErr) - 1 do Text := Text + O.StdErr[I] + #13#10; except Log('Exception in RunCapture: ' + GetExceptionMessage); Result := False; end; end; function ServiceExists(): Boolean; begin Result := RegKeyExists( HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Services\' + ServiceName ); end; function QueryServiceState(): String; var Code: Integer; Text: String; begin Result := 'UNKNOWN'; if not ServiceExists() then begin Result := 'NOT_INSTALLED'; Exit; end; if RunCapture( ExpandConstant('{sys}\sc.exe'), 'query "' + ServiceName + '"', Code, Text) then begin Text := UpperCase(Text); if Pos('RUNNING', Text) > 0 then Result := 'RUNNING' else if Pos('STOPPED', Text) > 0 then Result := 'STOPPED' else if Pos('STOP_PENDING', Text) > 0 then Result := 'STOP_PENDING' else if Pos('START_PENDING', Text) > 0 then Result := 'START_PENDING'; end; end; function WaitForServiceState( const Desired: String; TimeoutSeconds: Integer ): Boolean; var I, MaxIterations: Integer; begin MaxIterations := TimeoutSeconds * 2; for I := 1 to MaxIterations do begin if QueryServiceState() = Desired then begin Result := True; Exit; end; Sleep(500); end; Result := False; end; function StopServiceFailClosed(): Boolean; var Code: Integer; Text: String; begin Result := True; if not ServiceExists() then Exit; if QueryServiceState() = 'STOPPED' then Exit; Log('Stopping ' + ServiceName + ' before file replacement...'); RunCapture( ExpandConstant('{sys}\sc.exe'), 'stop "' + ServiceName + '"', Code, Text ); if not WaitForServiceState('STOPPED', 30) then begin Log('ERROR: ClassNotifyService failed to stop within 30 seconds.'); Result := False; end; end; function ClientIsRunning(): Boolean; var Code: Integer; Text: String; RanOk: Boolean; begin RanOk := RunCapture( ExpandConstant('{sys}\tasklist.exe'), '/NH /FI "IMAGENAME eq ' + ClientProcess + '"', Code, Text ); if (not RanOk) or (Code <> 0) then begin Log('WARNING: tasklist query failed (RanOk=' + IntToStr(Ord(RanOk)) + ', ExitCode=' + IntToStr(Code) + '). Assuming client may still be running (fail-closed).'); Result := True; Exit; end; Result := Pos(UpperCase(ClientProcess), UpperCase(Text)) > 0; end; function StopClientFailClosed(): Boolean; var Code: Integer; Text: String; I: Integer; begin Result := True; if not ClientIsRunning() then Exit; Log('Stopping ' + ClientProcess + '...'); RunCapture( ExpandConstant('{sys}\taskkill.exe'), '/F /IM "' + ClientProcess + '"', Code, Text ); for I := 1 to 40 do begin if not ClientIsRunning() then Exit; Sleep(250); end; Result := False; end; function PrepareToInstall(var NeedsRestart: Boolean): String; begin NeedsRestart := False; Result := ''; { Step 1: Quiesce service first so watchdog stops re-spawning client } if not StopServiceFailClosed() then begin Result := 'ClassNotify 服务无法安全停止。' + #13#10 + '为防止核心程序文件在运行中被覆盖,安装已中止。'; Exit; end; { Step 2: Terminate client process } if not StopClientFailClosed() then begin Result := 'ClassNotify 客户端仍在运行且无法终止。' + #13#10 + '核心文件尚未被替换,安装已安全中止。'; Exit; end; end; procedure CleanupLegacyArtifacts(); var OldUninstallExe: String; begin { Clean up legacy 168 MB uninstaller left by P1 .NET bootstrapper } OldUninstallExe := ExpandConstant('{app}\Uninstall.exe'); if FileExists(OldUninstallExe) then begin Log('Removing legacy 168MB uninstaller: ' + OldUninstallExe); DeleteFile(OldUninstallExe); end; { Clean up legacy P1 registry entry so Windows Installed Apps does not show duplicates } if RegKeyExists(HKEY_LOCAL_MACHINE, 'Software\Microsoft\Windows\CurrentVersion\Uninstall\ClassNotify') then begin Log('Cleaning legacy HKLM Uninstall\ClassNotify registry key...'); RegDeleteKeyIncludingSubkeys(HKEY_LOCAL_MACHINE, 'Software\Microsoft\Windows\CurrentVersion\Uninstall\ClassNotify'); end; end; function ConfigureAndStartService(): Boolean; var ServiceExePath: String; Code: Integer; Text: String; Params: String; begin Result := False; ServiceExePath := ExpandConstant('{app}\Service\ClassNotify.Service.exe'); if ServiceExists() then begin Log('Updating existing service configuration...'); Params := 'config "' + ServiceName + '"' + ' binPath= "\"' + ServiceExePath + '\""' + ' start= auto' + ' obj= LocalSystem' + ' DisplayName= "ClassNotify Classroom Service"'; end else begin Log('Creating new service configuration...'); Params := 'create "' + ServiceName + '"' + ' binPath= "\"' + ServiceExePath + '\""' + ' start= auto' + ' obj= LocalSystem' + ' DisplayName= "ClassNotify Classroom Service"'; end; if not RunCapture(ExpandConstant('{sys}\sc.exe'), Params, Code, Text) then begin Log('Failed to execute sc.exe for service configuration.'); Exit; end; if Code <> 0 then begin Log('sc.exe returned error: ' + Text); Exit; end; { Configure SCM Failure Recovery to exact P1 baseline: 5s / 10s / 60s } Log('Configuring SCM recovery failure actions...'); if (not RunCapture( ExpandConstant('{sys}\sc.exe'), 'failure "' + ServiceName + '" reset= 86400 actions= restart/5000/restart/10000/restart/60000', Code, Text)) or (Code <> 0) then begin Log('ERROR: Failed to configure SCM failure actions: ' + Text); Exit; end; if (not RunCapture( ExpandConstant('{sys}\sc.exe'), 'failureflag "' + ServiceName + '" 0', Code, Text)) or (Code <> 0) then begin Log('ERROR: Failed to configure SCM failureflag: ' + Text); Exit; end; { Start Service } Log('Starting service ' + ServiceName + '...'); if (not RunCapture( ExpandConstant('{sys}\sc.exe'), 'start "' + ServiceName + '"', Code, Text)) or (Code <> 0) then begin Log('ERROR: Failed to issue start command for service: ' + Text); Exit; end; if not WaitForServiceState('RUNNING', 30) then begin Log('ERROR: ClassNotifyService failed to reach RUNNING state within 30 seconds.'); Exit; end; Result := True; end; procedure CurStepChanged(CurStep: TSetupStep); begin if CurStep = ssPostInstall then begin if not ConfigureAndStartService() then begin RaiseException('ClassNotify 服务未能成功配置或启动,请检查系统事件查看器。'); end; { Clean up legacy artifacts only after new service is confirmed running } CleanupLegacyArtifacts(); { Note: Installer does NOT launch Client.exe directly! } { Service automatically detects interactive user session and spawns Client.exe with user privileges via WTS. } end; end; procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep); var Code: Integer; Text: String; begin if CurUninstallStep = usUninstall then begin Log('Stopping and deleting service during uninstallation...'); RunCapture(ExpandConstant('{sys}\sc.exe'), 'stop "' + ServiceName + '"', Code, Text); if not WaitForServiceState('STOPPED', 15) then begin Log('WARNING: Service did not reach STOPPED state within 15 seconds during uninstall.'); end; RunCapture(ExpandConstant('{sys}\sc.exe'), 'delete "' + ServiceName + '"', Code, Text); RunCapture(ExpandConstant('{sys}\taskkill.exe'), '/F /IM "' + ClientProcess + '"', Code, Text); end; end;