SetupLdr.dpr 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. program SetupLdr;
  2. {
  3. Inno Setup
  4. Copyright (C) 1997-2025 Jordan Russell
  5. Portions by Martijn Laan
  6. For conditions of distribution and use, see LICENSE.TXT.
  7. Setup Loader
  8. }
  9. uses
  10. SafeDLLPath in '..\Components\SafeDLLPath.pas',
  11. SetupLdrAndSetup.XPTheme in 'Src\SetupLdrAndSetup.XPTheme.pas',
  12. Windows,
  13. Messages,
  14. SysUtils,
  15. Compression.Base in 'Src\Compression.Base.pas',
  16. Compression.LZMA1SmallDecompressor in 'Src\Compression.LZMA1SmallDecompressor.pas',
  17. Shared.SetupEntFunc in 'Src\Shared.SetupEntFunc.pas',
  18. PathFunc in '..\Components\PathFunc.pas',
  19. Shared.CommonFunc in 'Src\Shared.CommonFunc.pas',
  20. SetupLdrAndSetup.Messages in 'Src\SetupLdrAndSetup.Messages.pas',
  21. Shared.SetupMessageIDs in 'Src\Shared.SetupMessageIDs.pas',
  22. Shared.Struct in 'Src\Shared.Struct.pas',
  23. SetupLdrAndSetup.InstFunc in 'Src\SetupLdrAndSetup.InstFunc.pas',
  24. Shared.FileClass in 'Src\Shared.FileClass.pas',
  25. SHA256 in '..\Components\SHA256.pas',
  26. Shared.VerInfoFunc in 'Src\Shared.VerInfoFunc.pas',
  27. Shared.EncryptionFunc in 'Src\Shared.EncryptionFunc.pas',
  28. ChaCha20 in '..\Components\ChaCha20.pas',
  29. PBKDF2 in '..\Components\PBKDF2.pas',
  30. UnsignedFunc in '..\Components\UnsignedFunc.pas';
  31. {$SETPEOSVERSION 6.1}
  32. {$SETPESUBSYSVERSION 6.1}
  33. {$WEAKLINKRTTI ON}
  34. { The compiler may delete one of the icons included here }
  35. {$R Res\Setup.icon.res}
  36. {$R Res\Setup.icon.dark.res}
  37. {$R Res\SetupLdr.version.res}
  38. {$R Res\SetupLdr.offsettable.res}
  39. const
  40. { Exit codes that are returned by SetupLdr.
  41. Note: Setup also returns exit codes with the same numbers. }
  42. ecInitializationError = 1; { Setup failed to initialize. }
  43. ecCancelledBeforeInstall = 2; { User clicked Cancel before the actual
  44. installation started. }
  45. type
  46. PLanguageEntryArray = ^TLanguageEntryArray;
  47. TLanguageEntryArray = array[0..999999] of TSetupLanguageEntry;
  48. var
  49. InitShowHelp: Boolean = False;
  50. InitDisableStartupPrompt: Boolean = False;
  51. InitSuppressMsgBoxes: Boolean = False;
  52. InitLang, InitPassword: String;
  53. ActiveLanguage: Integer = -1;
  54. PendingNewLanguage: Integer = -1;
  55. SetupEncryptionHeader: TSetupEncryptionHeader;
  56. SetupHeader: TSetupHeader;
  57. LanguageEntries: PLanguageEntryArray;
  58. LanguageEntryCount: Integer;
  59. SetupLdrExitCode: Integer = ecInitializationError;
  60. SetupLdrWnd: HWND = 0;
  61. OrigWndProc: Pointer;
  62. RestartSystem: Boolean = False;
  63. procedure RaiseLastError(const Msg: TSetupMessageID);
  64. var
  65. ErrorCode: DWORD;
  66. begin
  67. ErrorCode := GetLastError;
  68. raise Exception.Create(FmtSetupMessage(msgLastErrorMessage,
  69. [SetupMessages[Msg], IntToStr(ErrorCode), Win32ErrorString(ErrorCode)]));
  70. end;
  71. procedure ShowExceptionMsg;
  72. begin
  73. if ExceptObject is EAbort then
  74. Exit;
  75. if not InitSuppressMsgBoxes then
  76. MessageBox(0, PChar(GetExceptMessage), Pointer(SetupMessages[msgErrorTitle]),
  77. MB_OK or MB_ICONSTOP);
  78. { ^ use a Pointer cast instead of a PChar cast so that it will use "nil"
  79. if SetupMessages[msgErrorTitle] is empty due to the messages not being
  80. loaded yet. MessageBox displays 'Error' as the caption if the lpCaption
  81. parameter is nil. }
  82. end;
  83. procedure ProcessCommandLine(var SelfFilename: String);
  84. begin
  85. var SilentOrVerySilent := False;
  86. var WantToSuppressMsgBoxes := False;
  87. for var I := 1 to NewParamCount do begin
  88. var ParamName, ParamValue: String;
  89. SplitNewParamStr(I, ParamName, ParamValue);
  90. if SameText(ParamName, '/SP-') or SameText(ParamName, '/SPAWNWND=') then
  91. InitDisableStartupPrompt := True
  92. else if SameText(ParamName, '/Lang=') then
  93. InitLang := ParamValue
  94. else if SameText(ParamName, '/Password=') then
  95. InitPassword := ParamValue
  96. else if SameText(ParamName, '/HELP') or SameText(ParamName, '/?') then
  97. InitShowHelp := True
  98. else if SameText(ParamName, '/Silent') or SameText(ParamName, '/VerySilent') then
  99. SilentOrVerySilent := True
  100. else if SameText(ParamName, '/SuppressMsgBoxes') then
  101. WantToSuppressMsgBoxes := True
  102. {$IFDEF DEBUG}
  103. else if SameText(ParamName, '/SELFFILENAME=') then
  104. SelfFilename := PathExpand(ParamValue);
  105. {$ENDIF};
  106. end;
  107. if WantToSuppressMsgBoxes and SilentOrVerySilent then
  108. InitSuppressMsgBoxes := True;
  109. end;
  110. procedure SetActiveLanguage(const I: Integer);
  111. { Activates the specified language }
  112. begin
  113. if (I >= 0) and (I < LanguageEntryCount) and (I <> ActiveLanguage) then begin
  114. AssignSetupMessages(LanguageEntries[I].Data[1], ULength(LanguageEntries[I].Data));
  115. ActiveLanguage := I;
  116. end;
  117. end;
  118. function GetLanguageEntryProc(Index: Integer; var Entry: PSetupLanguageEntry): Boolean;
  119. begin
  120. Result := False;
  121. if Index < LanguageEntryCount then begin
  122. Entry := @LanguageEntries[Index];
  123. Result := True;
  124. end;
  125. end;
  126. procedure ActivateDefaultLanguage;
  127. { Auto-detects the most appropriate language and activates it.
  128. Note: A like-named version of this function is also present in Main.pas. }
  129. var
  130. I: Integer;
  131. begin
  132. DetermineDefaultLanguage(GetLanguageEntryProc, SetupHeader.LanguageDetectionMethod,
  133. InitLang, I);
  134. SetActiveLanguage(I);
  135. end;
  136. function SetupLdrWndProc(Wnd: HWND; Msg: UINT; WParam: WPARAM; LParam: LPARAM): LRESULT;
  137. stdcall;
  138. begin
  139. Result := 0;
  140. case Msg of
  141. WM_QUERYENDSESSION: begin
  142. { Return zero so that a shutdown attempt can't kill SetupLdr }
  143. end;
  144. WM_USER + 150: begin
  145. if WParam = 10000 then begin
  146. { Setup wants SetupLdr to restart the computer before it exits }
  147. RestartSystem := True;
  148. end
  149. else if WParam = 10001 then begin
  150. { Setup wants SetupLdr to change its active language }
  151. PendingNewLanguage := Integer(LParam);
  152. end;
  153. end;
  154. else
  155. Result := CallWindowProc(OrigWndProc, Wnd, Msg, WParam, LParam);
  156. end;
  157. end;
  158. procedure ProcessMessages;
  159. var
  160. Msg: TMsg;
  161. begin
  162. while PeekMessage(Msg, 0, 0, 0, PM_REMOVE) do begin
  163. TranslateMessage(Msg);
  164. DispatchMessage(Msg);
  165. end;
  166. end;
  167. procedure ExecAndWait(const Filename, Parms: String; var ExitCode: Integer);
  168. var
  169. CmdLine: String;
  170. StartupInfo: TStartupInfo;
  171. ProcessInfo: TProcessInformation;
  172. begin
  173. CmdLine := '"' + Filename + '" ' + Parms;
  174. { Pass current directory in "final" reparsed form so that Setup won't have
  175. trouble accessing the directory after enabling RedirectionGuard if there's
  176. an untrusted redirect in the path. }
  177. const WorkingDir = GetFinalCurrentDir;
  178. FillChar(StartupInfo, SizeOf(StartupInfo), 0);
  179. StartupInfo.cb := SizeOf(StartupInfo);
  180. if not CreateProcess(nil, PChar(CmdLine), nil, nil, False, 0, nil,
  181. PChar(WorkingDir), StartupInfo, ProcessInfo) then
  182. RaiseLastError(msgLdrCannotExecTemp);
  183. CloseHandle(ProcessInfo.hThread);
  184. { Wait for the process to terminate, processing messages in the meantime }
  185. repeat
  186. ProcessMessages;
  187. until MsgWaitForMultipleObjects(1, ProcessInfo.hProcess, False, INFINITE,
  188. QS_ALLINPUT) <> WAIT_OBJECT_0+1;
  189. { Now that the process has exited, process any remaining messages.
  190. (There may be an asynchronously-sent "restart request" message still
  191. queued if MWFMO saw the process terminate before checking for new
  192. messages.) }
  193. ProcessMessages;
  194. GetExitCodeProcess(ProcessInfo.hProcess, DWORD(ExitCode));
  195. CloseHandle(ProcessInfo.hProcess);
  196. end;
  197. procedure SetupCorruptError;
  198. begin
  199. if SetupMessages[msgSetupFileCorrupt] <> '' then
  200. raise Exception.Create(SetupMessages[msgSetupFileCorrupt])
  201. else
  202. { In case the messages haven't been loaded yet, use the constant }
  203. raise Exception.Create(SSetupFileCorrupt);
  204. end;
  205. procedure RunImageLocally(const Module: HMODULE);
  206. { Force all of the specified module to be paged in to ensure subsequent
  207. accesses don't cause the disk image to be read.
  208. Based on code from http://www.microsoft.com/msj/0398/win320398.htm, with
  209. some fixes incorporated. }
  210. procedure Touch(var X: Integer);
  211. begin
  212. { An atomic operation is used to ensure we can't corrupt a simultaneous
  213. write by another thread (though there shouldn't be other threads) }
  214. InterlockedExchangeAdd(X, 0);
  215. end;
  216. var
  217. SysInfo: TSystemInfo;
  218. CurAddr: Pointer;
  219. MemInfo: TMemoryBasicInformation;
  220. ChangedProtection: Boolean;
  221. OrigProtect: DWORD;
  222. begin
  223. { Get system's page size }
  224. GetSystemInfo(SysInfo);
  225. CurAddr := Pointer(Module);
  226. if VirtualQuery(CurAddr, MemInfo, SizeOf(MemInfo)) = 0 then
  227. Exit;
  228. { Perform this loop until we find a region beyond the end of the file }
  229. while MemInfo.AllocationBase = Pointer(Module) do begin
  230. { We can only force committed pages into RAM.
  231. We do not want to trigger guard pages and confuse the application. }
  232. if (MemInfo.State = MEM_COMMIT) and (MemInfo.Protect and PAGE_GUARD = 0) then begin
  233. ChangedProtection := False;
  234. { Determine if the pages in this region are nonwriteable }
  235. if (MemInfo.Protect = PAGE_NOACCESS) or
  236. (MemInfo.Protect = PAGE_READONLY) or
  237. (MemInfo.Protect = PAGE_EXECUTE) or
  238. (MemInfo.Protect = PAGE_EXECUTE_READ) then begin
  239. { Nonwriteable region, make it writeable (with the least protection) }
  240. if VirtualProtect(MemInfo.BaseAddress, MemInfo.RegionSize,
  241. PAGE_EXECUTE_READWRITE, @OrigProtect) then
  242. ChangedProtection := True;
  243. end;
  244. { Write to every page in the region.
  245. This forces the page to be in RAM and swapped to the paging file. }
  246. var Offset: SIZE_T := 0;
  247. while Offset < MemInfo.RegionSize do begin
  248. Touch(PInteger(PByte(MemInfo.BaseAddress) + Offset)^);
  249. Inc(Offset, SysInfo.dwPageSize);
  250. end;
  251. { If we changed the protection, change it back }
  252. if ChangedProtection then
  253. VirtualProtect(MemInfo.BaseAddress, MemInfo.RegionSize, OrigProtect,
  254. @OrigProtect);
  255. end;
  256. { Get next region }
  257. PByte(CurAddr) := PByte(MemInfo.BaseAddress) + MemInfo.RegionSize;
  258. if VirtualQuery(CurAddr, MemInfo, SizeOf(MemInfo)) = 0 then
  259. Break;
  260. end;
  261. end;
  262. function GetSetupLdrOffsetTable(const M: HMODULE): PSetupLdrOffsetTable;
  263. { Locates the offset table resource, and returns a pointer to it }
  264. var
  265. Rsrc: HRSRC;
  266. ResData: HGLOBAL;
  267. begin
  268. Rsrc := FindResource(M, MAKEINTRESOURCE(SetupLdrOffsetTableResID), RT_RCDATA);
  269. if Rsrc = 0 then
  270. SetupCorruptError;
  271. if SizeofResource(M, Rsrc) <> SizeOf(Result^) then
  272. SetupCorruptError;
  273. ResData := LoadResource(M, Rsrc);
  274. if ResData = 0 then
  275. SetupCorruptError;
  276. Result := LockResource(ResData);
  277. if Result = nil then
  278. SetupCorruptError;
  279. end;
  280. procedure ShowHelp(const CustomNote: String);
  281. const
  282. SNewLine = #13#10;
  283. var
  284. PrNote, Help: String;
  285. begin
  286. { do not localize }
  287. if proCommandLine in SetupHeader.PrivilegesRequiredOverridesAllowed then begin
  288. PrNote := '/ALLUSERS' + SNewLine +
  289. 'Instructs Setup to install in administrative install mode.' + SNewLine +
  290. '/CURRENTUSER' + SNewLine +
  291. 'Instructs Setup to install in non administrative install mode.' + SNewLine;
  292. end else
  293. PrNote := '';
  294. Help := 'The Setup program accepts optional command line parameters.' + SNewLine +
  295. SNewLine +
  296. '/HELP, /?' + SNewLine +
  297. 'Shows this information.' + SNewLine +
  298. '/SP-' + SNewLine +
  299. 'Disables the "This will install... Do you wish to continue?" message box at the beginning of Setup.' + SNewLine +
  300. '/SILENT, /VERYSILENT' + SNewLine +
  301. 'Instructs Setup to be silent or very silent.' + SNewLine +
  302. '/NOSTYLE' + SNewLine +
  303. 'Prevents Setup from activating custom styles (including the built-in custom dark style).' + SNewLine +
  304. '/SUPPRESSMSGBOXES' + SNewLine +
  305. 'Instructs Setup to suppress message boxes.' + SNewLine +
  306. '/LOG' + SNewLine +
  307. 'Causes Setup to create a log file in the user''s TEMP directory.' + SNewLine +
  308. '/LOG="filename"' + SNewLine +
  309. 'Same as /LOG, except it allows you to specify a fixed path/filename to use for the log file.' + SNewLine +
  310. '/NOCANCEL' + SNewLine +
  311. 'Prevents the user from cancelling during the installation process.' + SNewLine +
  312. '/NORESTART' + SNewLine +
  313. 'Prevents Setup from restarting the system following a successful installation, or after a Preparing to Install failure that requests a restart.' + SNewLine +
  314. '/RESTARTEXITCODE=exit code' + SNewLine +
  315. 'Specifies a custom exit code that Setup is to return when the system needs to be restarted.' + SNewLine +
  316. '/CLOSEAPPLICATIONS, /NOCLOSEAPPLICATIONS' + SNewLine +
  317. 'Instructs Setup to attempt closing applications using files that need to be updated, or prevents it from doing so.' + SNewLine +
  318. '/FORCECLOSEAPPLICATIONS, /FORCENOCLOSEAPPLICATIONS' + SNewLine +
  319. 'Instructs Setup to force close when closing applications, or prevents it from doing so.' + SNewLine +
  320. '/LOGCLOSEAPPLICATIONS' + SNewLine +
  321. 'Instructs Setup to create extra logging when closing applications for debugging purposes.' + SNewLine +
  322. '/RESTARTAPPLICATIONS, /NORESTARTAPPLICATIONS' + SNewLine +
  323. 'Instructs Setup to attempt restarting applications, or prevents it from doing so.' + SNewLine +
  324. '/REDIRECTIONGUARD, /NOREDIRECTIONGUARD' + SNewLine +
  325. 'Instructs Setup to attempt enabling RedirectionGuard, or prevents it from doing so.' + SNewLine +
  326. '/LOADINF="filename", /SAVEINF="filename"' + SNewLine +
  327. 'Instructs Setup to load the settings from the specified file after having checked the command line, or to save them to it.' + SNewLine +
  328. '/LANG=language' + SNewLine +
  329. 'Specifies the internal name of the language to use.' + SNewLine +
  330. '/DIR="x:\dirname"' + SNewLine +
  331. 'Overrides the default directory name.' + SNewLine +
  332. '/GROUP="folder name"' + SNewLine +
  333. 'Overrides the default folder name.' + SNewLine +
  334. '/NOICONS' + SNewLine +
  335. 'Instructs Setup to initially check the Don''t create a Start Menu folder check box.' + SNewLine +
  336. '/TYPE=type name' + SNewLine +
  337. 'Overrides the default setup type.' + SNewLine +
  338. '/COMPONENTS="comma separated list of component names"' + SNewLine +
  339. 'Overrides the default component settings.' + SNewLine +
  340. '/TASKS="comma separated list of task names"' + SNewLine +
  341. 'Specifies a list of tasks that should be initially selected.' + SNewLine +
  342. '/MERGETASKS="comma separated list of task names"' + SNewLine +
  343. 'Like the /TASKS parameter, except the specified tasks will be merged with the set of tasks that would have otherwise been selected by default.' + SNewLine +
  344. '/PASSWORD=password' + SNewLine +
  345. 'Specifies the password to use.' + SNewLine +
  346. PrNote +
  347. CustomNote +
  348. SNewLine +
  349. 'For more detailed information, please visit https://jrsoftware.org/ishelp/index.php?topic=setupcmdline';
  350. MessageBox(0, PChar(Help), 'Setup', MB_OK or MB_ICONSTOP);
  351. end;
  352. var
  353. SourceF, DestF: TFile;
  354. OffsetTable: TSetupLdrOffsetTable;
  355. TempDir: String;
  356. S: String;
  357. TempFile: String;
  358. TestID: TSetupID;
  359. P: Pointer;
  360. Reader: TCompressedBlockReader;
  361. I: Integer;
  362. begin
  363. try
  364. {$IFDEF DEBUG}
  365. ReportMemoryLeaksOnShutdown := True;
  366. {$ENDIF}
  367. { Ensure all of SetupLdr is paged in so that in the case of a disk spanning
  368. install Windows will never complain when the disk/CD containing SetupLdr
  369. is ejected. }
  370. RunImageLocally(HInstance);
  371. var SelfFilename := NewParamStr(0);
  372. ProcessCommandLine(SelfFilename);
  373. SourceF := TFile.Create(SelfFilename, fdOpenExisting, faRead, fsRead);
  374. try
  375. var M: HMODULE := 0;
  376. if not PathSame(SelfFilename, NewParamStr(0)) then begin { Can only happen on DEBUG }
  377. M := LoadLibraryEx(PChar(SelfFilename), 0, LOAD_LIBRARY_AS_DATAFILE);
  378. if M = 0 then
  379. raise Exception.Create('LoadLibraryEx failed');
  380. end;
  381. try
  382. OffsetTable := GetSetupLdrOffsetTable(M)^;
  383. finally
  384. if M <> 0 then
  385. FreeLibrary(M);
  386. end;
  387. { Note: We don't check the OffsetTable.ID here because it would put a
  388. copy of the ID in the data section, and that would confuse external
  389. programs that search for the offset table by ID. }
  390. if (OffsetTable.Version <> SetupLdrOffsetTableVersion) or
  391. (GetCRC32(OffsetTable, SizeOf(OffsetTable) - SizeOf(OffsetTable.TableCRC)) <> OffsetTable.TableCRC) or
  392. (SourceF.Size < OffsetTable.TotalSize) then
  393. SetupCorruptError;
  394. SourceF.Seek(OffsetTable.Offset0);
  395. SourceF.ReadBuffer(TestID, SizeOf(TestID));
  396. if TestID <> SetupID then
  397. SetupCorruptError;
  398. var SetupEncryptionHeaderCRC: Longint;
  399. SourceF.ReadBuffer(SetupEncryptionHeaderCRC, SizeOf(SetupEncryptionHeaderCRC));
  400. SourceF.ReadBuffer(SetupEncryptionHeader, SizeOf(SetupEncryptionHeader));
  401. if SetupEncryptionHeaderCRC <> GetCRC32(SetupEncryptionHeader, SizeOf(SetupEncryptionHeader)) then
  402. SetupCorruptError;
  403. var CryptKey: TSetupEncryptionKey;
  404. if SetupEncryptionHeader.EncryptionUse = euFull then begin
  405. if InitPassword = '' then
  406. raise Exception.Create(SMissingPassword);
  407. GenerateEncryptionKey(InitPassword, SetupEncryptionHeader.KDFSalt, SetupEncryptionHeader.KDFIterations, CryptKey);
  408. if not TestPassword(CryptKey, SetupEncryptionHeader.BaseNonce, SetupEncryptionHeader.PasswordTest) then
  409. raise Exception.Create(SIncorrectPassword);
  410. end;
  411. try
  412. Reader := TCompressedBlockReader.Create(SourceF, TLZMA1SmallDecompressor);
  413. try
  414. if SetupEncryptionHeader.EncryptionUse = euFull then
  415. Reader.InitDecryption(CryptKey, SetupEncryptionHeader.BaseNonce, sccCompressedBlocks1);
  416. SECompressedBlockRead(Reader, SetupHeader, SizeOf(SetupHeader),
  417. SetupHeaderStrings, SetupHeaderAnsiStrings);
  418. LanguageEntryCount := SetupHeader.NumLanguageEntries;
  419. LanguageEntries := AllocMem(LanguageEntryCount * SizeOf(TSetupLanguageEntry));
  420. for I := 0 to LanguageEntryCount-1 do
  421. SECompressedBlockRead(Reader, LanguageEntries[I], SizeOf(LanguageEntries[I]),
  422. SetupLanguageEntryStrings, SetupLanguageEntryAnsiStrings);
  423. finally
  424. Reader.Free;
  425. end;
  426. except
  427. on ECompressDataError do
  428. SetupCorruptError;
  429. end;
  430. ActivateDefaultLanguage;
  431. if InitShowHelp then begin
  432. { Show the command line help. }
  433. ShowHelp(SetupMessages[msgHelpTextNote]);
  434. SetupLdrExitCode := 0;
  435. end else begin
  436. { Show the startup prompt. If this is enabled, SetupHeader.AppName won't
  437. have constants. }
  438. if not(shDisableStartupPrompt in SetupHeader.Options) and
  439. not InitDisableStartupPrompt and
  440. (MessageBox(0, PChar(FmtSetupMessage1(msgSetupLdrStartupMessage, SetupHeader.AppName)),
  441. PChar(SetupMessages[msgSetupAppTitle]), MB_YESNO or MB_ICONQUESTION) <> IDYES) then begin
  442. SetupLdrExitCode := ecCancelledBeforeInstall;
  443. Abort;
  444. end;
  445. { Create a temporary directory, and extract the embedded setup program
  446. there }
  447. TempDir := CreateTempDir('.tmp', IsAdminLoggedOn);
  448. S := AddBackslash(TempDir) + PathChangeExt(PathExtractName(SelfFilename), '.tmp');
  449. TempFile := S; { assign only if string was successfully constructed }
  450. SourceF.Seek(OffsetTable.OffsetEXE);
  451. try
  452. P := nil;
  453. DestF := TFile.Create(TempFile, fdCreateAlways, faWrite, fsNone);
  454. try
  455. GetMem(P, OffsetTable.UncompressedSizeEXE);
  456. FillChar(P^, OffsetTable.UncompressedSizeEXE, 0);
  457. try
  458. Reader := TCompressedBlockReader.Create(SourceF, TLZMA1SmallDecompressor);
  459. try
  460. Reader.Read(P^, OffsetTable.UncompressedSizeEXE);
  461. finally
  462. Reader.Free;
  463. end;
  464. except
  465. on ECompressDataError do
  466. SetupCorruptError;
  467. end;
  468. TransformCallInstructions(P^, OffsetTable.UncompressedSizeEXE, False, 0);
  469. if GetCRC32(P^, OffsetTable.UncompressedSizeEXE) <> OffsetTable.CRCEXE then
  470. SetupCorruptError;
  471. { Preallocate the bytes to avoid file system fragmentation }
  472. DestF.Seek(OffsetTable.UncompressedSizeEXE);
  473. DestF.Truncate;
  474. DestF.Seek(0);
  475. DestF.WriteBuffer(P^, OffsetTable.UncompressedSizeEXE);
  476. finally
  477. FreeMem(P);
  478. DestF.Free;
  479. end;
  480. except
  481. on E: EFileError do begin
  482. SetLastError(E.ErrorCode);
  483. RaiseLastError(msgLdrCannotCreateTemp);
  484. end;
  485. end;
  486. FreeAndNil(SourceF);
  487. { Create SetupLdrWnd, which is used by Setup to communicate with
  488. SetupLdr }
  489. SetupLdrWnd := CreateWindowEx(0, 'STATIC', 'InnoSetupLdrWindow', 0,
  490. 0, 0, 0, 0, HWND_DESKTOP, 0, HInstance, nil);
  491. LONG_PTR(OrigWndProc) := SetWindowLongPtr(SetupLdrWnd, GWLP_WNDPROC,
  492. LONG_PTR(@SetupLdrWndProc));
  493. { Now execute Setup. Use the exit code it returns as our exit code.
  494. The UInt32 cast prevents sign extension. Also see
  495. https://learn.microsoft.com/en-us/windows/win32/winprog64/interprocess-communication
  496. SelfFilename is passed in "final" reparsed form so that Setup won't
  497. have trouble accessing the file after enabling RedirectionGuard if
  498. there's an untrusted redirect in the path. }
  499. ExecAndWait(TempFile, Format('/SL5="$%x,%d,%d,',
  500. [UInt32(SetupLdrWnd), OffsetTable.Offset0, OffsetTable.Offset1]) +
  501. GetFinalFileName(SelfFilename) + '" ' + GetCmdTail, SetupLdrExitCode);
  502. { Synchronize our active language with Setup's, in case we need to
  503. display any messages below }
  504. if PendingNewLanguage <> -1 then
  505. SetActiveLanguage(PendingNewLanguage);
  506. end;
  507. finally
  508. SourceF.Free;
  509. if TempFile <> '' then
  510. { Even though Setup has terminated by now, the system may still have
  511. the file locked for a short period of time (esp. on multiprocessor
  512. systems), so use DelayDeleteFile to delete it. }
  513. DelayDeleteFile(TempFile, 13, 50, 250);
  514. if TempDir <> '' then
  515. RemoveDirectory(PChar(TempDir));
  516. if SetupLdrWnd <> 0 then
  517. { SetupLdrWnd must be destroyed before RestartComputer is called,
  518. otherwise SetupLdrWndProc would deny the shutdown request }
  519. DestroyWindow(SetupLdrWnd);
  520. if Assigned(LanguageEntries) then begin
  521. Finalize(LanguageEntries[0], LanguageEntryCount);
  522. FreeMem(LanguageEntries);
  523. LanguageEntries := nil;
  524. end;
  525. end;
  526. if RestartSystem then begin
  527. if not RestartComputer and not InitSuppressMsgBoxes then
  528. MessageBox(0, PChar(SetupMessages[msgErrorRestartingComputer]),
  529. PChar(SetupMessages[msgErrorTitle]), MB_OK or MB_ICONEXCLAMATION or
  530. MB_SETFOREGROUND);
  531. end;
  532. except
  533. ShowExceptionMsg;
  534. end;
  535. System.ExitCode := SetupLdrExitCode;
  536. end.