system.pp 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856
  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. and Yury Sidorov member of the Free Pascal development team.
  5. FPC Pascal system unit for the WinCE.
  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 WINCE_EXCEPTION_HANDLING}
  18. {$define DISABLE_NO_THREAD_MANAGER}
  19. {$define HAS_CMDLINE}
  20. {$define HAS_MEMORYMANAGER} // comment this line to switch from wincemm to fpcmm
  21. {$define HAS_WIDESTRINGMANAGER}
  22. { include system-independent routine headers }
  23. {$I systemh.inc}
  24. const
  25. LineEnding = #13#10;
  26. LFNSupport = true;
  27. DirectorySeparator = '\';
  28. DriveSeparator = ':';
  29. ExtensionSeparator = '.';
  30. PathSeparator = ';';
  31. AllowDirectorySeparators : set of char = ['\','/'];
  32. AllowDriveSeparators : set of char = [':'];
  33. { FileNameCaseSensitive is defined separately below!!! }
  34. maxExitCode = 65535;
  35. MaxPathLen = 260;
  36. AllFilesMask = '*';
  37. const
  38. { Default filehandles }
  39. UnusedHandle : THandle = THandle(-1);
  40. StdInputHandle : THandle = 0;
  41. StdOutputHandle : THandle = 0;
  42. StdErrorHandle : THandle = 0;
  43. FileNameCaseSensitive : boolean = true;
  44. CtrlZMarksEOF: boolean = true; (* #26 is considered as end of file *)
  45. sLineBreak = LineEnding;
  46. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsCRLF;
  47. var
  48. { WinCE Info }
  49. hprevinst,
  50. MainInstance,
  51. DLLreason,DLLparam:DWord;
  52. type
  53. TDLL_Entry_Hook = procedure (dllparam : longint);
  54. const
  55. Dll_Process_Detach_Hook : TDLL_Entry_Hook = nil;
  56. Dll_Thread_Attach_Hook : TDLL_Entry_Hook = nil;
  57. Dll_Thread_Detach_Hook : TDLL_Entry_Hook = nil;
  58. { ANSI <-> Wide }
  59. function AnsiToWideBuf(AnsiBuf: PChar; AnsiBufLen: longint; WideBuf: PWideChar; WideBufLen: longint): longint;
  60. function WideToAnsiBuf(WideBuf: PWideChar; WideCharsLen: longint; AnsiBuf: PChar; AnsiBufLen: longint): longint;
  61. function PCharToPWideChar(str: PChar; strlen: longint = -1; outlen: PLongInt = nil): PWideChar;
  62. function StringToPWideChar(const s: AnsiString; outlen: PLongInt = nil): PWideChar;
  63. { Wrappers for some WinAPI calls }
  64. function CreateEvent(lpEventAttributes:pointer;bManualReset:longbool;bInitialState:longbool;lpName:pchar): THandle;
  65. function ResetEvent(h: THandle): LONGBOOL;
  66. function SetEvent(h: THandle): LONGBOOL;
  67. function GetCurrentProcessId:DWORD;
  68. function Win32GetCurrentThreadId:DWORD;
  69. function TlsAlloc : DWord;
  70. function TlsFree(dwTlsIndex : DWord) : LongBool;
  71. function GetFileAttributes(p : pchar) : dword;
  72. function DeleteFile(p : pchar) : longint;
  73. function MoveFile(old,_new : pchar) : longint;
  74. function CreateFile(lpFileName:pchar; dwDesiredAccess:DWORD; dwShareMode:DWORD;
  75. lpSecurityAttributes:pointer; dwCreationDisposition:DWORD;
  76. dwFlagsAndAttributes:DWORD; hTemplateFile:DWORD):longint;
  77. function CreateDirectory(name : pointer;sec : pointer) : longbool;
  78. function RemoveDirectory(name:pointer):longbool;
  79. {$ifdef CPUARM}
  80. { the external directive isn't really necessary here because it is overridden by external (FK) }
  81. function addd(d1,d2 : double) : double; compilerproc;
  82. cdecl;external 'coredll' name '__addd';
  83. function subd(d1,d2 : double) : double; compilerproc;
  84. cdecl;external 'coredll' name '__subd';
  85. function muld(d1,d2 : double) : double; compilerproc;
  86. cdecl;external 'coredll' name '__muld';
  87. function divd(d1,d2 : double) : double; compilerproc;
  88. cdecl;external 'coredll' name '__divd';
  89. function eqd(d1,d2 : double) : boolean; compilerproc;
  90. cdecl;external 'coredll' name '__eqd';
  91. function ned(d1,d2 : double) : boolean; compilerproc;
  92. cdecl;external 'coredll' name '__ned';
  93. function ltd(d1,d2 : double) : boolean; compilerproc;
  94. cdecl;external 'coredll' name '__ltd';
  95. function gtd(d1,d2 : double) : boolean; compilerproc;
  96. cdecl;external 'coredll' name '__gtd';
  97. function ged(d1,d2 : double) : boolean; compilerproc;
  98. cdecl;external 'coredll' name '__ged';
  99. function led(d1,d2 : double) : boolean; compilerproc;
  100. cdecl;external 'coredll' name '__led';
  101. { ***************** single ******************** }
  102. function eqs(d1,d2 : single) : boolean; compilerproc;
  103. cdecl;external 'coredll' name '__eqs';
  104. function nes(d1,d2 : single) : boolean; compilerproc;
  105. cdecl;external 'coredll' name '__nes';
  106. function lts(d1,d2 : single) : boolean; compilerproc;
  107. cdecl;external 'coredll' name '__lts';
  108. function gts(d1,d2 : single) : boolean; compilerproc;
  109. cdecl;external 'coredll' name '__gts';
  110. function ges(d1,d2 : single) : boolean; compilerproc;
  111. cdecl;external 'coredll' name '__ges';
  112. function les(d1,d2 : single) : boolean; compilerproc;
  113. cdecl;external 'coredll' name '__les';
  114. function dtos(d : double) : single; compilerproc;
  115. cdecl;external 'coredll' name '__dtos';
  116. function stod(d : single) : double; compilerproc;
  117. cdecl;external 'coredll' name '__stod';
  118. function negs(d : single) : single; compilerproc;
  119. cdecl;external 'coredll' name '__negs';
  120. function negd(d : double) : double; compilerproc;
  121. cdecl;external 'coredll' name '__negd';
  122. function utod(i : dword) : double; compilerproc;
  123. cdecl;external 'coredll' name '__utod';
  124. function itod(i : longint) : double; compilerproc;
  125. cdecl;external 'coredll' name '__itod';
  126. function ui64tod(i : qword) : double; compilerproc;
  127. cdecl;external 'coredll' name '__u64tod';
  128. function i64tod(i : int64) : double; compilerproc;
  129. cdecl;external 'coredll' name '__i64tod';
  130. function utos(i : dword) : single; compilerproc;
  131. cdecl;external 'coredll' name '__utos';
  132. function itos(i : longint) : single; compilerproc;
  133. cdecl;external 'coredll' name '__itos';
  134. function ui64tos(i : qword) : single; compilerproc;
  135. cdecl;external 'coredll' name '__u64tos';
  136. function i64tos(i : int64) : single; compilerproc;
  137. cdecl;external 'coredll' name '__i64tos';
  138. function adds(s1,s2 : single) : single; compilerproc;
  139. function subs(s1,s2 : single) : single; compilerproc;
  140. function muls(s1,s2 : single) : single; compilerproc;
  141. function divs(s1,s2 : single) : single; compilerproc;
  142. {$endif CPUARM}
  143. function CmdLine: PChar;
  144. { C compatible arguments }
  145. function argc: longint;
  146. function argv: ppchar;
  147. implementation
  148. var
  149. SysInstance : Longint;public name '_FPC_SysInstance';
  150. function MessageBox(w1:longint;l1,l2:PWideChar;w2:longint):longint;
  151. cdecl; external 'coredll' name 'MessageBoxW';
  152. {*****************************************************************************}
  153. {$define FPC_SYSTEM_HAS_MOVE}
  154. procedure memmove(dest, src: pointer; count: longint);
  155. cdecl; external 'coredll' name 'memmove';
  156. procedure Move(const source;var dest;count:SizeInt);[public, alias: 'FPC_MOVE'];
  157. begin
  158. if count > 0 then
  159. memmove(@dest, @source, count);
  160. end;
  161. {$define FPC_SYSTEM_HAS_COMPAREBYTE}
  162. function memcmp(buf1, buf2: pointer; count: longint): longint;
  163. cdecl; external 'coredll' name 'memcmp';
  164. function CompareByte(Const buf1,buf2;len:SizeInt):SizeInt;
  165. begin
  166. CompareByte := memcmp(@buf1, @buf2, len);
  167. end;
  168. {$ifdef CPUARM}
  169. {$define FPC_SYSTEM_HAS_INT}
  170. function floor(d : double) : double;
  171. cdecl;external 'coredll' name 'floor';
  172. function ceil(d : double) : double;
  173. cdecl;external 'coredll' name 'ceil';
  174. function fpc_int_real(d: ValReal): ValReal;compilerproc;
  175. begin
  176. if d > 0 then
  177. fpc_int_real:=floor(d)
  178. else
  179. fpc_int_real:=ceil(d);
  180. end;
  181. {$define FPC_SYSTEM_HAS_TRUNC}
  182. function __dtoi64(d: double) : int64; cdecl; external 'coredll';
  183. function fpc_trunc_real(d : ValReal) : int64; assembler; nostackframe; compilerproc;
  184. asm
  185. b __dtoi64
  186. end;
  187. {$define FPC_SYSTEM_HAS_ABS}
  188. function fabs(d: double): double; cdecl; external 'coredll';
  189. function fpc_abs_real(d : ValReal) : ValReal; assembler; nostackframe; compilerproc;
  190. asm
  191. b fabs
  192. end;
  193. {$define FPC_SYSTEM_HAS_SQRT}
  194. function coresqrt(d: double): double; cdecl; external 'coredll' name 'sqrt';
  195. function fpc_sqrt_real(d : ValReal) : ValReal; assembler; nostackframe; compilerproc;
  196. asm
  197. b coresqrt
  198. end;
  199. function adds(s1,s2 : single) : single;
  200. begin
  201. adds := addd(s1, s2);
  202. end;
  203. function subs(s1,s2 : single) : single;
  204. begin
  205. subs := subd(s1, s2);
  206. end;
  207. function muls(s1,s2 : single) : single;
  208. begin
  209. muls := muld(s1, s2);
  210. end;
  211. function divs(s1,s2 : single) : single;
  212. begin
  213. divs := divd(s1, s2);
  214. end;
  215. {$endif CPUARM}
  216. {*****************************************************************************}
  217. { include system independent routines }
  218. {$I system.inc}
  219. {*****************************************************************************
  220. ANSI <-> Wide
  221. *****************************************************************************}
  222. const
  223. { MultiByteToWideChar }
  224. MB_PRECOMPOSED = 1;
  225. MB_COMPOSITE = 2;
  226. MB_ERR_INVALID_CHARS = 8;
  227. MB_USEGLYPHCHARS = 4;
  228. CP_OEMCP = 1;
  229. function MultiByteToWideChar(CodePage:UINT; dwFlags:DWORD; lpMultiByteStr:PChar; cchMultiByte:longint; lpWideCharStr:PWideChar;cchWideChar:longint):longint;
  230. cdecl; external 'coredll' name 'MultiByteToWideChar';
  231. function WideCharToMultiByte(CodePage:UINT; dwFlags:DWORD; lpWideCharStr:PWideChar; cchWideChar:longint; lpMultiByteStr:PChar;cchMultiByte:longint; lpDefaultChar:PChar; lpUsedDefaultChar:pointer):longint;
  232. cdecl; external 'coredll' name 'WideCharToMultiByte';
  233. function GetACP:UINT; cdecl; external 'coredll' name 'GetACP';
  234. function GetConsoleCP:UINT; cdecl; external 'coredll' name 'GetConsoleCP';
  235. function GetConsoleOutputCP:UINT; cdecl; external 'coredll' name 'GetConsoleOutputCP';
  236. { Returns number of characters stored to WideBuf, including null-terminator. }
  237. function AnsiToWideBuf(AnsiBuf: PChar; AnsiBufLen: longint; WideBuf: PWideChar; WideBufLen: longint): longint;
  238. begin
  239. Result := MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, AnsiBuf, AnsiBufLen, WideBuf, WideBufLen div SizeOf(WideChar));
  240. if ((AnsiBufLen <> -1) or (Result = 0)) and (WideBuf <> nil) then
  241. begin
  242. if (Result + 1)*SizeOf(WideChar) > WideBufLen then
  243. begin
  244. Result := 0;
  245. if WideBufLen < SizeOf(WideChar) then
  246. exit;
  247. end;
  248. WideBuf[Result] := #0;
  249. end;
  250. if (AnsiBufLen <> -1) and ((Result <> 0) or (AnsiBufLen = 0)) then
  251. Inc(Result);
  252. end;
  253. { Returns number of characters stored to AnsiBuf, including null-terminator. }
  254. function WideToAnsiBuf(WideBuf: PWideChar; WideCharsLen: longint; AnsiBuf: PChar; AnsiBufLen: longint): longint;
  255. begin
  256. Result := WideCharToMultiByte(CP_ACP, 0, WideBuf, WideCharsLen, AnsiBuf, AnsiBufLen, nil, nil);
  257. if ((WideCharsLen <> -1) or (Result = 0)) and (AnsiBuf <> nil) then
  258. begin
  259. if Result + 1 > AnsiBufLen then
  260. begin
  261. Result := 0;
  262. if AnsiBufLen < 1 then
  263. exit;
  264. end;
  265. AnsiBuf[Result] := #0;
  266. end;
  267. if (WideCharsLen <> -1) and ((Result <> 0) or (WideCharsLen = 0)) then
  268. Inc(Result);
  269. end;
  270. { Returns dynamic memory block, which contains wide string. This memory should be freed using FreeMem. }
  271. { outlen will contain number of wide characters stored to result buffer, including null-terminator. }
  272. function PCharToPWideChar(str: PChar; strlen: longint = -1; outlen: PLongInt = nil): PWideChar;
  273. var
  274. len: longint;
  275. begin
  276. while True do begin
  277. if strlen <> -1 then
  278. len:=strlen + 1
  279. else
  280. len:=AnsiToWideBuf(str, -1, nil, 0);
  281. if len > 0 then
  282. begin
  283. len:=len*SizeOf(WideChar);
  284. GetMem(Result, len);
  285. len:=AnsiToWideBuf(str, strlen, Result, len);
  286. if (len = 0) and (strlen <> -1) then
  287. begin
  288. FreeMem(Result);
  289. strlen:=-1;
  290. continue;
  291. end;
  292. end
  293. else begin
  294. GetMem(Result, SizeOf(WideChar));
  295. len:=1;
  296. Result^:=#0;
  297. end;
  298. break;
  299. end;
  300. if outlen <> nil then
  301. outlen^:=len;
  302. end;
  303. { Returns dynamic memory block, which contains wide string. This memory should be freed using FreeMem. }
  304. { outlen will contain number of wide characters stored to result buffer, including null-terminator. }
  305. function StringToPWideChar(const s: AnsiString; outlen: PLongInt = nil): PWideChar;
  306. var
  307. len, wlen: longint;
  308. begin
  309. len:=Length(s);
  310. wlen:=(len + 1)*SizeOf(WideChar);
  311. GetMem(Result, wlen);
  312. wlen:=AnsiToWideBuf(PChar(s), len, Result, wlen)*SizeOf(WideChar);
  313. if wlen = 0 then
  314. begin
  315. wlen:=AnsiToWideBuf(PChar(s), len, nil, 0)*SizeOf(WideChar);
  316. if wlen > 0 then
  317. begin
  318. ReAllocMem(Result, wlen);
  319. wlen:=AnsiToWideBuf(PChar(s), len, Result, wlen)*SizeOf(WideChar);
  320. end
  321. else
  322. begin
  323. Result^:=#0;
  324. wlen:=SizeOf(WideChar);
  325. end;
  326. end;
  327. if outlen <> nil then
  328. outlen^:=wlen div SizeOf(WideChar);
  329. end;
  330. {*****************************************************************************
  331. WinAPI wrappers implementation
  332. *****************************************************************************}
  333. function GetFileAttributesW(p : pwidechar) : dword;
  334. cdecl; external KernelDLL name 'GetFileAttributesW';
  335. function DeleteFileW(p : pwidechar) : longint;
  336. cdecl; external KernelDLL name 'DeleteFileW';
  337. function MoveFileW(old,_new : pwidechar) : longint;
  338. cdecl; external KernelDLL name 'MoveFileW';
  339. function CreateFileW(lpFileName:pwidechar; dwDesiredAccess:DWORD; dwShareMode:DWORD;
  340. lpSecurityAttributes:pointer; dwCreationDisposition:DWORD;
  341. dwFlagsAndAttributes:DWORD; hTemplateFile:DWORD):longint;
  342. cdecl; external KernelDLL name 'CreateFileW';
  343. function CreateDirectoryW(name : pwidechar;sec : pointer) : longbool;
  344. cdecl; external KernelDLL name 'CreateDirectoryW';
  345. function RemoveDirectoryW(name:pwidechar):longbool;
  346. cdecl; external KernelDLL name 'RemoveDirectoryW';
  347. function GetFileAttributes(p : pchar) : dword;
  348. var
  349. buf: array[0..MaxPathLen] of WideChar;
  350. begin
  351. AnsiToWideBuf(p, -1, buf, SizeOf(buf));
  352. GetFileAttributes := GetFileAttributesW(buf);
  353. end;
  354. function DeleteFile(p : pchar) : longint;
  355. var
  356. buf: array[0..MaxPathLen] of WideChar;
  357. begin
  358. AnsiToWideBuf(p, -1, buf, SizeOf(buf));
  359. DeleteFile := DeleteFileW(buf);
  360. end;
  361. function MoveFile(old,_new : pchar) : longint;
  362. var
  363. buf_old, buf_new: array[0..MaxPathLen] of WideChar;
  364. begin
  365. AnsiToWideBuf(old, -1, buf_old, SizeOf(buf_old));
  366. AnsiToWideBuf(_new, -1, buf_new, SizeOf(buf_new));
  367. MoveFile := MoveFileW(buf_old, buf_new);
  368. end;
  369. function CreateFile(lpFileName:pchar; dwDesiredAccess:DWORD; dwShareMode:DWORD;
  370. lpSecurityAttributes:pointer; dwCreationDisposition:DWORD;
  371. dwFlagsAndAttributes:DWORD; hTemplateFile:DWORD):longint;
  372. var
  373. buf: array[0..MaxPathLen] of WideChar;
  374. begin
  375. AnsiToWideBuf(lpFileName, -1, buf, SizeOf(buf));
  376. CreateFile := CreateFileW(buf, dwDesiredAccess, dwShareMode, lpSecurityAttributes,
  377. dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
  378. end;
  379. function CreateDirectory(name : pointer;sec : pointer) : longbool;
  380. var
  381. buf: array[0..MaxPathLen] of WideChar;
  382. begin
  383. AnsiToWideBuf(name, -1, buf, SizeOf(buf));
  384. CreateDirectory := CreateDirectoryW(buf, sec);
  385. end;
  386. function RemoveDirectory(name:pointer):longbool;
  387. var
  388. buf: array[0..MaxPathLen] of WideChar;
  389. begin
  390. AnsiToWideBuf(name, -1, buf, SizeOf(buf));
  391. RemoveDirectory := RemoveDirectoryW(buf);
  392. end;
  393. const
  394. {$ifdef CPUARM}
  395. UserKData = $FFFFC800;
  396. {$else CPUARM}
  397. UserKData = $00005800;
  398. {$endif CPUARM}
  399. SYSHANDLE_OFFSET = $004;
  400. SYS_HANDLE_BASE = 64;
  401. SH_CURTHREAD = 1;
  402. SH_CURPROC = 2;
  403. type
  404. PHandle = ^THandle;
  405. const
  406. EVENT_PULSE = 1;
  407. EVENT_RESET = 2;
  408. EVENT_SET = 3;
  409. function CreateEventW(lpEventAttributes:pointer;bManualReset:longbool;bInitialState:longbool;lpName:PWideChar): THandle;
  410. cdecl; external KernelDLL name 'CreateEventW';
  411. function CreateEvent(lpEventAttributes:pointer;bManualReset:longbool;bInitialState:longbool;lpName:pchar): THandle;
  412. var
  413. buf: array[0..MaxPathLen] of WideChar;
  414. begin
  415. if lpName=nil then
  416. CreateEvent := CreateEventW(lpEventAttributes, bManualReset, bInitialState, nil)
  417. else begin
  418. AnsiToWideBuf(lpName, -1, buf, SizeOf(buf));
  419. CreateEvent := CreateEventW(lpEventAttributes, bManualReset, bInitialState, buf);
  420. end;
  421. end;
  422. function EventModify(h: THandle; func: DWORD): LONGBOOL;
  423. cdecl; external KernelDLL name 'EventModify';
  424. function TlsCall(p1, p2: DWORD): DWORD;
  425. cdecl; external KernelDLL name 'TlsCall';
  426. function ResetEvent(h: THandle): LONGBOOL;
  427. begin
  428. ResetEvent := EventModify(h,EVENT_RESET);
  429. end;
  430. function SetEvent(h: THandle): LONGBOOL;
  431. begin
  432. SetEvent := EventModify(h,EVENT_SET);
  433. end;
  434. function GetCurrentProcessId:DWORD;
  435. var
  436. p: PHandle;
  437. begin
  438. p:=PHandle(UserKData+SYSHANDLE_OFFSET + SH_CURPROC*SizeOf(THandle));
  439. GetCurrentProcessId := p^;
  440. end;
  441. function Win32GetCurrentThreadId:DWORD;
  442. var
  443. p: PHandle;
  444. begin
  445. p:=PHandle(UserKData+SYSHANDLE_OFFSET + SH_CURTHREAD*SizeOf(THandle));
  446. Win32GetCurrentThreadId := p^;
  447. end;
  448. const
  449. TLS_FUNCALLOC = 0;
  450. TLS_FUNCFREE = 1;
  451. function TlsAlloc : DWord;
  452. begin
  453. TlsAlloc := TlsCall(TLS_FUNCALLOC, 0);
  454. end;
  455. function TlsFree(dwTlsIndex : DWord) : LongBool;
  456. begin
  457. TlsFree := LongBool(TlsCall(TLS_FUNCFREE, dwTlsIndex));
  458. end;
  459. {*****************************************************************************
  460. Parameter Handling
  461. *****************************************************************************}
  462. function GetCommandLine : pwidechar;
  463. cdecl; external KernelDLL name 'GetCommandLineW';
  464. var
  465. ModuleName : array[0..255] of char;
  466. function GetCommandFile:pchar;
  467. var
  468. buf: array[0..MaxPathLen] of WideChar;
  469. begin
  470. if ModuleName[0] = #0 then begin
  471. GetModuleFileName(0, @buf, SizeOf(buf));
  472. WideToAnsiBuf(buf, -1, @ModuleName, SizeOf(ModuleName));
  473. end;
  474. GetCommandFile:=@ModuleName;
  475. end;
  476. var
  477. Fargc: longint;
  478. Fargv: ppchar;
  479. FCmdLine: PChar;
  480. procedure setup_arguments;
  481. var
  482. arglen,
  483. count : longint;
  484. argstart,
  485. pc,arg : pchar;
  486. quote : char;
  487. argvlen : longint;
  488. procedure allocarg(idx,len:longint);
  489. var
  490. oldargvlen : longint;
  491. begin
  492. if idx>=argvlen then
  493. begin
  494. oldargvlen:=argvlen;
  495. argvlen:=(idx+8) and (not 7);
  496. sysreallocmem(Fargv,argvlen*sizeof(pointer));
  497. fillchar(Fargv[oldargvlen],(argvlen-oldargvlen)*sizeof(pointer),0);
  498. end;
  499. { use realloc to reuse already existing memory }
  500. { always allocate, even if length is zero, since }
  501. { the arg. is still present! }
  502. sysreallocmem(Fargv[idx],len+1);
  503. end;
  504. begin
  505. { create commandline, it starts with the executed filename which is argv[0] }
  506. { WinCE passes the command NOT via the args, but via getmodulefilename}
  507. if FCmdLine <> nil then exit;
  508. argvlen:=0;
  509. pc:=getcommandfile;
  510. Arglen:=0;
  511. while pc[Arglen] <> #0 do
  512. Inc(Arglen);
  513. allocarg(0,arglen);
  514. move(pc^,Fargv[0]^,arglen+1);
  515. { Setup FCmdLine variable }
  516. arg:=PChar(GetCommandLine);
  517. count:=WideToAnsiBuf(PWideChar(arg), -1, nil, 0);
  518. FCmdLine:=SysGetMem(arglen + count + 3);
  519. FCmdLine^:='"';
  520. move(pc^, (FCmdLine + 1)^, arglen);
  521. (FCmdLine + arglen + 1)^:='"';
  522. (FCmdLine + arglen + 2)^:=' ';
  523. WideToAnsiBuf(PWideChar(arg), -1, FCmdLine + arglen + 3, count);
  524. { process arguments }
  525. count:=0;
  526. pc:=FCmdLine;
  527. {$IfDef SYSTEM_DEBUG_STARTUP}
  528. Writeln(stderr,'WinCE GetCommandLine is #',pc,'#');
  529. {$EndIf }
  530. while pc^<>#0 do
  531. begin
  532. { skip leading spaces }
  533. while pc^ in [#1..#32] do
  534. inc(pc);
  535. if pc^=#0 then
  536. break;
  537. { calc argument length }
  538. quote:=' ';
  539. argstart:=pc;
  540. arglen:=0;
  541. while (pc^<>#0) do
  542. begin
  543. case pc^ of
  544. #1..#32 :
  545. begin
  546. if quote<>' ' then
  547. inc(arglen)
  548. else
  549. break;
  550. end;
  551. '"' :
  552. begin
  553. if quote<>'''' then
  554. begin
  555. if pchar(pc+1)^<>'"' then
  556. begin
  557. if quote='"' then
  558. quote:=' '
  559. else
  560. quote:='"';
  561. end
  562. else
  563. inc(pc);
  564. end
  565. else
  566. inc(arglen);
  567. end;
  568. '''' :
  569. begin
  570. if quote<>'"' then
  571. begin
  572. if pchar(pc+1)^<>'''' then
  573. begin
  574. if quote='''' then
  575. quote:=' '
  576. else
  577. quote:='''';
  578. end
  579. else
  580. inc(pc);
  581. end
  582. else
  583. inc(arglen);
  584. end;
  585. else
  586. inc(arglen);
  587. end;
  588. inc(pc);
  589. end;
  590. { copy argument }
  591. { Don't copy the first one, it is already there.}
  592. If Count<>0 then
  593. begin
  594. allocarg(count,arglen);
  595. quote:=' ';
  596. pc:=argstart;
  597. arg:=Fargv[count];
  598. while (pc^<>#0) do
  599. begin
  600. case pc^ of
  601. #1..#32 :
  602. begin
  603. if quote<>' ' then
  604. begin
  605. arg^:=pc^;
  606. inc(arg);
  607. end
  608. else
  609. break;
  610. end;
  611. '"' :
  612. begin
  613. if quote<>'''' then
  614. begin
  615. if pchar(pc+1)^<>'"' then
  616. begin
  617. if quote='"' then
  618. quote:=' '
  619. else
  620. quote:='"';
  621. end
  622. else
  623. inc(pc);
  624. end
  625. else
  626. begin
  627. arg^:=pc^;
  628. inc(arg);
  629. end;
  630. end;
  631. '''' :
  632. begin
  633. if quote<>'"' then
  634. begin
  635. if pchar(pc+1)^<>'''' then
  636. begin
  637. if quote='''' then
  638. quote:=' '
  639. else
  640. quote:='''';
  641. end
  642. else
  643. inc(pc);
  644. end
  645. else
  646. begin
  647. arg^:=pc^;
  648. inc(arg);
  649. end;
  650. end;
  651. else
  652. begin
  653. arg^:=pc^;
  654. inc(arg);
  655. end;
  656. end;
  657. inc(pc);
  658. end;
  659. arg^:=#0;
  660. end;
  661. {$IfDef SYSTEM_DEBUG_STARTUP}
  662. Writeln(stderr,'dos arg ',count,' #',arglen,'#',Fargv[count],'#');
  663. {$EndIf SYSTEM_DEBUG_STARTUP}
  664. inc(count);
  665. end;
  666. { get argc and create an nil entry }
  667. Fargc:=count;
  668. allocarg(argc,0);
  669. { free unused memory }
  670. sysreallocmem(Fargv,(argc+1)*sizeof(pointer));
  671. end;
  672. function CmdLine: PChar;
  673. begin
  674. setup_arguments;
  675. Result:=FCmdLine;
  676. end;
  677. function argc: longint;
  678. begin
  679. setup_arguments;
  680. Result:=Fargc;
  681. end;
  682. function argv: ppchar;
  683. begin
  684. setup_arguments;
  685. Result:=Fargv;
  686. end;
  687. function paramcount : longint;
  688. begin
  689. paramcount := argc - 1;
  690. end;
  691. function paramstr(l : longint) : string;
  692. begin
  693. setup_arguments;
  694. if (l>=0) and (l<Fargc) then
  695. paramstr:=strpas(Fargv[l])
  696. else
  697. paramstr:='';
  698. end;
  699. procedure randomize;
  700. begin
  701. randseed:=GetTickCount;
  702. end;
  703. {*****************************************************************************
  704. System Dependent Exit code
  705. *****************************************************************************}
  706. procedure PascalMain;external name 'PASCALMAIN';
  707. procedure ExitThread(Exitcode : longint); cdecl; external 'coredll';
  708. Procedure system_exit;
  709. begin
  710. if IsLibrary then
  711. exit;
  712. if not IsConsole then
  713. begin
  714. Close(stderr);
  715. Close(stdout);
  716. Close(erroutput);
  717. Close(Input);
  718. Close(Output);
  719. end;
  720. ExitThread(exitcode);
  721. end;
  722. {$ifdef cpu386}
  723. var
  724. { value of the stack segment
  725. to check if the call stack can be written on exceptions }
  726. _SS : Cardinal;
  727. {$endif cpu386}
  728. Const
  729. { DllEntryPoint }
  730. DLL_PROCESS_ATTACH = 1;
  731. DLL_THREAD_ATTACH = 2;
  732. DLL_PROCESS_DETACH = 0;
  733. DLL_THREAD_DETACH = 3;
  734. function Dll_entry : longbool;[public, alias : '_FPC_DLL_Entry'];
  735. begin
  736. IsLibrary:=true;
  737. Dll_entry:=false;
  738. case DLLreason of
  739. DLL_PROCESS_ATTACH :
  740. begin
  741. PASCALMAIN;
  742. Dll_entry:=true;
  743. end;
  744. DLL_THREAD_ATTACH :
  745. begin
  746. { Allocate Threadvars ?!}
  747. if assigned(Dll_Thread_Attach_Hook) then
  748. Dll_Thread_Attach_Hook(DllParam);
  749. end;
  750. DLL_THREAD_DETACH :
  751. begin
  752. if assigned(Dll_Thread_Detach_Hook) then
  753. Dll_Thread_Detach_Hook(DllParam);
  754. { Release Threadvars ?!}
  755. end;
  756. DLL_PROCESS_DETACH :
  757. begin
  758. Lib_Exit;
  759. if assigned(Dll_Process_Detach_Hook) then
  760. Dll_Process_Detach_Hook(DllParam);
  761. end;
  762. end;
  763. end;
  764. {$ifdef WINCE_EXCEPTION_HANDLING}
  765. //
  766. // Hardware exception handling
  767. //
  768. {
  769. Error code definitions for the WinCE API functions
  770. Values are 32 bit values layed out as follows:
  771. 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
  772. 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
  773. +---+-+-+-----------------------+-------------------------------+
  774. |Sev|C|R| Facility | Code |
  775. +---+-+-+-----------------------+-------------------------------+
  776. where
  777. Sev - is the severity code
  778. 00 - Success
  779. 01 - Informational
  780. 10 - Warning
  781. 11 - Error
  782. C - is the Customer code flag
  783. R - is a reserved bit
  784. Facility - is the facility code
  785. Code - is the facility's status code
  786. }
  787. const
  788. SEVERITY_SUCCESS = $00000000;
  789. SEVERITY_INFORMATIONAL = $40000000;
  790. SEVERITY_WARNING = $80000000;
  791. SEVERITY_ERROR = $C0000000;
  792. const
  793. STATUS_SEGMENT_NOTIFICATION = $40000005;
  794. DBG_TERMINATE_THREAD = $40010003;
  795. DBG_TERMINATE_PROCESS = $40010004;
  796. DBG_CONTROL_C = $40010005;
  797. DBG_CONTROL_BREAK = $40010008;
  798. STATUS_GUARD_PAGE_VIOLATION = $80000001;
  799. STATUS_DATATYPE_MISALIGNMENT = $80000002;
  800. STATUS_BREAKPOINT = $80000003;
  801. STATUS_SINGLE_STEP = $80000004;
  802. DBG_EXCEPTION_NOT_HANDLED = $80010001;
  803. STATUS_ACCESS_VIOLATION = $C0000005;
  804. STATUS_IN_PAGE_ERROR = $C0000006;
  805. STATUS_INVALID_HANDLE = $C0000008;
  806. STATUS_NO_MEMORY = $C0000017;
  807. STATUS_ILLEGAL_INSTRUCTION = $C000001D;
  808. STATUS_NONCONTINUABLE_EXCEPTION = $C0000025;
  809. STATUS_INVALID_DISPOSITION = $C0000026;
  810. STATUS_ARRAY_BOUNDS_EXCEEDED = $C000008C;
  811. STATUS_FLOAT_DENORMAL_OPERAND = $C000008D;
  812. STATUS_FLOAT_DIVIDE_BY_ZERO = $C000008E;
  813. STATUS_FLOAT_INEXACT_RESULT = $C000008F;
  814. STATUS_FLOAT_INVALID_OPERATION = $C0000090;
  815. STATUS_FLOAT_OVERFLOW = $C0000091;
  816. STATUS_FLOAT_STACK_CHECK = $C0000092;
  817. STATUS_FLOAT_UNDERFLOW = $C0000093;
  818. STATUS_INTEGER_DIVIDE_BY_ZERO = $C0000094;
  819. STATUS_INTEGER_OVERFLOW = $C0000095;
  820. STATUS_PRIVILEGED_INSTRUCTION = $C0000096;
  821. STATUS_STACK_OVERFLOW = $C00000FD;
  822. STATUS_CONTROL_C_EXIT = $C000013A;
  823. STATUS_FLOAT_MULTIPLE_FAULTS = $C00002B4;
  824. STATUS_FLOAT_MULTIPLE_TRAPS = $C00002B5;
  825. STATUS_REG_NAT_CONSUMPTION = $C00002C9;
  826. const
  827. ExceptionContinueExecution = 0;
  828. ExceptionContinueSearch = 1;
  829. ExceptionNestedException = 2;
  830. ExceptionCollidedUnwind = 3;
  831. ExceptionExecuteHandler = 4;
  832. MaxExceptionLevel = 16;
  833. exceptLevel : Byte = 0;
  834. {$ifdef CPUARM}
  835. const
  836. CONTEXT_ARM = $0000040;
  837. CONTEXT_CONTROL = CONTEXT_ARM or $00000001;
  838. CONTEXT_INTEGER = CONTEXT_ARM or $00000002;
  839. CONTEXT_SEGMENTS = CONTEXT_ARM or $00000004;
  840. CONTEXT_FLOATING_POINT = CONTEXT_ARM or $00000008;
  841. CONTEXT_DEBUG_REGISTERS = CONTEXT_ARM or $00000010;
  842. CONTEXT_EXTENDED_REGISTERS = CONTEXT_ARM or $00000020;
  843. CONTEXT_FULL = CONTEXT_CONTROL or CONTEXT_INTEGER or CONTEXT_SEGMENTS;
  844. NUM_VFP_REGS = 32;
  845. NUM_EXTRA_CONTROL_REGS = 8;
  846. type
  847. PContext = ^TContext;
  848. TContext = record
  849. ContextFlags : LongWord;
  850. // This section is specified/returned if the ContextFlags word contains
  851. // the flag CONTEXT_INTEGER.
  852. R0 : LongWord;
  853. R1 : LongWord;
  854. R2 : LongWord;
  855. R3 : LongWord;
  856. R4 : LongWord;
  857. R5 : LongWord;
  858. R6 : LongWord;
  859. R7 : LongWord;
  860. R8 : LongWord;
  861. R9 : LongWord;
  862. R10 : LongWord;
  863. R11 : LongWord;
  864. R12 : LongWord;
  865. // This section is specified/returned if the ContextFlags word contains
  866. // the flag CONTEXT_CONTROL.
  867. Sp : LongWord;
  868. Lr : LongWord;
  869. Pc : LongWord;
  870. Psr : LongWord;
  871. Fpscr : LongWord;
  872. FpExc : LongWord;
  873. // Floating point registers
  874. S : array[0..(NUM_VFP_REGS + 1)-1] of LongWord;
  875. FpExtra : array[0..(NUM_EXTRA_CONTROL_REGS)-1] of LongWord;
  876. end;
  877. {$endif CPUARM}
  878. {$ifdef CPUI386}
  879. const
  880. CONTEXT_X86 = $00010000;
  881. CONTEXT_CONTROL = CONTEXT_X86 or $00000001;
  882. CONTEXT_INTEGER = CONTEXT_X86 or $00000002;
  883. CONTEXT_SEGMENTS = CONTEXT_X86 or $00000004;
  884. CONTEXT_FLOATING_POINT = CONTEXT_X86 or $00000008;
  885. CONTEXT_DEBUG_REGISTERS = CONTEXT_X86 or $00000010;
  886. CONTEXT_EXTENDED_REGISTERS = CONTEXT_X86 or $00000020;
  887. MAXIMUM_SUPPORTED_EXTENSION = 512;
  888. type
  889. PFloatingSaveArea = ^TFloatingSaveArea;
  890. TFloatingSaveArea = packed record
  891. ControlWord : Cardinal;
  892. StatusWord : Cardinal;
  893. TagWord : Cardinal;
  894. ErrorOffset : Cardinal;
  895. ErrorSelector : Cardinal;
  896. DataOffset : Cardinal;
  897. DataSelector : Cardinal;
  898. RegisterArea : array[0..79] of Byte;
  899. Cr0NpxState : Cardinal;
  900. end;
  901. PContext = ^TContext;
  902. TContext = packed record
  903. //
  904. // The flags values within this flag control the contents of
  905. // a CONTEXT record.
  906. //
  907. ContextFlags : Cardinal;
  908. //
  909. // This section is specified/returned if CONTEXT_DEBUG_REGISTERS is
  910. // set in ContextFlags. Note that CONTEXT_DEBUG_REGISTERS is NOT
  911. // included in CONTEXT_FULL.
  912. //
  913. Dr0, Dr1, Dr2,
  914. Dr3, Dr6, Dr7 : Cardinal;
  915. //
  916. // This section is specified/returned if the
  917. // ContextFlags word contains the flag CONTEXT_FLOATING_POINT.
  918. //
  919. FloatSave : TFloatingSaveArea;
  920. //
  921. // This section is specified/returned if the
  922. // ContextFlags word contains the flag CONTEXT_SEGMENTS.
  923. //
  924. SegGs, SegFs,
  925. SegEs, SegDs : Cardinal;
  926. //
  927. // This section is specified/returned if the
  928. // ContextFlags word contains the flag CONTEXT_INTEGER.
  929. //
  930. Edi, Esi, Ebx,
  931. Edx, Ecx, Eax : Cardinal;
  932. //
  933. // This section is specified/returned if the
  934. // ContextFlags word contains the flag CONTEXT_CONTROL.
  935. //
  936. Ebp : Cardinal;
  937. Eip : Cardinal;
  938. SegCs : Cardinal;
  939. EFlags, Esp, SegSs : Cardinal;
  940. //
  941. // This section is specified/returned if the ContextFlags word
  942. // contains the flag CONTEXT_EXTENDED_REGISTERS.
  943. // The format and contexts are processor specific
  944. //
  945. ExtendedRegisters : array[0..MAXIMUM_SUPPORTED_EXTENSION-1] of Byte;
  946. end;
  947. {$endif CPUI386}
  948. type
  949. PExceptionPointers = ^TExceptionPointers;
  950. TExceptionPointers = packed record
  951. ExceptionRecord : PExceptionRecord;
  952. ContextRecord : PContext;
  953. end;
  954. {$ifdef CPUI386}
  955. {**************************** i386 Exception handling *****************************************}
  956. function GetCurrentProcess:DWORD;
  957. begin
  958. GetCurrentProcess := SH_CURPROC+SYS_HANDLE_BASE;
  959. end;
  960. function ReadProcessMemory(process : dword;address : pointer;dest : pointer;size : dword;bytesread : pdword) : longbool;
  961. cdecl; external 'coredll' name 'ReadProcessMemory';
  962. function is_prefetch(p : pointer) : boolean;
  963. var
  964. a : array[0..15] of byte;
  965. doagain : boolean;
  966. instrlo,instrhi,opcode : byte;
  967. i : longint;
  968. begin
  969. result:=false;
  970. { read memory savely without causing another exeception }
  971. if not(ReadProcessMemory(GetCurrentProcess,p,@a,sizeof(a),nil)) then
  972. exit;
  973. i:=0;
  974. doagain:=true;
  975. while doagain and (i<15) do
  976. begin
  977. opcode:=a[i];
  978. instrlo:=opcode and $f;
  979. instrhi:=opcode and $f0;
  980. case instrhi of
  981. { prefix? }
  982. $20,$30:
  983. doagain:=(instrlo and 7)=6;
  984. $60:
  985. doagain:=(instrlo and $c)=4;
  986. $f0:
  987. doagain:=instrlo in [0,2,3];
  988. $0:
  989. begin
  990. result:=(instrlo=$f) and (a[i+1] in [$d,$18]);
  991. exit;
  992. end;
  993. else
  994. doagain:=false;
  995. end;
  996. inc(i);
  997. end;
  998. end;
  999. var
  1000. exceptEip : array[0..MaxExceptionLevel-1] of Longint;
  1001. exceptError : array[0..MaxExceptionLevel-1] of Byte;
  1002. resetFPU : array[0..MaxExceptionLevel-1] of Boolean;
  1003. {$ifdef SYSTEMEXCEPTIONDEBUG}
  1004. procedure DebugHandleErrorAddrFrame(error, addr, frame : longint);
  1005. begin
  1006. if IsConsole then
  1007. begin
  1008. write(stderr,'HandleErrorAddrFrame(error=',error);
  1009. write(stderr,',addr=',hexstr(addr,8));
  1010. writeln(stderr,',frame=',hexstr(frame,8),')');
  1011. end;
  1012. HandleErrorAddrFrame(error,addr,frame);
  1013. end;
  1014. {$endif SYSTEMEXCEPTIONDEBUG}
  1015. procedure JumpToHandleErrorFrame;
  1016. var
  1017. eip, ebp, error : Longint;
  1018. begin
  1019. // save ebp
  1020. asm
  1021. movl (%ebp),%eax
  1022. movl %eax,ebp
  1023. end;
  1024. if (exceptLevel > 0) then
  1025. dec(exceptLevel);
  1026. eip:=exceptEip[exceptLevel];
  1027. error:=exceptError[exceptLevel];
  1028. {$ifdef SYSTEMEXCEPTIONDEBUG}
  1029. if IsConsole then
  1030. writeln(stderr,'In JumpToHandleErrorFrame error=',error);
  1031. {$endif SYSTEMEXCEPTIONDEBUG}
  1032. if resetFPU[exceptLevel] then
  1033. SysResetFPU;
  1034. { build a fake stack }
  1035. asm
  1036. {$ifdef REGCALL}
  1037. movl ebp,%ecx
  1038. movl eip,%edx
  1039. movl error,%eax
  1040. pushl eip
  1041. movl ebp,%ebp // Change frame pointer
  1042. {$else}
  1043. movl ebp,%eax
  1044. pushl %eax
  1045. movl eip,%eax
  1046. pushl %eax
  1047. movl error,%eax
  1048. pushl %eax
  1049. movl eip,%eax
  1050. pushl %eax
  1051. movl ebp,%ebp // Change frame pointer
  1052. {$endif}
  1053. {$ifdef SYSTEMEXCEPTIONDEBUG}
  1054. jmpl DebugHandleErrorAddrFrame
  1055. {$else not SYSTEMEXCEPTIONDEBUG}
  1056. jmpl HandleErrorAddrFrame
  1057. {$endif SYSTEMEXCEPTIONDEBUG}
  1058. end;
  1059. end;
  1060. function i386_exception_handler(ExceptionRecord: PExceptionRecord;
  1061. EstablisherFrame: pointer; ContextRecord: PContext;
  1062. DispatcherContext: pointer): longint; cdecl;
  1063. var
  1064. res: longint;
  1065. must_reset_fpu: boolean;
  1066. begin
  1067. res := ExceptionContinueSearch;
  1068. if ContextRecord^.SegSs=_SS then begin
  1069. must_reset_fpu := true;
  1070. {$ifdef SYSTEMEXCEPTIONDEBUG}
  1071. if IsConsole then Writeln(stderr,'Exception ',
  1072. hexstr(excep^.ExceptionRecord^.ExceptionCode, 8));
  1073. {$endif SYSTEMEXCEPTIONDEBUG}
  1074. case cardinal(ExceptionRecord^.ExceptionCode) of
  1075. STATUS_INTEGER_DIVIDE_BY_ZERO,
  1076. STATUS_FLOAT_DIVIDE_BY_ZERO :
  1077. res := 200;
  1078. STATUS_ARRAY_BOUNDS_EXCEEDED :
  1079. begin
  1080. res := 201;
  1081. must_reset_fpu := false;
  1082. end;
  1083. STATUS_STACK_OVERFLOW :
  1084. begin
  1085. res := 202;
  1086. must_reset_fpu := false;
  1087. end;
  1088. STATUS_FLOAT_OVERFLOW :
  1089. res := 205;
  1090. STATUS_FLOAT_DENORMAL_OPERAND,
  1091. STATUS_FLOAT_UNDERFLOW :
  1092. res := 206;
  1093. {excep^.ContextRecord^.FloatSave.StatusWord := excep^.ContextRecord^.FloatSave.StatusWord and $ffffff00;}
  1094. STATUS_FLOAT_INEXACT_RESULT,
  1095. STATUS_FLOAT_INVALID_OPERATION,
  1096. STATUS_FLOAT_STACK_CHECK :
  1097. res := 207;
  1098. STATUS_INTEGER_OVERFLOW :
  1099. begin
  1100. res := 215;
  1101. must_reset_fpu := false;
  1102. end;
  1103. STATUS_ILLEGAL_INSTRUCTION:
  1104. res := 216;
  1105. STATUS_ACCESS_VIOLATION:
  1106. { Athlon prefetch bug? }
  1107. if is_prefetch(pointer(ContextRecord^.Eip)) then
  1108. begin
  1109. { if yes, then retry }
  1110. ExceptionRecord^.ExceptionCode := 0;
  1111. res:=ExceptionContinueExecution;
  1112. end
  1113. else
  1114. res := 216;
  1115. STATUS_CONTROL_C_EXIT:
  1116. res := 217;
  1117. STATUS_PRIVILEGED_INSTRUCTION:
  1118. begin
  1119. res := 218;
  1120. must_reset_fpu := false;
  1121. end;
  1122. else
  1123. begin
  1124. if ((ExceptionRecord^.ExceptionCode and SEVERITY_ERROR) = SEVERITY_ERROR) then
  1125. res := 217
  1126. else
  1127. res := 255;
  1128. end;
  1129. end;
  1130. if (res >= 200) and (exceptLevel < MaxExceptionLevel) then begin
  1131. exceptEip[exceptLevel] := ContextRecord^.Eip;
  1132. exceptError[exceptLevel] := res;
  1133. resetFPU[exceptLevel] := must_reset_fpu;
  1134. inc(exceptLevel);
  1135. ContextRecord^.Eip := Longint(@JumpToHandleErrorFrame);
  1136. ExceptionRecord^.ExceptionCode := 0;
  1137. res := ExceptionContinueExecution;
  1138. {$ifdef SYSTEMEXCEPTIONDEBUG}
  1139. if IsConsole then begin
  1140. writeln(stderr,'Exception Continue Exception set at ',
  1141. hexstr(exceptEip[exceptLevel],8));
  1142. writeln(stderr,'Eip changed to ',
  1143. hexstr(longint(@JumpToHandleErrorFrame),8), ' error=', error);
  1144. end;
  1145. {$endif SYSTEMEXCEPTIONDEBUG}
  1146. end;
  1147. end;
  1148. i386_exception_handler := res;
  1149. end;
  1150. {$endif CPUI386}
  1151. {$ifdef CPUARM}
  1152. {**************************** ARM Exception handling *****************************************}
  1153. var
  1154. exceptPC : array[0..MaxExceptionLevel-1] of Longint;
  1155. exceptError : array[0..MaxExceptionLevel-1] of Byte;
  1156. procedure JumpToHandleErrorFrame;
  1157. var
  1158. _pc, _fp, _error : Longint;
  1159. begin
  1160. // get original fp
  1161. asm
  1162. ldr r0,[r11,#-12]
  1163. str r0,_fp
  1164. end;
  1165. if (exceptLevel > 0) then
  1166. dec(exceptLevel);
  1167. _pc:=exceptPC[exceptLevel];
  1168. _error:=exceptError[exceptLevel];
  1169. asm
  1170. ldr r0,_error
  1171. ldr r1,_pc
  1172. ldr r2,_fp
  1173. mov r11,r2 // Change frame pointer
  1174. b HandleErrorAddrFrame
  1175. end;
  1176. end;
  1177. function ARM_ExceptionHandler(ExceptionRecord: PExceptionRecord;
  1178. EstablisherFrame: pointer; ContextRecord: PContext;
  1179. DispatcherContext: pointer): longint; [public, alias : '_ARM_ExceptionHandler'];
  1180. var
  1181. res: longint;
  1182. begin
  1183. res := ExceptionContinueSearch;
  1184. case cardinal(ExceptionRecord^.ExceptionCode) of
  1185. STATUS_INTEGER_DIVIDE_BY_ZERO,
  1186. STATUS_FLOAT_DIVIDE_BY_ZERO :
  1187. res := 200;
  1188. STATUS_ARRAY_BOUNDS_EXCEEDED :
  1189. res := 201;
  1190. STATUS_STACK_OVERFLOW :
  1191. res := 202;
  1192. STATUS_FLOAT_OVERFLOW :
  1193. res := 205;
  1194. STATUS_FLOAT_DENORMAL_OPERAND,
  1195. STATUS_FLOAT_UNDERFLOW :
  1196. res := 206;
  1197. STATUS_FLOAT_INEXACT_RESULT,
  1198. STATUS_FLOAT_INVALID_OPERATION,
  1199. STATUS_FLOAT_STACK_CHECK :
  1200. res := 207;
  1201. STATUS_INTEGER_OVERFLOW :
  1202. res := 215;
  1203. STATUS_ILLEGAL_INSTRUCTION:
  1204. res := 216;
  1205. STATUS_ACCESS_VIOLATION:
  1206. res := 216;
  1207. STATUS_DATATYPE_MISALIGNMENT:
  1208. res := 214;
  1209. STATUS_CONTROL_C_EXIT:
  1210. res := 217;
  1211. STATUS_PRIVILEGED_INSTRUCTION:
  1212. res := 218;
  1213. else
  1214. begin
  1215. if ((cardinal(ExceptionRecord^.ExceptionCode) and SEVERITY_ERROR) = SEVERITY_ERROR) then
  1216. res := 217
  1217. else
  1218. res := 255;
  1219. end;
  1220. end;
  1221. if (res <> ExceptionContinueSearch) and (exceptLevel < MaxExceptionLevel) then begin
  1222. exceptPC[exceptLevel] := ContextRecord^.PC;
  1223. exceptError[exceptLevel] := res;
  1224. inc(exceptLevel);
  1225. ContextRecord^.PC := Longint(@JumpToHandleErrorFrame);
  1226. ExceptionRecord^.ExceptionCode := 0;
  1227. res := ExceptionContinueExecution;
  1228. {$ifdef SYSTEMEXCEPTIONDEBUG}
  1229. if IsConsole then begin
  1230. writeln(stderr,'Exception Continue Exception set at ',
  1231. hexstr(exceptEip[exceptLevel],8));
  1232. writeln(stderr,'Eip changed to ',
  1233. hexstr(longint(@JumpToHandleErrorFrame),8), ' error=', error);
  1234. end;
  1235. {$endif SYSTEMEXCEPTIONDEBUG}
  1236. end;
  1237. ARM_ExceptionHandler := res;
  1238. end;
  1239. {$endif CPUARM}
  1240. {$endif WINCE_EXCEPTION_HANDLING}
  1241. procedure Exe_entry;[public, alias : '_FPC_EXE_Entry'];
  1242. var
  1243. st: pointer;
  1244. begin
  1245. IsLibrary:=false;
  1246. {$ifdef CPUARM}
  1247. asm
  1248. str sp,st
  1249. end;
  1250. StackTop:=st;
  1251. asm
  1252. mov fp,#0
  1253. bl PASCALMAIN;
  1254. end;
  1255. {$endif CPUARM}
  1256. {$ifdef CPUI386}
  1257. asm
  1258. {$ifdef WINCE_EXCEPTION_HANDLING}
  1259. pushl i386_exception_handler
  1260. pushl %fs:(0)
  1261. mov %esp,%fs:(0)
  1262. {$endif WINCE_EXCEPTION_HANDLING}
  1263. pushl %ebp
  1264. movl %esp,%eax
  1265. movl %eax,st
  1266. end;
  1267. StackTop:=st;
  1268. asm
  1269. xorl %eax,%eax
  1270. movw %ss,%ax
  1271. movl %eax,_SS
  1272. xorl %ebp,%ebp
  1273. call PASCALMAIN
  1274. popl %ebp
  1275. {$ifdef WINCE_EXCEPTION_HANDLING}
  1276. popl %fs:(0)
  1277. addl $4, %esp
  1278. {$endif WINCE_EXCEPTION_HANDLING}
  1279. end;
  1280. {$endif CPUI386}
  1281. end;
  1282. procedure _FPC_mainCRTStartup;public name '_mainCRTStartup';
  1283. begin
  1284. IsConsole:=True;
  1285. Exe_entry;
  1286. end;
  1287. procedure _FPC_WinMainCRTStartup;public name '_WinMainCRTStartup';
  1288. begin
  1289. IsConsole:=False;
  1290. Exe_entry;
  1291. end;
  1292. procedure _FPC_DLLMainCRTStartup(_hinstance,_dllreason,_dllparam:longint);public name '_DLLMainCRTStartup';
  1293. begin
  1294. IsConsole:=true;
  1295. sysinstance:=_hinstance;
  1296. dllreason:=_dllreason;
  1297. dllparam:=_dllparam;
  1298. DLL_Entry;
  1299. end;
  1300. procedure _FPC_DLLWinMainCRTStartup(_hinstance,_dllreason,_dllparam:longint);public name '_DLLWinMainCRTStartup';
  1301. begin
  1302. IsConsole:=false;
  1303. sysinstance:=_hinstance;
  1304. dllreason:=_dllreason;
  1305. dllparam:=_dllparam;
  1306. DLL_Entry;
  1307. end;
  1308. {****************************************************************************
  1309. OS dependend widestrings
  1310. ****************************************************************************}
  1311. function CharUpperBuff(lpsz:LPWSTR; cchLength:DWORD):DWORD; cdecl; external KernelDLL name 'CharUpperBuffW';
  1312. function CharLowerBuff(lpsz:LPWSTR; cchLength:DWORD):DWORD; cdecl; external KernelDLL name 'CharLowerBuffW';
  1313. procedure WinCEWide2AnsiMove(source:pwidechar;var dest:RawByteString;cp:TSystemCodePage;len:SizeInt);
  1314. var
  1315. destlen: SizeInt;
  1316. begin
  1317. // retrieve length including trailing #0
  1318. // not anymore, because this must also be usable for single characters
  1319. destlen:=WideCharToMultiByte(cp, 0, source, len, nil, 0, nil, nil);
  1320. // this will null-terminate
  1321. setlength(dest, destlen);
  1322. if destlen>0 then
  1323. begin
  1324. WideCharToMultiByte(cp, 0, source, len, @dest[1], destlen, nil, nil);
  1325. PAnsiRec(pointer(dest)-AnsiFirstOff)^.CodePage:=cp;
  1326. end;
  1327. end;
  1328. procedure WinCEAnsi2WideMove(source:pchar;cp:TSystemCodePage;var dest:widestring;len:SizeInt);
  1329. var
  1330. destlen: SizeInt;
  1331. dwFlags: DWORD;
  1332. begin
  1333. if cp=CP_UTF8 then
  1334. dwFlags:=0
  1335. else
  1336. dwFlags:=MB_PRECOMPOSED;
  1337. destlen:=MultiByteToWideChar(cp, dwFlags, source, len, nil, 0);
  1338. // this will null-terminate
  1339. setlength(dest, destlen);
  1340. if destlen>0 then
  1341. MultiByteToWideChar(cp, dwFlags, source, len, @dest[1], destlen);
  1342. end;
  1343. function WinCEWideUpper(const s : WideString) : WideString;
  1344. begin
  1345. result:=s;
  1346. UniqueString(result);
  1347. if length(result)>0 then
  1348. CharUpperBuff(LPWSTR(result),length(result));
  1349. end;
  1350. function WinCEWideLower(const s : WideString) : WideString;
  1351. begin
  1352. result:=s;
  1353. UniqueString(result);
  1354. if length(result)>0 then
  1355. CharLowerBuff(LPWSTR(result),length(result));
  1356. end;
  1357. procedure WinCEUnicode2AnsiMove(source:punicodechar;var dest:RawByteString;cp:TSystemCodePage;len:SizeInt);
  1358. var
  1359. destlen: SizeInt;
  1360. begin
  1361. // retrieve length including trailing #0
  1362. // not anymore, because this must also be usable for single characters
  1363. destlen:=WideCharToMultiByte(cp, 0, source, len, nil, 0, nil, nil);
  1364. // this will null-terminate
  1365. setlength(dest, destlen);
  1366. if destlen>0 then
  1367. begin
  1368. WideCharToMultiByte(cp, 0, source, len, @dest[1], destlen, nil, nil);
  1369. PAnsiRec(pointer(dest)-AnsiFirstOff)^.CodePage:=cp;
  1370. end;
  1371. end;
  1372. procedure WinCEAnsi2UnicodeMove(source:pchar;cp : TSystemCodePage;var dest:UnicodeString;len:SizeInt);
  1373. var
  1374. destlen: SizeInt;
  1375. dwflags: DWORD;
  1376. begin
  1377. if cp=CP_UTF8 then
  1378. dwFlags:=0
  1379. else
  1380. dwFlags:=MB_PRECOMPOSED;
  1381. destlen:=MultiByteToWideChar(cp, dwFlags, source, len, nil, 0);
  1382. // this will null-terminate
  1383. setlength(dest, destlen);
  1384. if destlen>0 then
  1385. begin
  1386. MultiByteToWideChar(cp, dwFlags, source, len, @dest[1], destlen);
  1387. PUnicodeRec(pointer(dest)-UnicodeFirstOff)^.CodePage:=CP_UTF16;
  1388. end;
  1389. end;
  1390. function WinCEUnicodeUpper(const s : UnicodeString) : UnicodeString;
  1391. begin
  1392. Result:=WinCEWideUpper(s);
  1393. end;
  1394. function WinCEUnicodeLower(const s : UnicodeString) : UnicodeString;
  1395. begin
  1396. Result:=WinCEWideLower(s);
  1397. end;
  1398. function WinCEGetStandardCodePage(const stdcp: TStandardCodePageEnum): TSystemCodePage;
  1399. begin
  1400. case stdcp of
  1401. scpAnsi: Result := GetACP;
  1402. scpConsoleInput: Result := GetConsoleCP;
  1403. scpConsoleOutput: Result := GetConsoleOutputCP;
  1404. end;
  1405. end;
  1406. { there is a similiar procedure in sysutils which inits the fields which
  1407. are only relevant for the sysutils units }
  1408. procedure InitWinCEWidestrings;
  1409. begin
  1410. widestringmanager.Wide2AnsiMoveProc:=@WinCEWide2AnsiMove;
  1411. widestringmanager.Ansi2WideMoveProc:=@WinCEAnsi2WideMove;
  1412. widestringmanager.UpperWideStringProc:=@WinCEWideUpper;
  1413. widestringmanager.LowerWideStringProc:=@WinCEWideLower;
  1414. { Unicode }
  1415. widestringmanager.Unicode2AnsiMoveProc:=@WinCEUnicode2AnsiMove;
  1416. widestringmanager.Ansi2UnicodeMoveProc:=@WinCEAnsi2UnicodeMove;
  1417. widestringmanager.UpperUnicodeStringProc:=@WinCEUnicodeUpper;
  1418. widestringmanager.LowerUnicodeStringProc:=@WinCEUnicodeLower;
  1419. { Codepage }
  1420. widestringmanager.GetStandardCodePageProc:=@WinCEGetStandardCodePage;
  1421. DefaultSystemCodePage:=GetACP;
  1422. DefaultUnicodeCodePage:=CP_UTF16;
  1423. end;
  1424. {$IFDEF HAS_MEMORYMANAGER}
  1425. {****************************************************************************
  1426. Memory manager
  1427. ****************************************************************************}
  1428. function malloc(Size : ptruint) : Pointer; cdecl; external 'coredll';
  1429. procedure free(P : pointer); cdecl; external 'coredll';
  1430. function realloc(P : Pointer; Size : ptruint) : pointer; cdecl; external 'coredll';
  1431. function _msize(P : pointer): ptruint; cdecl; external 'coredll';
  1432. function SysGetMem (Size : ptruint) : Pointer;
  1433. begin
  1434. Result:=malloc(Size);
  1435. end;
  1436. Function SysFreeMem (P : pointer) : ptruint;
  1437. begin
  1438. free(P);
  1439. Result:=0;
  1440. end;
  1441. Function SysFreeMemSize(p:pointer;Size:ptruint):ptruint;
  1442. begin
  1443. Result:=0;
  1444. if (size > 0) and (p <> nil) then
  1445. Result:=SysFreeMem(P);
  1446. end;
  1447. Function SysAllocMem(Size : ptruint) : Pointer;
  1448. begin
  1449. Result:=SysGetMem(Size);
  1450. if Result <> nil then
  1451. FillChar(Result^, Size, 0);
  1452. end;
  1453. Function SysReAllocMem (var p:pointer;Size:ptruint):Pointer;
  1454. begin
  1455. Result:=realloc(p, Size);
  1456. p:=Result;
  1457. end;
  1458. function SysTryResizeMem(var p:pointer;size : ptruint):boolean;
  1459. var
  1460. res: pointer;
  1461. begin
  1462. res:=realloc(p, Size);
  1463. Result:=(res <> nil) or (Size = 0);
  1464. if Result then
  1465. p:=res;
  1466. end;
  1467. function SysMemSize(P : pointer): ptruint;
  1468. begin
  1469. Result:=_msize(P);
  1470. end;
  1471. function SysGetHeapStatus:THeapStatus;
  1472. begin
  1473. fillchar(Result,sizeof(Result),0);
  1474. end;
  1475. function SysGetFPCHeapStatus:TFPCHeapStatus;
  1476. begin
  1477. fillchar(Result,sizeof(Result),0);
  1478. end;
  1479. {$ENDIF HAS_MEMORYMANAGER}
  1480. {****************************************************************************
  1481. Error Message writing using messageboxes
  1482. ****************************************************************************}
  1483. const
  1484. ErrorBufferLength = 1024;
  1485. var
  1486. ErrorBuf : array[0..ErrorBufferLength] of char;
  1487. ErrorBufW : array[0..ErrorBufferLength] of widechar;
  1488. ErrorLen : longint;
  1489. Function ErrorWrite(Var F: TextRec): Integer;
  1490. {
  1491. An error message should always end with #13#10#13#10
  1492. }
  1493. var
  1494. i : longint;
  1495. Begin
  1496. while F.BufPos>0 do
  1497. begin
  1498. begin
  1499. if F.BufPos+ErrorLen>ErrorBufferLength then
  1500. i:=ErrorBufferLength-ErrorLen
  1501. else
  1502. i:=F.BufPos;
  1503. Move(F.BufPtr^,ErrorBuf[ErrorLen],i);
  1504. inc(ErrorLen,i);
  1505. ErrorBuf[ErrorLen]:=#0;
  1506. end;
  1507. if ErrorLen=ErrorBufferLength then
  1508. begin
  1509. AnsiToWideBuf(@ErrorBuf, -1, @ErrorBufW, SizeOf(ErrorBufW));
  1510. MessageBox(0,@ErrorBufW,'Error',$10010); { MB_SETFOREGROUND or ICON_ERROR }
  1511. ErrorLen:=0;
  1512. end;
  1513. Dec(F.BufPos,i);
  1514. end;
  1515. ErrorWrite:=0;
  1516. End;
  1517. Function ErrorClose(Var F: TextRec): Integer;
  1518. begin
  1519. if ErrorLen>0 then
  1520. begin
  1521. AnsiToWideBuf(@ErrorBuf, -1, @ErrorBufW, SizeOf(ErrorBufW));
  1522. MessageBox(0,@ErrorBufW,'Error',$10010); { MB_SETFOREGROUND or ICON_ERROR }
  1523. ErrorLen:=0;
  1524. end;
  1525. ErrorLen:=0;
  1526. ErrorClose:=0;
  1527. end;
  1528. Function ErrorOpen(Var F: TextRec): Integer;
  1529. Begin
  1530. TextRec(F).InOutFunc:=@ErrorWrite;
  1531. TextRec(F).FlushFunc:=@ErrorWrite;
  1532. TextRec(F).CloseFunc:=@ErrorClose;
  1533. ErrorLen:=0;
  1534. ErrorOpen:=0;
  1535. End;
  1536. procedure AssignError(Var T: Text);
  1537. begin
  1538. Assign(T,'');
  1539. TextRec(T).OpenFunc:=@ErrorOpen;
  1540. Rewrite(T);
  1541. end;
  1542. function _getstdfilex(fd: integer): pointer; cdecl; external 'coredll';
  1543. function _fileno(fd: pointer): THandle; cdecl; external 'coredll';
  1544. procedure SysInitStdIO;
  1545. begin
  1546. { Setup stdin, stdout and stderr, for GUI apps redirect stderr,stdout to be
  1547. displayed in and messagebox }
  1548. if not IsConsole then
  1549. begin
  1550. AssignError(stderr);
  1551. AssignError(stdout);
  1552. Assign(Output,'');
  1553. Assign(Input,'');
  1554. Assign(ErrOutput,'');
  1555. end
  1556. else
  1557. begin
  1558. StdInputHandle:=_fileno(_getstdfilex(0));
  1559. StdOutputHandle:=_fileno(_getstdfilex(1));
  1560. StdErrorHandle:=_fileno(_getstdfilex(2));
  1561. OpenStdIO(Input,fmInput,StdInputHandle);
  1562. OpenStdIO(Output,fmOutput,StdOutputHandle);
  1563. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  1564. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  1565. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  1566. end;
  1567. end;
  1568. (* ProcessID cached to avoid repeated calls to GetCurrentProcess. *)
  1569. var
  1570. ProcessID: SizeUInt;
  1571. function GetProcessID: SizeUInt;
  1572. begin
  1573. GetProcessID := ProcessID;
  1574. end;
  1575. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  1576. begin
  1577. result := stklen;
  1578. end;
  1579. procedure SysCleanup;
  1580. var
  1581. i: integer;
  1582. begin
  1583. if FCmdLine = nil then
  1584. exit;
  1585. SysFreeMem(FCmdLine);
  1586. for i:=0 to Fargc do
  1587. sysfreemem(Fargv[i]);
  1588. sysfreemem(Fargv);
  1589. end;
  1590. initialization
  1591. SysResetFPU;
  1592. if not(IsLibrary) then
  1593. SysInitFPU;
  1594. StackLength := CheckInitialStkLen(InitialStkLen);
  1595. StackBottom := StackTop - StackLength;
  1596. { some misc stuff }
  1597. hprevinst:=0;
  1598. if not IsLibrary then
  1599. SysInstance:=GetModuleHandle(nil);
  1600. MainInstance:=SysInstance;
  1601. {$IFNDEF HAS_MEMORYMANAGER}
  1602. { Setup Heap }
  1603. InitHeap;
  1604. {$ENDIF HAS_MEMORYMANAGER}
  1605. SysInitExceptions;
  1606. initunicodestringmanager;
  1607. InitWinCEWidestrings;
  1608. if not IsLibrary then
  1609. begin
  1610. SysInitStdIO;
  1611. end;
  1612. { Reset IO Error }
  1613. InOutRes:=0;
  1614. ProcessID := GetCurrentProcessID;
  1615. { threading }
  1616. InitSystemThreads;
  1617. { Reset internal error variable }
  1618. errno:=0;
  1619. initvariantmanager;
  1620. DispCallByIDProc:=@DoDispCallByIDError;
  1621. finalization
  1622. SysCleanup;
  1623. end.