system.pp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2009 by Sven Barth
  4. FPC Pascal system unit for the WinNT API.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit System;
  12. interface
  13. {$ifdef SYSTEMDEBUG}
  14. {$define SYSTEMEXCEPTIONDEBUG}
  15. {$endif SYSTEMDEBUG}
  16. {.$define FPC_HAS_INDIRECT_MAIN_INFORMATION}
  17. {$ifdef cpui386}
  18. {$define Set_i386_Exception_handler}
  19. {$endif cpui386}
  20. {.$define DISABLE_NO_THREAD_MANAGER}
  21. {$ifdef KMODE}
  22. {$define HAS_MEMORYMANAGER}
  23. {$endif KMODE}
  24. { include system-independent routine headers }
  25. {$I systemh.inc}
  26. var
  27. CurrentPeb: Pointer;
  28. IsDeviceDriver: Boolean = False;
  29. const
  30. LineEnding = #13#10;
  31. LFNSupport = true;
  32. DirectorySeparator = '\';
  33. DriveSeparator = '\';
  34. ExtensionSeparator = '.';
  35. PathSeparator = ';';
  36. AllowDirectorySeparators : set of char = ['\'];
  37. AllowDriveSeparators : set of char = [];
  38. { FileNameCaseSensitive is defined separately below!!! }
  39. maxExitCode = High(LongInt);
  40. MaxPathLen = High(Word);
  41. AllFilesMask = '*';
  42. type
  43. PEXCEPTION_FRAME = ^TEXCEPTION_FRAME;
  44. TEXCEPTION_FRAME = record
  45. next : PEXCEPTION_FRAME;
  46. handler : pointer;
  47. end;
  48. {$ifndef kmode}
  49. type
  50. TDLL_Entry_Hook = procedure (dllparam : longint);
  51. const
  52. Dll_Process_Detach_Hook : TDLL_Entry_Hook = nil;
  53. Dll_Thread_Attach_Hook : TDLL_Entry_Hook = nil;
  54. Dll_Thread_Detach_Hook : TDLL_Entry_Hook = nil;
  55. {$endif}
  56. const
  57. // NT is case sensitive
  58. FileNameCaseSensitive : boolean = true;
  59. // todo: check whether this is really the case on NT
  60. CtrlZMarksEOF: boolean = true; (* #26 is considered as end of file *)
  61. sLineBreak = LineEnding;
  62. System_exception_frame : PEXCEPTION_FRAME =nil;
  63. implementation
  64. { include system independent routines }
  65. {$I system.inc}
  66. procedure KeQueryTickCount(TickCount: PLargeInteger); stdcall; external ntdll name 'KeQueryTickCount';
  67. procedure randomize;
  68. var
  69. tc: PLargeInteger;
  70. begin
  71. FillChar(tc, SizeOf(TLargeInteger), 0);
  72. KeQueryTickCount(@tc);
  73. // the lower part should differ most on system startup
  74. randseed := tc^.LowPart;
  75. end;
  76. {*****************************************************************************
  77. System Dependent Exit code
  78. *****************************************************************************}
  79. procedure PascalMain;stdcall;external name 'PASCALMAIN';
  80. {$ifndef KMODE}
  81. function NtTerminateProcess(aProcess: THandle; aStatus: NTSTATUS): NTSTATUS; stdcall; external ntdll name 'NtTerminateProcess';
  82. {$endif KMODE}
  83. Procedure system_exit;
  84. begin
  85. if IsLibrary or IsDeviceDriver then
  86. Exit;
  87. {$ifndef KMODE}
  88. NtTerminateProcess(THandle(-1), ExitCode);
  89. {$endif KMODE}
  90. end;
  91. {$ifdef kmode}
  92. function FPCDriverStartup(aDriverObject: Pointer; aRegistryPath: Pointer): NTSTATUS; [public, alias: 'FPC_DriverStartup'];
  93. begin
  94. IsDeviceDriver := True;
  95. IsConsole := True;
  96. IsLibrary := True;
  97. SysDriverObject := aDriverObject;
  98. SysRegistryPath := aRegistryPath;
  99. PASCALMAIN;
  100. SysDriverObject := Nil;
  101. SysRegistryPath := Nil;
  102. Result := ExitCode;
  103. end;
  104. {$else}
  105. const
  106. DLL_PROCESS_ATTACH = 1;
  107. DLL_THREAD_ATTACH = 2;
  108. DLL_PROCESS_DETACH = 0;
  109. DLL_THREAD_DETACH = 3;
  110. function FPCDLLEntry(aHInstance: Pointer; aDLLReason: LongInt; aDLLParam: LongInt): LongBool; [public, alias: 'FPC_DLLEntry'];
  111. begin
  112. IsLibrary := True;
  113. FPCDLLEntry := True;
  114. case aDLLReason of
  115. DLL_PROCESS_ATTACH: begin
  116. PascalMain;
  117. FPCDLLEntry := ExitCode = 0;
  118. end;
  119. DLL_THREAD_ATTACH: begin
  120. if Dll_Thread_Attach_Hook <> Nil then
  121. Dll_Thread_Attach_Hook(aDllParam);
  122. end;
  123. DLL_THREAD_DETACH: begin
  124. if Dll_Thread_Detach_Hook <> Nil then
  125. Dll_Thread_Detach_Hook(aDllParam);
  126. end;
  127. DLL_PROCESS_DETACH: begin
  128. if Dll_Process_Detach_Hook <> Nil then
  129. Dll_Process_Detach_Hook(aDllParam);
  130. // finalize units
  131. do_exit;
  132. end;
  133. end;
  134. end;
  135. procedure FPCProcessStartup(aArgument: Pointer);[public, alias: 'FPC_ProcessStartup'];
  136. begin
  137. IsConsole := True;
  138. IsLibrary := False;
  139. CurrentPeb := aArgument;
  140. PASCALMAIN;
  141. system_exit;
  142. end;
  143. {$endif}
  144. {$ifdef kmode}
  145. // Kernel Mode Entry Point
  146. function NtDriverEntry( aDriverObject: Pointer; aRegistryPath: Pointer ): LongInt; stdcall; [public, alias: '_NtDriverEntry'];
  147. begin
  148. NtDriverEntry := FPCDriverStartup(aDriverObject, aRegistryPath);
  149. end;
  150. {$else}
  151. // User Mode Entry Points
  152. procedure NtProcessStartup( aArgument: Pointer ); stdcall; [public, alias: '_NtProcessStartup'];
  153. begin
  154. FPCProcessStartup(aArgument);
  155. end;
  156. function DLLMainStartup( aHInstance: Pointer; aDLLReason, aDLLParam: LongInt ): LongBool; stdcall; [public, alias: '_DLLMainStartup'];
  157. begin
  158. DLLMainStartup := FPCDLLEntry(aHInstance, aDLLReason, aDLLParam);
  159. end;
  160. {$endif}
  161. begin
  162. {$if not defined(KMODE) and not defined(HAS_MEMORYMANAGER)}
  163. { Setup heap }
  164. InitHeap;
  165. {$endif ndef KMODE and ndef HAS_MEMORYMANAGER}
  166. SysInitExceptions;
  167. initvariantmanager;
  168. { we do not use winlike widestrings and also the RTL can't be compiled with
  169. 2.2, so we can savely use the UnicodeString manager only. }
  170. initunicodestringmanager;
  171. end.