system.pp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2005 by Florian Klaempfl and Pavel Ozerski
  4. member of the Free Pascal development team.
  5. FPC Pascal system unit for the Win32 API.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. unit System;
  13. interface
  14. {$ifdef SYSTEMDEBUG}
  15. {$define SYSTEMEXCEPTIONDEBUG}
  16. {$endif SYSTEMDEBUG}
  17. {$define FPC_HAS_INDIRECT_MAIN_INFORMATION}
  18. {$ifdef cpui386}
  19. {$define Set_i386_Exception_handler}
  20. {$endif cpui386}
  21. {$define DISABLE_NO_THREAD_MANAGER}
  22. {$define HAS_WIDESTRINGMANAGER}
  23. { include system-independent routine headers }
  24. {$I systemh.inc}
  25. const
  26. LineEnding = #13#10;
  27. LFNSupport = true;
  28. DirectorySeparator = '\';
  29. DriveSeparator = ':';
  30. ExtensionSeparator = '.';
  31. PathSeparator = ';';
  32. AllowDirectorySeparators : set of char = ['\','/'];
  33. AllowDriveSeparators : set of char = [':'];
  34. { FileNameCaseSensitive and FileNameCasePreserving are defined separately below!!! }
  35. maxExitCode = 65535;
  36. MaxPathLen = 260;
  37. AllFilesMask = '*';
  38. type
  39. PEXCEPTION_FRAME = ^TEXCEPTION_FRAME;
  40. TEXCEPTION_FRAME = record
  41. next : PEXCEPTION_FRAME;
  42. handler : pointer;
  43. end;
  44. const
  45. { Default filehandles }
  46. UnusedHandle : THandle = THandle(-1);
  47. StdInputHandle : THandle = 0;
  48. StdOutputHandle : THandle = 0;
  49. StdErrorHandle : THandle = 0;
  50. FileNameCaseSensitive : boolean = false;
  51. FileNameCasePreserving: boolean = true;
  52. CtrlZMarksEOF: boolean = true; (* #26 is considered as end of file *)
  53. sLineBreak = LineEnding;
  54. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsCRLF;
  55. System_exception_frame : PEXCEPTION_FRAME =nil;
  56. type
  57. TStartupInfo=packed record
  58. cb : longint;
  59. lpReserved : Pointer;
  60. lpDesktop : Pointer;
  61. lpTitle : Pointer;
  62. dwX : longint;
  63. dwY : longint;
  64. dwXSize : longint;
  65. dwYSize : longint;
  66. dwXCountChars : longint;
  67. dwYCountChars : longint;
  68. dwFillAttribute : longint;
  69. dwFlags : longint;
  70. wShowWindow : Word;
  71. cbReserved2 : Word;
  72. lpReserved2 : Pointer;
  73. hStdInput : longint;
  74. hStdOutput : longint;
  75. hStdError : longint;
  76. end;
  77. var
  78. { C compatible arguments }
  79. argc : longint; public name 'operatingsystem_parameter_argc';
  80. argv : ppchar; public name 'operatingsystem_parameter_argv';
  81. { Win32 Info }
  82. startupinfo : tstartupinfo;
  83. MainInstance,
  84. cmdshow : longint;
  85. DLLreason : dword; public name 'operatingsystem_dllreason';
  86. DLLparam : PtrInt; public name 'operatingsystem_dllparam';
  87. StartupConsoleMode : DWORD;
  88. const
  89. hprevinst: longint=0;
  90. type
  91. TDLL_Entry_Hook = procedure (dllparam : PtrInt);
  92. const
  93. Dll_Process_Detach_Hook : TDLL_Entry_Hook = nil;
  94. Dll_Thread_Attach_Hook : TDLL_Entry_Hook = nil;
  95. Dll_Thread_Detach_Hook : TDLL_Entry_Hook = nil;
  96. Const
  97. { it can be discussed whether fmShareDenyNone means read and write or read, write and delete, see
  98. also http://bugs.freepascal.org/view.php?id=8898, this allows users to configure the used
  99. value
  100. }
  101. fmShareDenyNoneFlags : DWord = 3;
  102. implementation
  103. var
  104. SysInstance : Longint;public name '_FPC_SysInstance';
  105. InitFinalTable : record end; external name 'INITFINAL';
  106. ThreadvarTablesTable : record end; external name 'FPC_THREADVARTABLES';
  107. procedure PascalMain;stdcall;external name 'PASCALMAIN';
  108. procedure asm_exit;stdcall;external name 'asm_exit';
  109. const
  110. EntryInformation : TEntryInformation = (
  111. InitFinalTable : @InitFinalTable;
  112. ThreadvarTablesTable : @ThreadvarTablesTable;
  113. asm_exit : @asm_exit;
  114. PascalMain : @PascalMain;
  115. valgrind_used : false;
  116. );
  117. { include system independent routines }
  118. {$I system.inc}
  119. { include code common with win64 }
  120. {$I syswin.inc}
  121. {*****************************************************************************
  122. System Dependent Exit code
  123. *****************************************************************************}
  124. procedure install_exception_handlers;forward;
  125. procedure remove_exception_handlers;forward;
  126. Procedure system_exit;
  127. begin
  128. if IsLibrary then
  129. begin
  130. { If exiting from DLL_PROCESS_ATTACH/DETACH, unwind to DllMain context. }
  131. if DllInitState in [DLL_PROCESS_ATTACH,DLL_PROCESS_DETACH] then
  132. LongJmp(DLLBuf,1)
  133. else
  134. { Abnormal termination, Halt has been called from DLL function,
  135. put down the entire process (DLL_PROCESS_DETACH will still
  136. occur). At this point RTL has been already finalized in InternalExit
  137. and shouldn't be finalized another time in DLL_PROCESS_DETACH.
  138. Indicate this by resetting MainThreadIdWin32. }
  139. MainThreadIDWin32:=0;
  140. end;
  141. if not IsConsole then
  142. begin
  143. Close(stderr);
  144. Close(stdout);
  145. Close(erroutput);
  146. Close(Input);
  147. Close(Output);
  148. { what about Input and Output ?? PM }
  149. { now handled, FPK }
  150. end;
  151. if not IsLibrary then
  152. remove_exception_handlers;
  153. { do cleanup required by the startup code }
  154. EntryInformation.asm_exit();
  155. { call exitprocess, with cleanup as required }
  156. ExitProcess(exitcode);
  157. end;
  158. var
  159. { value of the stack segment
  160. to check if the call stack can be written on exceptions }
  161. _SS : Cardinal;
  162. procedure Exe_entry(const info : TEntryInformation);[public,alias:'_FPC_EXE_Entry'];
  163. var
  164. ST : pointer;
  165. begin
  166. EntryInformation:=info;
  167. IsLibrary:=false;
  168. { install the handlers for exe only ?
  169. or should we install them for DLL also ? (PM) }
  170. install_exception_handlers;
  171. { This strange construction is needed to solve the _SS problem
  172. with a smartlinked syswin32 (PFV) }
  173. asm
  174. { allocate space for an exception frame }
  175. pushl $0
  176. pushl %fs:(0)
  177. { movl %esp,%fs:(0)
  178. but don't insert it as it doesn't
  179. point to anything yet
  180. this will be used in signals unit }
  181. movl %esp,%eax
  182. movl %eax,System_exception_frame
  183. pushl %ebp
  184. movl %esp,%eax
  185. movl %eax,st
  186. end;
  187. StackTop:=st;
  188. asm
  189. xorl %eax,%eax
  190. movw %ss,%ax
  191. movl %eax,_SS
  192. xorl %ebp,%ebp
  193. end;
  194. EntryInformation.PascalMain();
  195. asm
  196. popl %ebp
  197. end;
  198. { if we pass here there was no error ! }
  199. system_exit;
  200. end;
  201. function is_prefetch(p : pointer) : boolean;
  202. var
  203. a : array[0..15] of byte;
  204. doagain : boolean;
  205. instrlo,instrhi,opcode : byte;
  206. i : longint;
  207. begin
  208. result:=false;
  209. { read memory savely without causing another exeception }
  210. if not(ReadProcessMemory(GetCurrentProcess,p,@a,sizeof(a),nil)) then
  211. exit;
  212. i:=0;
  213. doagain:=true;
  214. while doagain and (i<15) do
  215. begin
  216. opcode:=a[i];
  217. instrlo:=opcode and $f;
  218. instrhi:=opcode and $f0;
  219. case instrhi of
  220. { prefix? }
  221. $20,$30:
  222. doagain:=(instrlo and 7)=6;
  223. $60:
  224. doagain:=(instrlo and $c)=4;
  225. $f0:
  226. doagain:=instrlo in [0,2,3];
  227. $0:
  228. begin
  229. result:=(instrlo=$f) and (a[i+1] in [$d,$18]);
  230. exit;
  231. end;
  232. else
  233. doagain:=false;
  234. end;
  235. inc(i);
  236. end;
  237. end;
  238. //
  239. // Hardware exception handling
  240. //
  241. {$ifdef Set_i386_Exception_handler}
  242. type
  243. PFloatingSaveArea = ^TFloatingSaveArea;
  244. TFloatingSaveArea = packed record
  245. ControlWord : Cardinal;
  246. StatusWord : Cardinal;
  247. TagWord : Cardinal;
  248. ErrorOffset : Cardinal;
  249. ErrorSelector : Cardinal;
  250. DataOffset : Cardinal;
  251. DataSelector : Cardinal;
  252. RegisterArea : array[0..79] of Byte;
  253. Cr0NpxState : Cardinal;
  254. end;
  255. PContext = ^TContext;
  256. TContext = packed record
  257. //
  258. // The flags values within this flag control the contents of
  259. // a CONTEXT record.
  260. //
  261. ContextFlags : Cardinal;
  262. //
  263. // This section is specified/returned if CONTEXT_DEBUG_REGISTERS is
  264. // set in ContextFlags. Note that CONTEXT_DEBUG_REGISTERS is NOT
  265. // included in CONTEXT_FULL.
  266. //
  267. Dr0, Dr1, Dr2,
  268. Dr3, Dr6, Dr7 : Cardinal;
  269. //
  270. // This section is specified/returned if the
  271. // ContextFlags word contains the flag CONTEXT_FLOATING_POINT.
  272. //
  273. FloatSave : TFloatingSaveArea;
  274. //
  275. // This section is specified/returned if the
  276. // ContextFlags word contains the flag CONTEXT_SEGMENTS.
  277. //
  278. SegGs, SegFs,
  279. SegEs, SegDs : Cardinal;
  280. //
  281. // This section is specified/returned if the
  282. // ContextFlags word contains the flag CONTEXT_INTEGER.
  283. //
  284. Edi, Esi, Ebx,
  285. Edx, Ecx, Eax : Cardinal;
  286. //
  287. // This section is specified/returned if the
  288. // ContextFlags word contains the flag CONTEXT_CONTROL.
  289. //
  290. Ebp : Cardinal;
  291. Eip : Cardinal;
  292. SegCs : Cardinal;
  293. EFlags, Esp, SegSs : Cardinal;
  294. //
  295. // This section is specified/returned if the ContextFlags word
  296. // contains the flag CONTEXT_EXTENDED_REGISTERS.
  297. // The format and contexts are processor specific
  298. //
  299. ExtendedRegisters : array[0..MAXIMUM_SUPPORTED_EXTENSION-1] of Byte;
  300. end;
  301. PExceptionPointers = ^TExceptionPointers;
  302. TExceptionPointers = packed record
  303. ExceptionRecord : PExceptionRecord;
  304. ContextRecord : PContext;
  305. end;
  306. { type of functions that should be used for exception handling }
  307. TTopLevelExceptionFilter = function (excep : PExceptionPointers) : Longint;stdcall;
  308. function SetUnhandledExceptionFilter(lpTopLevelExceptionFilter : TTopLevelExceptionFilter) : TTopLevelExceptionFilter;
  309. stdcall;external 'kernel32' name 'SetUnhandledExceptionFilter';
  310. const
  311. MaxExceptionLevel = 16;
  312. exceptLevel : Byte = 0;
  313. var
  314. exceptEip : array[0..MaxExceptionLevel-1] of Longint;
  315. exceptError : array[0..MaxExceptionLevel-1] of Byte;
  316. resetFPU : array[0..MaxExceptionLevel-1] of Boolean;
  317. {$ifdef SYSTEMEXCEPTIONDEBUG}
  318. procedure DebugHandleErrorAddrFrame(error : longint; addr, frame : pointer);
  319. begin
  320. if IsConsole then
  321. begin
  322. write(stderr,'HandleErrorAddrFrame(error=',error);
  323. write(stderr,',addr=',hexstr(ptruint(addr),8));
  324. writeln(stderr,',frame=',hexstr(ptruint(frame),8),')');
  325. end;
  326. HandleErrorAddrFrame(error,addr,frame);
  327. end;
  328. {$endif SYSTEMEXCEPTIONDEBUG}
  329. procedure JumpToHandleErrorFrame;
  330. var
  331. eip, ebp, error : Longint;
  332. begin
  333. // save ebp
  334. asm
  335. movl (%ebp),%eax
  336. movl %eax,ebp
  337. end;
  338. if (exceptLevel > 0) then
  339. dec(exceptLevel);
  340. eip:=exceptEip[exceptLevel];
  341. error:=exceptError[exceptLevel];
  342. {$ifdef SYSTEMEXCEPTIONDEBUG}
  343. if IsConsole then
  344. writeln(stderr,'In JumpToHandleErrorFrame error=',error);
  345. {$endif SYSTEMEXCEPTIONDEBUG}
  346. if resetFPU[exceptLevel] then
  347. SysResetFPU;
  348. { build a fake stack }
  349. asm
  350. movl ebp,%ecx
  351. movl eip,%edx
  352. movl error,%eax
  353. pushl eip
  354. movl ebp,%ebp // Change frame pointer
  355. {$ifdef SYSTEMEXCEPTIONDEBUG}
  356. jmpl DebugHandleErrorAddrFrame
  357. {$else not SYSTEMEXCEPTIONDEBUG}
  358. jmpl HandleErrorAddrFrame
  359. {$endif SYSTEMEXCEPTIONDEBUG}
  360. end;
  361. end;
  362. function syswin32_i386_exception_handler(excep : PExceptionPointers) : Longint;stdcall;
  363. var
  364. res: longint;
  365. err: byte;
  366. must_reset_fpu: boolean;
  367. begin
  368. res := EXCEPTION_CONTINUE_SEARCH;
  369. if excep^.ContextRecord^.SegSs=_SS then begin
  370. err := 0;
  371. must_reset_fpu := true;
  372. {$ifdef SYSTEMEXCEPTIONDEBUG}
  373. if IsConsole then Writeln(stderr,'Exception ',
  374. hexstr(excep^.ExceptionRecord^.ExceptionCode, 8));
  375. {$endif SYSTEMEXCEPTIONDEBUG}
  376. case excep^.ExceptionRecord^.ExceptionCode of
  377. STATUS_INTEGER_DIVIDE_BY_ZERO,
  378. STATUS_FLOAT_DIVIDE_BY_ZERO :
  379. err := 200;
  380. STATUS_ARRAY_BOUNDS_EXCEEDED :
  381. begin
  382. err := 201;
  383. must_reset_fpu := false;
  384. end;
  385. STATUS_STACK_OVERFLOW :
  386. begin
  387. err := 202;
  388. must_reset_fpu := false;
  389. end;
  390. STATUS_FLOAT_OVERFLOW :
  391. err := 205;
  392. STATUS_FLOAT_DENORMAL_OPERAND,
  393. STATUS_FLOAT_UNDERFLOW :
  394. err := 206;
  395. {excep^.ContextRecord^.FloatSave.StatusWord := excep^.ContextRecord^.FloatSave.StatusWord and $ffffff00;}
  396. STATUS_FLOAT_INEXACT_RESULT,
  397. STATUS_FLOAT_INVALID_OPERATION,
  398. STATUS_FLOAT_STACK_CHECK :
  399. err := 207;
  400. STATUS_INTEGER_OVERFLOW :
  401. begin
  402. err := 215;
  403. must_reset_fpu := false;
  404. end;
  405. STATUS_ILLEGAL_INSTRUCTION:
  406. { if we're testing sse support, simply set the flag and continue }
  407. if sse_check then
  408. begin
  409. os_supports_sse:=false;
  410. { skip the offending movaps %xmm7, %xmm6 instruction }
  411. inc(excep^.ContextRecord^.Eip,3);
  412. excep^.ExceptionRecord^.ExceptionCode := 0;
  413. res:=EXCEPTION_CONTINUE_EXECUTION;
  414. end
  415. else
  416. err := 216;
  417. STATUS_ACCESS_VIOLATION:
  418. { Athlon prefetch bug? }
  419. if is_prefetch(pointer(excep^.ContextRecord^.Eip)) then
  420. begin
  421. { if yes, then retry }
  422. excep^.ExceptionRecord^.ExceptionCode := 0;
  423. res:=EXCEPTION_CONTINUE_EXECUTION;
  424. end
  425. else
  426. err := 216;
  427. STATUS_CONTROL_C_EXIT:
  428. err := 217;
  429. STATUS_PRIVILEGED_INSTRUCTION:
  430. begin
  431. err := 218;
  432. must_reset_fpu := false;
  433. end;
  434. else
  435. begin
  436. if ((excep^.ExceptionRecord^.ExceptionCode and SEVERITY_ERROR) = SEVERITY_ERROR) then
  437. err := 217
  438. else
  439. err := 255;
  440. end;
  441. end;
  442. if (err <> 0) and (exceptLevel < MaxExceptionLevel) then begin
  443. exceptEip[exceptLevel] := excep^.ContextRecord^.Eip;
  444. exceptError[exceptLevel] := err;
  445. resetFPU[exceptLevel] := must_reset_fpu;
  446. inc(exceptLevel);
  447. excep^.ContextRecord^.Eip := Longint(@JumpToHandleErrorFrame);
  448. excep^.ExceptionRecord^.ExceptionCode := 0;
  449. res := EXCEPTION_CONTINUE_EXECUTION;
  450. {$ifdef SYSTEMEXCEPTIONDEBUG}
  451. if IsConsole then begin
  452. writeln(stderr,'Exception Continue Exception set at ',
  453. hexstr(exceptEip[exceptLevel],8));
  454. writeln(stderr,'Eip changed to ',
  455. hexstr(longint(@JumpToHandleErrorFrame),8), ' error=', err);
  456. end;
  457. {$endif SYSTEMEXCEPTIONDEBUG}
  458. end;
  459. end;
  460. syswin32_i386_exception_handler := res;
  461. end;
  462. procedure install_exception_handlers;
  463. {$ifdef SYSTEMEXCEPTIONDEBUG}
  464. var
  465. oldexceptaddr,
  466. newexceptaddr : Longint;
  467. {$endif SYSTEMEXCEPTIONDEBUG}
  468. begin
  469. {$ifdef SYSTEMEXCEPTIONDEBUG}
  470. asm
  471. movl $0,%eax
  472. movl %fs:(%eax),%eax
  473. movl %eax,oldexceptaddr
  474. end;
  475. {$endif SYSTEMEXCEPTIONDEBUG}
  476. SetUnhandledExceptionFilter(@syswin32_i386_exception_handler);
  477. {$ifdef SYSTEMEXCEPTIONDEBUG}
  478. asm
  479. movl $0,%eax
  480. movl %fs:(%eax),%eax
  481. movl %eax,newexceptaddr
  482. end;
  483. if IsConsole then
  484. writeln(stderr,'Old exception ',hexstr(oldexceptaddr,8),
  485. ' new exception ',hexstr(newexceptaddr,8));
  486. {$endif SYSTEMEXCEPTIONDEBUG}
  487. end;
  488. procedure remove_exception_handlers;
  489. begin
  490. SetUnhandledExceptionFilter(nil);
  491. end;
  492. {$else not cpui386 (Processor specific !!)}
  493. procedure install_exception_handlers;
  494. begin
  495. end;
  496. procedure remove_exception_handlers;
  497. begin
  498. end;
  499. {$endif Set_i386_Exception_handler}
  500. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  501. type
  502. tdosheader = packed record
  503. e_magic : word;
  504. e_cblp : word;
  505. e_cp : word;
  506. e_crlc : word;
  507. e_cparhdr : word;
  508. e_minalloc : word;
  509. e_maxalloc : word;
  510. e_ss : word;
  511. e_sp : word;
  512. e_csum : word;
  513. e_ip : word;
  514. e_cs : word;
  515. e_lfarlc : word;
  516. e_ovno : word;
  517. e_res : array[0..3] of word;
  518. e_oemid : word;
  519. e_oeminfo : word;
  520. e_res2 : array[0..9] of word;
  521. e_lfanew : longint;
  522. end;
  523. tpeheader = packed record
  524. PEMagic : longint;
  525. Machine : word;
  526. NumberOfSections : word;
  527. TimeDateStamp : longint;
  528. PointerToSymbolTable : longint;
  529. NumberOfSymbols : longint;
  530. SizeOfOptionalHeader : word;
  531. Characteristics : word;
  532. Magic : word;
  533. MajorLinkerVersion : byte;
  534. MinorLinkerVersion : byte;
  535. SizeOfCode : longint;
  536. SizeOfInitializedData : longint;
  537. SizeOfUninitializedData : longint;
  538. AddressOfEntryPoint : longint;
  539. BaseOfCode : longint;
  540. BaseOfData : longint;
  541. ImageBase : longint;
  542. SectionAlignment : longint;
  543. FileAlignment : longint;
  544. MajorOperatingSystemVersion : word;
  545. MinorOperatingSystemVersion : word;
  546. MajorImageVersion : word;
  547. MinorImageVersion : word;
  548. MajorSubsystemVersion : word;
  549. MinorSubsystemVersion : word;
  550. Reserved1 : longint;
  551. SizeOfImage : longint;
  552. SizeOfHeaders : longint;
  553. CheckSum : longint;
  554. Subsystem : word;
  555. DllCharacteristics : word;
  556. SizeOfStackReserve : longint;
  557. SizeOfStackCommit : longint;
  558. SizeOfHeapReserve : longint;
  559. SizeOfHeapCommit : longint;
  560. LoaderFlags : longint;
  561. NumberOfRvaAndSizes : longint;
  562. DataDirectory : array[1..$80] of byte;
  563. end;
  564. begin
  565. if (SysInstance=0) and not IsLibrary then
  566. SysInstance:=getmodulehandle(nil);
  567. if (SysInstance=0) then
  568. result:=stklen
  569. else
  570. result:=tpeheader((pointer(SysInstance)+(tdosheader(pointer(SysInstance)^).e_lfanew))^).SizeOfStackReserve;
  571. end;
  572. begin
  573. { get some helpful informations }
  574. GetStartupInfo(@startupinfo);
  575. { some misc Win32 stuff }
  576. if not IsLibrary then
  577. SysInstance:=getmodulehandle(nil);
  578. MainInstance:=SysInstance;
  579. { pass dummy value }
  580. StackLength := CheckInitialStkLen($1000000);
  581. StackBottom := StackTop - StackLength;
  582. cmdshow:=startupinfo.wshowwindow;
  583. { Setup heap and threading, these may be already initialized from TLS callback }
  584. if not Assigned(CurrentTM.BeginThread) then
  585. begin
  586. InitHeap;
  587. InitSystemThreads;
  588. end;
  589. SysInitExceptions;
  590. { setup fastmove stuff }
  591. fpc_cpucodeinit;
  592. initwidestringmanager;
  593. initunicodestringmanager;
  594. InitWin32Widestrings;
  595. SysInitStdIO;
  596. { Arguments }
  597. setup_arguments;
  598. { Reset IO Error }
  599. InOutRes:=0;
  600. ProcessID := GetCurrentProcessID;
  601. { Reset internal error variable }
  602. errno:=0;
  603. initvariantmanager;
  604. DispCallByIDProc:=@DoDispCallByIDError;
  605. end.