SetupLdr.dpr 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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;
  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. end;
  103. if WantToSuppressMsgBoxes and SilentOrVerySilent then
  104. InitSuppressMsgBoxes := True;
  105. end;
  106. procedure SetActiveLanguage(const I: Integer);
  107. { Activates the specified language }
  108. begin
  109. if (I >= 0) and (I < LanguageEntryCount) and (I <> ActiveLanguage) then begin
  110. AssignSetupMessages(LanguageEntries[I].Data[1], ULength(LanguageEntries[I].Data));
  111. ActiveLanguage := I;
  112. end;
  113. end;
  114. function GetLanguageEntryProc(Index: Integer; var Entry: PSetupLanguageEntry): Boolean;
  115. begin
  116. Result := False;
  117. if Index < LanguageEntryCount then begin
  118. Entry := @LanguageEntries[Index];
  119. Result := True;
  120. end;
  121. end;
  122. procedure ActivateDefaultLanguage;
  123. { Auto-detects the most appropriate language and activates it.
  124. Note: A like-named version of this function is also present in Main.pas. }
  125. var
  126. I: Integer;
  127. begin
  128. DetermineDefaultLanguage(GetLanguageEntryProc, SetupHeader.LanguageDetectionMethod,
  129. InitLang, I);
  130. SetActiveLanguage(I);
  131. end;
  132. function SetupLdrWndProc(Wnd: HWND; Msg: UINT; WParam: WPARAM; LParam: LPARAM): LRESULT;
  133. stdcall;
  134. begin
  135. Result := 0;
  136. case Msg of
  137. WM_QUERYENDSESSION: begin
  138. { Return zero so that a shutdown attempt can't kill SetupLdr }
  139. end;
  140. WM_USER + 150: begin
  141. if WParam = 10000 then begin
  142. { Setup wants SetupLdr to restart the computer before it exits }
  143. RestartSystem := True;
  144. end
  145. else if WParam = 10001 then begin
  146. { Setup wants SetupLdr to change its active language }
  147. PendingNewLanguage := Integer(LParam);
  148. end;
  149. end;
  150. else
  151. Result := CallWindowProc(OrigWndProc, Wnd, Msg, WParam, LParam);
  152. end;
  153. end;
  154. procedure ProcessMessages;
  155. var
  156. Msg: TMsg;
  157. begin
  158. while PeekMessage(Msg, 0, 0, 0, PM_REMOVE) do begin
  159. TranslateMessage(Msg);
  160. DispatchMessage(Msg);
  161. end;
  162. end;
  163. procedure ExecAndWait(const Filename, Parms: String; var ExitCode: Integer);
  164. var
  165. CmdLine: String;
  166. StartupInfo: TStartupInfo;
  167. ProcessInfo: TProcessInformation;
  168. begin
  169. CmdLine := '"' + Filename + '" ' + Parms;
  170. FillChar(StartupInfo, SizeOf(StartupInfo), 0);
  171. StartupInfo.cb := SizeOf(StartupInfo);
  172. if not CreateProcess(nil, PChar(CmdLine), nil, nil, False, 0, nil, nil,
  173. StartupInfo, ProcessInfo) then
  174. RaiseLastError(msgLdrCannotExecTemp);
  175. CloseHandle(ProcessInfo.hThread);
  176. { Wait for the process to terminate, processing messages in the meantime }
  177. repeat
  178. ProcessMessages;
  179. until MsgWaitForMultipleObjects(1, ProcessInfo.hProcess, False, INFINITE,
  180. QS_ALLINPUT) <> WAIT_OBJECT_0+1;
  181. { Now that the process has exited, process any remaining messages.
  182. (There may be an asynchronously-sent "restart request" message still
  183. queued if MWFMO saw the process terminate before checking for new
  184. messages.) }
  185. ProcessMessages;
  186. GetExitCodeProcess(ProcessInfo.hProcess, DWORD(ExitCode));
  187. CloseHandle(ProcessInfo.hProcess);
  188. end;
  189. procedure SetupCorruptError;
  190. begin
  191. if SetupMessages[msgSetupFileCorrupt] <> '' then
  192. raise Exception.Create(SetupMessages[msgSetupFileCorrupt])
  193. else
  194. { In case the messages haven't been loaded yet, use the constant }
  195. raise Exception.Create(SSetupFileCorrupt);
  196. end;
  197. procedure RunImageLocally(const Module: HMODULE);
  198. { Force all of the specified module to be paged in to ensure subsequent
  199. accesses don't cause the disk image to be read.
  200. Based on code from http://www.microsoft.com/msj/0398/win320398.htm, with
  201. some fixes incorporated. }
  202. procedure Touch(var X: DWORD);
  203. { Note: Uses asm to ensure it isn't optimized away }
  204. asm
  205. xor edx, edx
  206. lock or [eax], edx
  207. end;
  208. var
  209. SysInfo: TSystemInfo;
  210. CurAddr: Pointer;
  211. MemInfo: TMemoryBasicInformation;
  212. ChangedProtection: Boolean;
  213. OrigProtect: DWORD;
  214. Offset: Cardinal;
  215. begin
  216. { Get system's page size }
  217. GetSystemInfo(SysInfo);
  218. CurAddr := Pointer(Module);
  219. if VirtualQuery(CurAddr, MemInfo, SizeOf(MemInfo)) = 0 then
  220. Exit;
  221. { Perform this loop until we find a region beyond the end of the file }
  222. while MemInfo.AllocationBase = Pointer(Module) do begin
  223. { We can only force committed pages into RAM.
  224. We do not want to trigger guard pages and confuse the application. }
  225. if (MemInfo.State = MEM_COMMIT) and (MemInfo.Protect and PAGE_GUARD = 0) then begin
  226. ChangedProtection := False;
  227. { Determine if the pages in this region are nonwriteable }
  228. if (MemInfo.Protect = PAGE_NOACCESS) or
  229. (MemInfo.Protect = PAGE_READONLY) or
  230. (MemInfo.Protect = PAGE_EXECUTE) or
  231. (MemInfo.Protect = PAGE_EXECUTE_READ) then begin
  232. { Nonwriteable region, make it writeable (with the least protection) }
  233. if VirtualProtect(MemInfo.BaseAddress, MemInfo.RegionSize,
  234. PAGE_EXECUTE_READWRITE, @OrigProtect) then
  235. ChangedProtection := True;
  236. end;
  237. { Write to every page in the region.
  238. This forces the page to be in RAM and swapped to the paging file. }
  239. Offset := 0;
  240. while Offset < Cardinal(MemInfo.RegionSize) do begin
  241. Touch(PDWORD(Cardinal(MemInfo.BaseAddress) + Offset)^);
  242. Inc(Offset, SysInfo.dwPageSize);
  243. end;
  244. { If we changed the protection, change it back }
  245. if ChangedProtection then
  246. VirtualProtect(MemInfo.BaseAddress, MemInfo.RegionSize, OrigProtect,
  247. @OrigProtect);
  248. end;
  249. { Get next region }
  250. Cardinal(CurAddr) := Cardinal(MemInfo.BaseAddress) + MemInfo.RegionSize;
  251. if VirtualQuery(CurAddr, MemInfo, SizeOf(MemInfo)) = 0 then
  252. Break;
  253. end;
  254. end;
  255. function GetSetupLdrOffsetTable: PSetupLdrOffsetTable;
  256. { Locates the offset table resource, and returns a pointer to it }
  257. var
  258. Rsrc: HRSRC;
  259. ResData: HGLOBAL;
  260. begin
  261. Rsrc := FindResource(0, MAKEINTRESOURCE(SetupLdrOffsetTableResID), RT_RCDATA);
  262. if Rsrc = 0 then
  263. SetupCorruptError;
  264. if SizeofResource(0, Rsrc) <> SizeOf(Result^) then
  265. SetupCorruptError;
  266. ResData := LoadResource(0, Rsrc);
  267. if ResData = 0 then
  268. SetupCorruptError;
  269. Result := LockResource(ResData);
  270. if Result = nil then
  271. SetupCorruptError;
  272. end;
  273. procedure ShowHelp(const CustomNote: String);
  274. const
  275. SNewLine = #13#10;
  276. var
  277. PrNote, Help: String;
  278. begin
  279. { do not localize }
  280. if proCommandLine in SetupHeader.PrivilegesRequiredOverridesAllowed then begin
  281. PrNote := '/ALLUSERS' + SNewLine +
  282. 'Instructs Setup to install in administrative install mode.' + SNewLine +
  283. '/CURRENTUSER' + SNewLine +
  284. 'Instructs Setup to install in non administrative install mode.' + SNewLine;
  285. end else
  286. PrNote := '';
  287. Help := 'The Setup program accepts optional command line parameters.' + SNewLine +
  288. SNewLine +
  289. '/HELP, /?' + SNewLine +
  290. 'Shows this information.' + SNewLine +
  291. '/SP-' + SNewLine +
  292. 'Disables the "This will install... Do you wish to continue?" message box at the beginning of Setup.' + SNewLine +
  293. '/SILENT, /VERYSILENT' + SNewLine +
  294. 'Instructs Setup to be silent or very silent.' + SNewLine +
  295. '/NOSTYLE' + SNewLine +
  296. 'Prevents Setup from activating custom styles (including the built-in custom dark style).' + SNewLine +
  297. '/SUPPRESSMSGBOXES' + SNewLine +
  298. 'Instructs Setup to suppress message boxes.' + SNewLine +
  299. '/LOG' + SNewLine +
  300. 'Causes Setup to create a log file in the user''s TEMP directory.' + SNewLine +
  301. '/LOG="filename"' + SNewLine +
  302. 'Same as /LOG, except it allows you to specify a fixed path/filename to use for the log file.' + SNewLine +
  303. '/NOCANCEL' + SNewLine +
  304. 'Prevents the user from cancelling during the installation process.' + SNewLine +
  305. '/NORESTART' + SNewLine +
  306. 'Prevents Setup from restarting the system following a successful installation, or after a Preparing to Install failure that requests a restart.' + SNewLine +
  307. '/RESTARTEXITCODE=exit code' + SNewLine +
  308. 'Specifies a custom exit code that Setup is to return when the system needs to be restarted.' + SNewLine +
  309. '/CLOSEAPPLICATIONS, /NOCLOSEAPPLICATIONS' + SNewLine +
  310. 'Instructs Setup to attempt closing applications using files that need to be updated, or prevents it from doing so.' + SNewLine +
  311. '/FORCECLOSEAPPLICATIONS, /FORCENOCLOSEAPPLICATIONS' + SNewLine +
  312. 'Instructs Setup to force close when closing applications, or prevents it from doing so.' + SNewLine +
  313. '/LOGCLOSEAPPLICATIONS' + SNewLine +
  314. 'Instructs Setup to create extra logging when closing applications for debugging purposes.' + SNewLine +
  315. '/RESTARTAPPLICATIONS, /NORESTARTAPPLICATIONS' + SNewLine +
  316. 'Instructs Setup to attempt restarting applications, or prevents it from doing so.' + SNewLine +
  317. '/LOADINF="filename", /SAVEINF="filename"' + SNewLine +
  318. 'Instructs Setup to load the settings from the specified file after having checked the command line, or to save them to it.' + SNewLine +
  319. '/LANG=language' + SNewLine +
  320. 'Specifies the internal name of the language to use.' + SNewLine +
  321. '/DIR="x:\dirname"' + SNewLine +
  322. 'Overrides the default directory name.' + SNewLine +
  323. '/GROUP="folder name"' + SNewLine +
  324. 'Overrides the default folder name.' + SNewLine +
  325. '/NOICONS' + SNewLine +
  326. 'Instructs Setup to initially check the Don''t create a Start Menu folder check box.' + SNewLine +
  327. '/TYPE=type name' + SNewLine +
  328. 'Overrides the default setup type.' + SNewLine +
  329. '/COMPONENTS="comma separated list of component names"' + SNewLine +
  330. 'Overrides the default component settings.' + SNewLine +
  331. '/TASKS="comma separated list of task names"' + SNewLine +
  332. 'Specifies a list of tasks that should be initially selected.' + SNewLine +
  333. '/MERGETASKS="comma separated list of task names"' + SNewLine +
  334. '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 +
  335. '/PASSWORD=password' + SNewLine +
  336. 'Specifies the password to use.' + SNewLine +
  337. PrNote +
  338. CustomNote +
  339. SNewLine +
  340. 'For more detailed information, please visit https://jrsoftware.org/ishelp/index.php?topic=setupcmdline';
  341. MessageBox(0, PChar(Help), 'Setup', MB_OK or MB_ICONSTOP);
  342. end;
  343. var
  344. SelfFilename: String;
  345. SourceF, DestF: TFile;
  346. OffsetTable: PSetupLdrOffsetTable;
  347. TempDir: String;
  348. S: String;
  349. TempFile: String;
  350. TestID: TSetupID;
  351. P: Pointer;
  352. Reader: TCompressedBlockReader;
  353. I: Integer;
  354. begin
  355. try
  356. {$IFDEF DEBUG}
  357. ReportMemoryLeaksOnShutdown := True;
  358. {$ENDIF}
  359. { Ensure all of SetupLdr is paged in so that in the case of a disk spanning
  360. install Windows will never complain when the disk/CD containing SetupLdr
  361. is ejected. }
  362. RunImageLocally(HInstance);
  363. ProcessCommandLine;
  364. SelfFilename := NewParamStr(0);
  365. SourceF := TFile.Create(SelfFilename, fdOpenExisting, faRead, fsRead);
  366. try
  367. OffsetTable := GetSetupLdrOffsetTable;
  368. { Note: We don't check the OffsetTable.ID here because it would put a
  369. copy of the ID in the data section, and that would confuse external
  370. programs that search for the offset table by ID. }
  371. if (OffsetTable.Version <> SetupLdrOffsetTableVersion) or
  372. (GetCRC32(OffsetTable^, SizeOf(OffsetTable^) - SizeOf(OffsetTable.TableCRC)) <> OffsetTable.TableCRC) or
  373. (SourceF.Size < OffsetTable.TotalSize) then
  374. SetupCorruptError;
  375. SourceF.Seek(OffsetTable.Offset0);
  376. SourceF.ReadBuffer(TestID, SizeOf(TestID));
  377. if TestID <> SetupID then
  378. SetupCorruptError;
  379. var SetupEncryptionHeaderCRC: Longint;
  380. SourceF.Read(SetupEncryptionHeaderCRC, SizeOf(SetupEncryptionHeaderCRC));
  381. SourceF.Read(SetupEncryptionHeader, SizeOf(SetupEncryptionHeader));
  382. if SetupEncryptionHeaderCRC <> GetCRC32(SetupEncryptionHeader, SizeOf(SetupEncryptionHeader)) then
  383. SetupCorruptError;
  384. var CryptKey: TSetupEncryptionKey;
  385. if SetupEncryptionHeader.EncryptionUse = euFull then begin
  386. if InitPassword = '' then
  387. raise Exception.Create(SMissingPassword);
  388. GenerateEncryptionKey(InitPassword, SetupEncryptionHeader.KDFSalt, SetupEncryptionHeader.KDFIterations, CryptKey);
  389. if not TestPassword(CryptKey, SetupEncryptionHeader.BaseNonce, SetupEncryptionHeader.PasswordTest) then
  390. raise Exception.Create(SIncorrectPassword);
  391. end;
  392. try
  393. Reader := TCompressedBlockReader.Create(SourceF, TLZMA1SmallDecompressor);
  394. try
  395. if SetupEncryptionHeader.EncryptionUse = euFull then
  396. Reader.InitDecryption(CryptKey, SetupEncryptionHeader.BaseNonce, sccCompressedBlocks1);
  397. SECompressedBlockRead(Reader, SetupHeader, SizeOf(SetupHeader),
  398. SetupHeaderStrings, SetupHeaderAnsiStrings);
  399. LanguageEntryCount := SetupHeader.NumLanguageEntries;
  400. LanguageEntries := AllocMem(LanguageEntryCount * SizeOf(TSetupLanguageEntry));
  401. for I := 0 to LanguageEntryCount-1 do
  402. SECompressedBlockRead(Reader, LanguageEntries[I], SizeOf(LanguageEntries[I]),
  403. SetupLanguageEntryStrings, SetupLanguageEntryAnsiStrings);
  404. finally
  405. Reader.Free;
  406. end;
  407. except
  408. on ECompressDataError do
  409. SetupCorruptError;
  410. end;
  411. ActivateDefaultLanguage;
  412. if InitShowHelp then begin
  413. { Show the command line help. }
  414. ShowHelp(SetupMessages[msgHelpTextNote]);
  415. SetupLdrExitCode := 0;
  416. end else begin
  417. { Show the startup prompt. If this is enabled, SetupHeader.AppName won't
  418. have constants. }
  419. if not(shDisableStartupPrompt in SetupHeader.Options) and
  420. not InitDisableStartupPrompt and
  421. (MessageBox(0, PChar(FmtSetupMessage1(msgSetupLdrStartupMessage, SetupHeader.AppName)),
  422. PChar(SetupMessages[msgSetupAppTitle]), MB_YESNO or MB_ICONQUESTION) <> IDYES) then begin
  423. SetupLdrExitCode := ecCancelledBeforeInstall;
  424. Abort;
  425. end;
  426. { Create a temporary directory, and extract the embedded setup program
  427. there }
  428. TempDir := CreateTempDir('.tmp', IsAdminLoggedOn);
  429. S := AddBackslash(TempDir) + PathChangeExt(PathExtractName(SelfFilename), '.tmp');
  430. TempFile := S; { assign only if string was successfully constructed }
  431. SourceF.Seek(OffsetTable.OffsetEXE);
  432. try
  433. P := nil;
  434. DestF := TFile.Create(TempFile, fdCreateAlways, faWrite, fsNone);
  435. try
  436. GetMem(P, OffsetTable.UncompressedSizeEXE);
  437. FillChar(P^, OffsetTable.UncompressedSizeEXE, 0);
  438. try
  439. Reader := TCompressedBlockReader.Create(SourceF, TLZMA1SmallDecompressor);
  440. try
  441. Reader.Read(P^, OffsetTable.UncompressedSizeEXE);
  442. finally
  443. Reader.Free;
  444. end;
  445. except
  446. on ECompressDataError do
  447. SetupCorruptError;
  448. end;
  449. TransformCallInstructions(P^, OffsetTable.UncompressedSizeEXE, False, 0);
  450. if GetCRC32(P^, OffsetTable.UncompressedSizeEXE) <> OffsetTable.CRCEXE then
  451. SetupCorruptError;
  452. { Preallocate the bytes to avoid file system fragmentation }
  453. DestF.Seek(OffsetTable.UncompressedSizeEXE);
  454. DestF.Truncate;
  455. DestF.Seek(0);
  456. DestF.WriteBuffer(P^, OffsetTable.UncompressedSizeEXE);
  457. finally
  458. FreeMem(P);
  459. DestF.Free;
  460. end;
  461. except
  462. on E: EFileError do begin
  463. SetLastError(E.ErrorCode);
  464. RaiseLastError(msgLdrCannotCreateTemp);
  465. end;
  466. end;
  467. FreeAndNil(SourceF);
  468. { Create SetupLdrWnd, which is used by Setup to communicate with
  469. SetupLdr }
  470. SetupLdrWnd := CreateWindowEx(0, 'STATIC', 'InnoSetupLdrWindow', 0,
  471. 0, 0, 0, 0, HWND_DESKTOP, 0, HInstance, nil);
  472. Longint(OrigWndProc) := SetWindowLong(SetupLdrWnd, GWL_WNDPROC,
  473. Longint(@SetupLdrWndProc));
  474. { Now execute Setup. Use the exit code it returns as our exit code. }
  475. ExecAndWait(TempFile, Format('/SL5="$%x,%d,%d,',
  476. [UInt32(SetupLdrWnd), OffsetTable.Offset0, OffsetTable.Offset1]) +
  477. SelfFilename + '" ' + GetCmdTail, SetupLdrExitCode);
  478. { Synchronize our active language with Setup's, in case we need to
  479. display any messages below }
  480. if PendingNewLanguage <> -1 then
  481. SetActiveLanguage(PendingNewLanguage);
  482. end;
  483. finally
  484. SourceF.Free;
  485. if TempFile <> '' then
  486. { Even though Setup has terminated by now, the system may still have
  487. the file locked for a short period of time (esp. on multiprocessor
  488. systems), so use DelayDeleteFile to delete it. }
  489. DelayDeleteFile(TempFile, 13, 50, 250);
  490. if TempDir <> '' then
  491. RemoveDirectory(PChar(TempDir));
  492. if SetupLdrWnd <> 0 then
  493. { SetupLdrWnd must be destroyed before RestartComputer is called,
  494. otherwise SetupLdrWndProc would deny the shutdown request }
  495. DestroyWindow(SetupLdrWnd);
  496. if Assigned(LanguageEntries) then begin
  497. Finalize(LanguageEntries[0], LanguageEntryCount);
  498. FreeMem(LanguageEntries);
  499. LanguageEntries := nil;
  500. end;
  501. end;
  502. if RestartSystem then begin
  503. if not RestartComputer and not InitSuppressMsgBoxes then
  504. MessageBox(0, PChar(SetupMessages[msgErrorRestartingComputer]),
  505. PChar(SetupMessages[msgErrorTitle]), MB_OK or MB_ICONEXCLAMATION or
  506. MB_SETFOREGROUND);
  507. end;
  508. except
  509. ShowExceptionMsg;
  510. end;
  511. System.ExitCode := SetupLdrExitCode;
  512. end.