SetupLdr.dpr 22 KB

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