SetupLdr.dpr 22 KB

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