2
0

syswin32.pp 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1993-98 by Florian Klaempfl and Pavel Ozerski
  5. member of the Free Pascal development team.
  6. FPC Pascal system unit for the Win32 API.
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. {$S-}
  14. unit syswin32;
  15. {$I os.inc}
  16. interface
  17. { include system-independent routine headers }
  18. {$I systemh.inc}
  19. { include heap support headers }
  20. {$I heaph.inc}
  21. {$ifdef i386}
  22. {$define Set_i386_Exception_handler}
  23. {$endif i386}
  24. const
  25. { Default filehandles }
  26. UnusedHandle : longint = -1;
  27. StdInputHandle : longint = 0;
  28. StdOutputHandle : longint = 0;
  29. StdErrorHandle : longint = 0;
  30. type
  31. TStartupInfo=packed record
  32. cb : longint;
  33. lpReserved : Pointer;
  34. lpDesktop : Pointer;
  35. lpTitle : Pointer;
  36. dwX : longint;
  37. dwY : longint;
  38. dwXSize : longint;
  39. dwYSize : longint;
  40. dwXCountChars : longint;
  41. dwYCountChars : longint;
  42. dwFillAttribute : longint;
  43. dwFlags : longint;
  44. wShowWindow : Word;
  45. cbReserved2 : Word;
  46. lpReserved2 : Pointer;
  47. hStdInput : longint;
  48. hStdOutput : longint;
  49. hStdError : longint;
  50. end;
  51. var
  52. { C compatible arguments }
  53. argc : longint;
  54. argv : ppchar;
  55. { Win32 Info }
  56. startupinfo : tstartupinfo;
  57. hprevinst,
  58. HInstance,
  59. MainInstance,
  60. cmdshow : longint;
  61. IsLibrary,IsMultiThreaded,IsConsole : boolean;
  62. DLLreason,DLLparam:longint;
  63. implementation
  64. { include system independent routines }
  65. {$I system.inc}
  66. { some declarations for Win32 API calls }
  67. {$I win32.inc}
  68. CONST
  69. { These constants are used for conversion of error codes }
  70. { from win32 i/o errors to tp i/o errors }
  71. { errors 1 to 18 are the same as in Turbo Pascal }
  72. { DO NOT MODIFY UNLESS YOU KNOW EXACTLY WHAT YOU ARE DOING! }
  73. { The media is write protected. }
  74. ERROR_WRITE_PROTECT = 19;
  75. { The system cannot find the device specified. }
  76. ERROR_BAD_UNIT = 20;
  77. { The device is not ready. }
  78. ERROR_NOT_READY = 21;
  79. { The device does not recognize the command. }
  80. ERROR_BAD_COMMAND = 22;
  81. { Data error (cyclic redundancy check) }
  82. ERROR_CRC = 23;
  83. { The program issued a command but the }
  84. { command length is incorrect. }
  85. ERROR_BAD_LENGTH = 24;
  86. { The drive cannot locate a specific }
  87. { area or track on the disk. }
  88. ERROR_SEEK = 25;
  89. { The specified disk or diskette cannot be accessed. }
  90. ERROR_NOT_DOS_DISK = 26;
  91. { The drive cannot find the sector requested. }
  92. ERROR_SECTOR_NOT_FOUND = 27;
  93. { The printer is out of paper. }
  94. ERROR_OUT_OF_PAPER = 28;
  95. { The system cannot write to the specified device. }
  96. ERROR_WRITE_FAULT = 29;
  97. { The system cannot read from the specified device. }
  98. ERROR_READ_FAULT = 30;
  99. { A device attached to the system is not functioning.}
  100. ERROR_GEN_FAILURE = 31;
  101. { The process cannot access the file because }
  102. { it is being used by another process. }
  103. ERROR_SHARING_VIOLATION = 32;
  104. var
  105. errno : longint;
  106. { misc. functions }
  107. function GetLastError : DWORD;
  108. external 'kernel32' name 'GetLastError';
  109. function MessageBox(w1:longint;l1,l2:pointer;w2:longint):longint;
  110. external 'user32' name 'MessageBoxA';
  111. { time and date functions }
  112. function GetTickCount : longint;
  113. external 'kernel32' name 'GetTickCount';
  114. { process functions }
  115. procedure ExitProcess(uExitCode : UINT);
  116. external 'kernel32' name 'ExitProcess';
  117. Procedure Errno2InOutRes;
  118. Begin
  119. { DO NOT MODIFY UNLESS YOU KNOW EXACTLY WHAT YOU ARE DOING }
  120. if (errno >= ERROR_WRITE_PROTECT) and (errno <= ERROR_GEN_FAILURE) THEN
  121. BEGIN
  122. { This is the offset to the Win32 to add to directly map }
  123. { to the DOS/TP compatible error codes when in this range }
  124. InOutRes := word(errno)+131;
  125. END
  126. else
  127. { This case is special }
  128. if errno=ERROR_SHARING_VIOLATION THEN
  129. BEGIN
  130. InOutRes :=5;
  131. END
  132. else
  133. { other error codes can directly be mapped }
  134. InOutRes := Word(errno);
  135. errno:=0;
  136. end;
  137. {$ifdef dummy}
  138. procedure int_stackcheck(stack_size:longint);[public,alias: 'STACKCHECK'];
  139. {
  140. called when trying to get local stack if the compiler directive $S
  141. is set this function must preserve esi !!!! because esi is set by
  142. the calling proc for methods it must preserve all registers !!
  143. With a 2048 byte safe area used to write to StdIo without crossing
  144. the stack boundary
  145. }
  146. begin
  147. asm
  148. pushl %eax
  149. pushl %ebx
  150. movl stack_size,%ebx
  151. addl $2048,%ebx
  152. movl %esp,%eax
  153. subl %ebx,%eax
  154. movl stacklimit,%ebx
  155. cmpl %eax,%ebx
  156. jae __short_on_stack
  157. popl %ebx
  158. popl %eax
  159. leave
  160. ret $4
  161. __short_on_stack:
  162. { can be usefull for error recovery !! }
  163. popl %ebx
  164. popl %eax
  165. end['EAX','EBX'];
  166. HandleError(202);
  167. end;
  168. {$endif dummy}
  169. procedure halt(errnum : byte);
  170. begin
  171. do_exit;
  172. ExitProcess(errnum);
  173. end;
  174. function paramcount : longint;
  175. begin
  176. paramcount := argc - 1;
  177. end;
  178. function paramstr(l : longint) : string;
  179. begin
  180. if (l>=0) and (l+1<=argc) then
  181. paramstr:=strpas(argv[l])
  182. else
  183. paramstr:='';
  184. end;
  185. procedure randomize;
  186. begin
  187. randseed:=GetTickCount;
  188. end;
  189. {*****************************************************************************
  190. Heap Management
  191. *****************************************************************************}
  192. { memory functions }
  193. function GlobalAlloc(mode,size:longint):longint;
  194. external 'kernel32' name 'GlobalAlloc';
  195. function GlobalLock(handle:longint):pointer;
  196. external 'kernel32' name 'GlobalLock';
  197. {$ifdef SYSTEMDEBUG}
  198. function GlobalSize(h:longint):longint;
  199. external 'kernel32' name 'GlobalSize';
  200. {$endif}
  201. {$ASMMODE DIRECT}
  202. function getheapstart:pointer;assembler;
  203. asm
  204. leal HEAP,%eax
  205. end ['EAX'];
  206. function getheapsize:longint;assembler;
  207. asm
  208. movl HEAPSIZE,%eax
  209. end ['EAX'];
  210. {$ASMMODE ATT}
  211. function Sbrk(size : longint):longint;
  212. var
  213. h,l : longint;
  214. begin
  215. h:=GlobalAlloc(258,size);
  216. l:=longint(GlobalLock(h));
  217. if l=0 then
  218. l:=-1;
  219. {$ifdef SYSTEMDEBUG}
  220. Writeln('new heap part at $',hexstr(l,8), ' size = ',GlobalSize(h));
  221. {$endif}
  222. sbrk:=l;
  223. end;
  224. { include standard heap management }
  225. {$I heap.inc}
  226. {*****************************************************************************
  227. Low Level File Routines
  228. *****************************************************************************}
  229. function WriteFile(fh:longint;buf:pointer;len:longint;var loaded:longint;
  230. overlap:pointer):longint;
  231. external 'kernel32' name 'WriteFile';
  232. function ReadFile(fh:longint;buf:pointer;len:longint;var loaded:longint;
  233. overlap:pointer):longint;
  234. external 'kernel32' name 'ReadFile';
  235. function CloseHandle(h : longint) : longint;
  236. external 'kernel32' name 'CloseHandle';
  237. function DeleteFile(p : pchar) : longint;
  238. external 'kernel32' name 'DeleteFileA';
  239. function MoveFile(old,_new : pchar) : longint;
  240. external 'kernel32' name 'MoveFileA';
  241. function SetFilePointer(l1,l2 : longint;l3 : pointer;l4 : longint) : longint;
  242. external 'kernel32' name 'SetFilePointer';
  243. function GetFileSize(h:longint;p:pointer) : longint;
  244. external 'kernel32' name 'GetFileSize';
  245. function CreateFile(name : pointer;access,sharing : longint;
  246. security : pointer;how,attr,template : longint) : longint;
  247. external 'kernel32' name 'CreateFileA';
  248. function SetEndOfFile(h : longint) : longbool;
  249. external 'kernel32' name 'SetEndOfFile';
  250. function GetFileType(Handle:DWORD):DWord;
  251. external 'kernel32' name 'GetFileType';
  252. procedure AllowSlash(p:pchar);
  253. var
  254. i : longint;
  255. begin
  256. { allow slash as backslash }
  257. for i:=0 to strlen(p) do
  258. if p[i]='/' then p[i]:='\';
  259. end;
  260. procedure do_close(h : longint);
  261. begin
  262. closehandle(h);
  263. end;
  264. procedure do_erase(p : pchar);
  265. begin
  266. AllowSlash(p);
  267. if DeleteFile(p)=0 then
  268. Begin
  269. errno:=GetLastError;
  270. Errno2InoutRes;
  271. end;
  272. end;
  273. procedure do_rename(p1,p2 : pchar);
  274. begin
  275. AllowSlash(p1);
  276. AllowSlash(p2);
  277. if MoveFile(p1,p2)=0 then
  278. Begin
  279. errno:=GetLastError;
  280. Errno2InoutRes;
  281. end;
  282. end;
  283. function do_write(h,addr,len : longint) : longint;
  284. var
  285. size:longint;
  286. begin
  287. if writefile(h,pointer(addr),len,size,nil)=0 then
  288. Begin
  289. errno:=GetLastError;
  290. Errno2InoutRes;
  291. end;
  292. do_write:=size;
  293. end;
  294. function do_read(h,addr,len : longint) : longint;
  295. var
  296. result:longint;
  297. begin
  298. if readfile(h,pointer(addr),len,result,nil)=0 then
  299. Begin
  300. errno:=GetLastError;
  301. Errno2InoutRes;
  302. end;
  303. do_read:=result;
  304. end;
  305. function do_filepos(handle : longint) : longint;
  306. var
  307. l:longint;
  308. begin
  309. l:=SetFilePointer(handle,0,nil,FILE_CURRENT);
  310. if l=-1 then
  311. begin
  312. l:=0;
  313. errno:=GetLastError;
  314. Errno2InoutRes;
  315. end;
  316. do_filepos:=l;
  317. end;
  318. procedure do_seek(handle,pos : longint);
  319. begin
  320. if SetFilePointer(handle,pos,nil,FILE_BEGIN)=-1 then
  321. Begin
  322. errno:=GetLastError;
  323. Errno2InoutRes;
  324. end;
  325. end;
  326. function do_seekend(handle:longint):longint;
  327. begin
  328. do_seekend:=SetFilePointer(handle,0,nil,FILE_END);
  329. if do_seekend=-1 then
  330. begin
  331. errno:=GetLastError;
  332. Errno2InoutRes;
  333. end;
  334. end;
  335. function do_filesize(handle : longint) : longint;
  336. var
  337. aktfilepos : longint;
  338. begin
  339. aktfilepos:=do_filepos(handle);
  340. do_filesize:=do_seekend(handle);
  341. do_seek(handle,aktfilepos);
  342. end;
  343. procedure do_truncate (handle,pos:longint);
  344. begin
  345. do_seek(handle,pos);
  346. if not(SetEndOfFile(handle)) then
  347. begin
  348. errno:=GetLastError;
  349. Errno2InoutRes;
  350. end;
  351. end;
  352. procedure do_open(var f;p : pchar;flags:longint);
  353. {
  354. filerec and textrec have both handle and mode as the first items so
  355. they could use the same routine for opening/creating.
  356. when (flags and $10) the file will be append
  357. when (flags and $100) the file will be truncate/rewritten
  358. when (flags and $1000) there is no check for close (needed for textfiles)
  359. }
  360. var
  361. oflags,cd : longint;
  362. begin
  363. AllowSlash(p);
  364. { close first if opened }
  365. if ((flags and $1000)=0) then
  366. begin
  367. case filerec(f).mode of
  368. fminput,fmoutput,fminout : Do_Close(filerec(f).handle);
  369. fmclosed : ;
  370. else
  371. begin
  372. {not assigned}
  373. inoutres:=102;
  374. exit;
  375. end;
  376. end;
  377. end;
  378. { reset file handle }
  379. filerec(f).handle:=UnusedHandle;
  380. { convert filemode to filerec modes }
  381. case (flags and 3) of
  382. 0 : begin
  383. filerec(f).mode:=fminput;
  384. oflags:=GENERIC_READ;
  385. end;
  386. 1 : begin
  387. filerec(f).mode:=fmoutput;
  388. oflags:=GENERIC_WRITE;
  389. end;
  390. 2 : begin
  391. filerec(f).mode:=fminout;
  392. oflags:=GENERIC_WRITE or GENERIC_READ;
  393. end;
  394. end;
  395. { standard is opening and existing file }
  396. cd:=OPEN_EXISTING;
  397. { create it ? }
  398. if (flags and $100)<>0 then
  399. cd:=CREATE_ALWAYS
  400. { or append ? }
  401. else
  402. if (flags and $10)<>0 then
  403. cd:=OPEN_ALWAYS;
  404. { empty name is special }
  405. if p[0]=#0 then
  406. begin
  407. case filerec(f).mode of
  408. fminput : filerec(f).handle:=StdInputHandle;
  409. fmappend,
  410. fmoutput : begin
  411. filerec(f).handle:=StdOutputHandle;
  412. filerec(f).mode:=fmoutput; {fool fmappend}
  413. end;
  414. end;
  415. exit;
  416. end;
  417. filerec(f).handle:=CreateFile(p,oflags,0,nil,cd,FILE_ATTRIBUTE_NORMAL,0);
  418. { append mode }
  419. if (flags and $10)<>0 then
  420. begin
  421. do_seekend(filerec(f).handle);
  422. filerec(f).mode:=fmoutput; {fool fmappend}
  423. end;
  424. { get errors }
  425. if (filerec(f).handle=0) or (filerec(f).handle=-1) then
  426. { handle -1 is returned sometimes !! (PM) }
  427. begin
  428. errno:=GetLastError;
  429. Errno2InoutRes;
  430. end;
  431. end;
  432. function do_isdevice(handle:longint):boolean;
  433. begin
  434. do_isdevice:=(getfiletype(handle)=2);
  435. end;
  436. {*****************************************************************************
  437. UnTyped File Handling
  438. *****************************************************************************}
  439. {$i file.inc}
  440. {*****************************************************************************
  441. Typed File Handling
  442. *****************************************************************************}
  443. {$i typefile.inc}
  444. {*****************************************************************************
  445. Text File Handling
  446. *****************************************************************************}
  447. {$DEFINE EOF_CTRLZ}
  448. {$i text.inc}
  449. {*****************************************************************************
  450. Directory Handling
  451. *****************************************************************************}
  452. function CreateDirectory(name : pointer;sec : pointer) : longint;
  453. external 'kernel32' name 'CreateDirectoryA';
  454. function RemoveDirectory(name:pointer):longint;
  455. external 'kernel32' name 'RemoveDirectoryA';
  456. function SetCurrentDirectory(name : pointer) : longint;
  457. external 'kernel32' name 'SetCurrentDirectoryA';
  458. function GetCurrentDirectory(bufsize : longint;name : pchar) : longint;
  459. external 'kernel32' name 'GetCurrentDirectoryA';
  460. type
  461. TDirFnType=function(name:pointer):word;
  462. procedure dirfn(afunc : TDirFnType;const s:string);
  463. var
  464. buffer : array[0..255] of char;
  465. begin
  466. move(s[1],buffer,length(s));
  467. buffer[length(s)]:=#0;
  468. AllowSlash(pchar(@buffer));
  469. if aFunc(@buffer)=0 then
  470. begin
  471. errno:=GetLastError;
  472. Errno2InoutRes;
  473. end;
  474. end;
  475. function CreateDirectoryTrunc(name:pointer):word;
  476. begin
  477. CreateDirectoryTrunc:=CreateDirectory(name,nil);
  478. end;
  479. procedure mkdir(const s:string);[IOCHECK];
  480. begin
  481. If InOutRes <> 0 then exit;
  482. dirfn(TDirFnType(@CreateDirectoryTrunc),s);
  483. end;
  484. procedure rmdir(const s:string);[IOCHECK];
  485. begin
  486. If InOutRes <> 0 then exit;
  487. dirfn(TDirFnType(@RemoveDirectory),s);
  488. end;
  489. procedure chdir(const s:string);[IOCHECK];
  490. begin
  491. If InOutRes <> 0 then exit;
  492. dirfn(TDirFnType(@SetCurrentDirectory),s);
  493. end;
  494. procedure getdir(drivenr:byte;var dir:shortstring);
  495. const
  496. Drive:array[0..3]of char=(#0,':',#0,#0);
  497. var
  498. defaultdrive:boolean;
  499. DirBuf,SaveBuf:array[0..259] of Char;
  500. begin
  501. defaultdrive:=drivenr=0;
  502. if not defaultdrive then
  503. begin
  504. byte(Drive[0]):=Drivenr+64;
  505. GetCurrentDirectory(SizeOf(SaveBuf),SaveBuf);
  506. SetCurrentDirectory(@Drive);
  507. end;
  508. GetCurrentDirectory(SizeOf(DirBuf),DirBuf);
  509. if not defaultdrive then
  510. SetCurrentDirectory(@SaveBuf);
  511. dir:=strpas(DirBuf);
  512. end;
  513. {*****************************************************************************
  514. SystemUnit Initialization
  515. *****************************************************************************}
  516. { Startup }
  517. procedure GetStartupInfo(p : pointer);
  518. external 'kernel32' name 'GetStartupInfoA';
  519. function GetStdHandle(nStdHandle:DWORD):THANDLE;
  520. external 'kernel32' name 'GetStdHandle';
  521. { command line/enviroment functions }
  522. function GetCommandLine : pchar;
  523. external 'kernel32' name 'GetCommandLineA';
  524. { module functions }
  525. function GetModuleFileName(l1:longint;p:pointer;l2:longint):longint;
  526. external 'kernel32' name 'GetModuleFileNameA';
  527. function GetModuleHandle(p : pointer) : longint;
  528. external 'kernel32' name 'GetModuleHandleA';
  529. var
  530. ModuleName : array[0..255] of char;
  531. function GetCommandFile:pchar;
  532. begin
  533. GetModuleFileName(0,@ModuleName,255);
  534. GetCommandFile:=@ModuleName;
  535. end;
  536. procedure setup_arguments;
  537. var
  538. arglen,
  539. count : longint;
  540. argstart,
  541. cmdline : pchar;
  542. quote : set of char;
  543. argsbuf : array[0..127] of pchar;
  544. begin
  545. { create commandline, it starts with the executed filename which is argv[0] }
  546. cmdline:=GetCommandLine;
  547. count:=0;
  548. repeat
  549. { skip leading spaces }
  550. while cmdline^ in [' ',#9,#13] do
  551. inc(longint(cmdline));
  552. case cmdline^ of
  553. #0 : break;
  554. '"' : begin
  555. quote:=['"'];
  556. inc(longint(cmdline));
  557. end;
  558. '''' : begin
  559. quote:=[''''];
  560. inc(longint(cmdline));
  561. end;
  562. else
  563. quote:=[' ',#9,#13];
  564. end;
  565. { scan until the end of the argument }
  566. argstart:=cmdline;
  567. while (cmdline^<>#0) and not(cmdline^ in quote) do
  568. inc(longint(cmdline));
  569. { reserve some memory }
  570. arglen:=cmdline-argstart;
  571. getmem(argsbuf[count],arglen+1);
  572. move(argstart^,argsbuf[count]^,arglen);
  573. argsbuf[count][arglen]:=#0;
  574. { skip quote }
  575. if cmdline^ in quote then
  576. inc(longint(cmdline));
  577. inc(count);
  578. until false;
  579. { create argc }
  580. argc:=count;
  581. { create an nil entry }
  582. argsbuf[count]:=nil;
  583. inc(count);
  584. { create the argv }
  585. getmem(argv,count shl 2);
  586. move(argsbuf,argv^,count shl 2);
  587. end;
  588. {$ifdef dummy}
  589. Function SetUpStack : longint;
  590. { This routine does the following : }
  591. { returns the value of the initial SP - __stklen }
  592. begin
  593. asm
  594. pushl %ebx
  595. pushl %eax
  596. movl __stklen,%ebx
  597. movl %esp,%eax
  598. subl %ebx,%eax
  599. movl %eax,__RESULT
  600. popl %eax
  601. popl %ebx
  602. end;
  603. end;
  604. {$endif}
  605. procedure install_exception_handlers;forward;
  606. var
  607. { value of the stack segment
  608. to check if the call stack can be written on exceptions }
  609. _SS : longint;
  610. procedure Exe_entry;[public, alias : '_FPC_EXE_Entry'];
  611. begin
  612. IsLibrary:=false;
  613. { install the handlers for exe only ?
  614. or should we install them for DLL also ? (PM) }
  615. install_exception_handlers;
  616. { This strange construction is needed to solve the _SS problem
  617. with a smartlinked syswin32 (PFV) }
  618. {$ASMMODE ATT}
  619. asm
  620. pushl %ebp
  621. xorl %ebp,%ebp
  622. movw %ss,%bp
  623. movl %ebp,_SS
  624. xorl %ebp,%ebp
  625. end;
  626. {$ASMMODE DIRECT}
  627. asm
  628. call PASCALMAIN
  629. popl %ebp
  630. end;
  631. { if we pass here there was no error ! }
  632. ExitProcess(0);
  633. end;
  634. procedure Dll_entry;[public, alias : '_FPC_DLL_Entry'];
  635. begin
  636. IsLibrary:=true;
  637. case DLLreason of
  638. 1,2 :
  639. begin
  640. {$ASMMODE ATT}
  641. asm
  642. xorl %edi,%edi
  643. movw %ss,%di
  644. movl %edi,_SS
  645. end;
  646. {$ASMMODE DIRECT}
  647. asm
  648. call PASCALMAIN
  649. end;
  650. end
  651. else
  652. begin
  653. asm
  654. call FPC_DO_EXIT
  655. end;
  656. end;
  657. end;
  658. end;
  659. {$ASMMODE ATT}
  660. {$ifdef Set_i386_Exception_handler}
  661. const
  662. EXCEPTION_MAXIMUM_PARAMETERS = 15;
  663. EXCEPTION_ACCESS_VIOLATION = $c0000005;
  664. EXCEPTION_BREAKPOINT = $80000003;
  665. EXCEPTION_DATATYPE_MISALIGNMENT = $80000002;
  666. EXCEPTION_SINGLE_STEP = $80000004;
  667. EXCEPTION_ARRAY_BOUNDS_EXCEEDED = $c000008c;
  668. EXCEPTION_FLT_DENORMAL_OPERAND = $c000008d;
  669. EXCEPTION_FLT_DIVIDE_BY_ZERO = $c000008e;
  670. EXCEPTION_FLT_INEXACT_RESULT = $c000008f;
  671. EXCEPTION_FLT_INVALID_OPERATION = $c0000090;
  672. EXCEPTION_FLT_OVERFLOW = $c0000091;
  673. EXCEPTION_FLT_STACK_CHECK = $c0000092;
  674. EXCEPTION_FLT_UNDERFLOW = $c0000093;
  675. EXCEPTION_INT_DIVIDE_BY_ZERO = $c0000094;
  676. EXCEPTION_INT_OVERFLOW = $c0000095;
  677. EXCEPTION_INVALID_HANDLE = $c0000008;
  678. EXCEPTION_PRIV_INSTRUCTION = $c0000096;
  679. EXCEPTION_NONCONTINUABLE_EXCEPTION = $c0000025;
  680. EXCEPTION_NONCONTINUABLE = $1;
  681. EXCEPTION_STACK_OVERFLOW = $c00000fd;
  682. EXCEPTION_INVALID_DISPOSITION = $c0000026;
  683. ExceptionContinueExecution = 0;
  684. ExceptionContinueSearch = 1;
  685. type
  686. FLOATING_SAVE_AREA = record
  687. ControlWord : DWORD;
  688. StatusWord : DWORD;
  689. TagWord : DWORD;
  690. ErrorOffset : DWORD;
  691. ErrorSelector : DWORD;
  692. DataOffset : DWORD;
  693. DataSelector : DWORD;
  694. RegisterArea : array[0..79] of BYTE;
  695. Cr0NpxState : DWORD;
  696. end;
  697. _FLOATING_SAVE_AREA = FLOATING_SAVE_AREA;
  698. TFLOATINGSAVEAREA = FLOATING_SAVE_AREA;
  699. PFLOATINGSAVEAREA = ^FLOATING_SAVE_AREA;
  700. CONTEXT = record
  701. ContextFlags : DWORD;
  702. Dr0 : DWORD;
  703. Dr1 : DWORD;
  704. Dr2 : DWORD;
  705. Dr3 : DWORD;
  706. Dr6 : DWORD;
  707. Dr7 : DWORD;
  708. FloatSave : FLOATING_SAVE_AREA;
  709. SegGs : DWORD;
  710. SegFs : DWORD;
  711. SegEs : DWORD;
  712. SegDs : DWORD;
  713. Edi : DWORD;
  714. Esi : DWORD;
  715. Ebx : DWORD;
  716. Edx : DWORD;
  717. Ecx : DWORD;
  718. Eax : DWORD;
  719. Ebp : DWORD;
  720. Eip : DWORD;
  721. SegCs : DWORD;
  722. EFlags : DWORD;
  723. Esp : DWORD;
  724. SegSs : DWORD;
  725. end;
  726. LPCONTEXT = ^CONTEXT;
  727. _CONTEXT = CONTEXT;
  728. TCONTEXT = CONTEXT;
  729. PCONTEXT = ^CONTEXT;
  730. type pexception_record = ^exception_record;
  731. EXCEPTION_RECORD = record
  732. ExceptionCode : longint;
  733. ExceptionFlags : longint;
  734. ExceptionRecord : pexception_record;
  735. ExceptionAddress : pointer;
  736. NumberParameters : longint;
  737. ExceptionInformation : array[0..EXCEPTION_MAXIMUM_PARAMETERS-1] of pointer;
  738. end;
  739. PEXCEPTION_POINTERS = ^EXCEPTION_POINTERS;
  740. EXCEPTION_POINTERS = record
  741. ExceptionRecord : PEXCEPTION_RECORD ;
  742. ContextRecord : PCONTEXT ;
  743. end;
  744. { type of functions that should be used for exception handling }
  745. LPTOP_LEVEL_EXCEPTION_FILTER = function(excep :PEXCEPTION_POINTERS) : longint;
  746. function SetUnhandledExceptionFilter(lpTopLevelExceptionFilter : LPTOP_LEVEL_EXCEPTION_FILTER)
  747. : LPTOP_LEVEL_EXCEPTION_FILTER;
  748. external 'kernel32' name 'SetUnhandledExceptionFilter';
  749. function syswin32_i386_exception_handler(excep :PEXCEPTION_POINTERS) : longint;
  750. var frame : longint;
  751. begin
  752. { default : unhandled !}
  753. if excep^.ContextRecord^.SegSs=_SS then
  754. frame:=excep^.ContextRecord^.Ebp
  755. else
  756. frame:=0;
  757. syswin32_i386_exception_handler:=ExceptionContinueSearch;
  758. case excep^.ExceptionRecord^.ExceptionCode of
  759. EXCEPTION_ACCESS_VIOLATION :
  760. HandleErrorFrame(216,frame);
  761. { EXCEPTION_BREAKPOINT = $80000003;
  762. EXCEPTION_DATATYPE_MISALIGNMENT = $80000002;
  763. EXCEPTION_SINGLE_STEP = $80000004; }
  764. EXCEPTION_ARRAY_BOUNDS_EXCEEDED :
  765. HandleErrorFrame(201,frame);
  766. { EXCEPTION_FLT_DENORMAL_OPERAND = $c000008d; }
  767. EXCEPTION_FLT_DIVIDE_BY_ZERO :
  768. HandleErrorFrame(200,frame);
  769. {EXCEPTION_FLT_INEXACT_RESULT = $c000008f;
  770. EXCEPTION_FLT_INVALID_OPERATION = $c0000090;}
  771. EXCEPTION_FLT_OVERFLOW :
  772. HandleErrorFrame(205,frame);
  773. EXCEPTION_FLT_STACK_CHECK :
  774. HandleErrorFrame(207,frame);
  775. { EXCEPTION_FLT_UNDERFLOW :
  776. HandleErrorFrame(206,frame); should be accepted as zero !! }
  777. EXCEPTION_INT_DIVIDE_BY_ZERO :
  778. HandleErrorFrame(200,frame);
  779. EXCEPTION_INT_OVERFLOW :
  780. HandleErrorFrame(215,frame);
  781. {EXCEPTION_INVALID_HANDLE = $c0000008;
  782. EXCEPTION_PRIV_INSTRUCTION = $c0000096;
  783. EXCEPTION_NONCONTINUABLE_EXCEPTION = $c0000025;
  784. EXCEPTION_NONCONTINUABLE = $1;}
  785. EXCEPTION_STACK_OVERFLOW :
  786. HandleErrorFrame(202,frame);
  787. {EXCEPTION_INVALID_DISPOSITION = $c0000026;}
  788. end;
  789. end;
  790. var
  791. old_exception : LPTOP_LEVEL_EXCEPTION_FILTER;
  792. procedure install_exception_handlers;
  793. begin
  794. old_exception:=SetUnhandledExceptionFilter(@syswin32_i386_exception_handler);
  795. end;
  796. {$else not i386 (Processor specific !!)}
  797. procedure install_exception_handlers;
  798. begin
  799. end;
  800. {$endif Set_i386_Exception_handler}
  801. const
  802. Exe_entry_code : pointer = @Exe_entry;
  803. Dll_entry_code : pointer = @Dll_entry;
  804. begin
  805. { get some helpful informations }
  806. GetStartupInfo(@startupinfo);
  807. { some misc Win32 stuff }
  808. hprevinst:=0;
  809. if not IsLibrary then
  810. HInstance:=getmodulehandle(GetCommandFile);
  811. MainInstance:=HInstance;
  812. { No idea how to know this issue !! }
  813. IsMultithreaded:=false;
  814. cmdshow:=startupinfo.wshowwindow;
  815. { to test stack depth }
  816. loweststack:=maxlongint;
  817. { real test stack depth }
  818. { stacklimit := setupstack; }
  819. { Setup heap }
  820. InitHeap;
  821. { Setup stdin, stdout and stderr }
  822. StdInputHandle:=longint(GetStdHandle(STD_INPUT_HANDLE));
  823. StdOutputHandle:=longint(GetStdHandle(STD_OUTPUT_HANDLE));
  824. StdErrorHandle:=longint(GetStdHandle(STD_ERROR_HANDLE));
  825. OpenStdIO(Input,fmInput,StdInputHandle);
  826. OpenStdIO(Output,fmOutput,StdOutputHandle);
  827. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  828. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  829. { Arguments }
  830. setup_arguments;
  831. { Reset IO Error }
  832. InOutRes:=0;
  833. { Reset internal error variable }
  834. errno := 0;
  835. end.
  836. {
  837. $Log$
  838. Revision 1.32 1998-12-28 23:30:11 peter
  839. * fixes for smartlinking
  840. Revision 1.31 1998/12/28 15:50:51 peter
  841. + stdout, which is needed when you write something in the system unit
  842. to the screen. Like the runtime error
  843. Revision 1.30 1998/12/21 14:28:23 pierre
  844. * HandleError -> HandleErrorFrame to avoid problem in
  845. assembler code in i386.inc
  846. (call to overloaded function in assembler block !)
  847. Revision 1.29 1998/12/15 22:43:14 peter
  848. * removed temp symbols
  849. Revision 1.28 1998/12/09 17:57:33 pierre
  850. + exception handling by default
  851. Revision 1.27 1998/12/01 14:00:08 pierre
  852. + added conversion from exceptions into run time error
  853. (only if syswin32 compiled with -ddebug for now !)
  854. * added HandleErrorFrame(errno,frame)
  855. where you specify the frame
  856. needed for win32 exception handling
  857. Revision 1.26 1998/11/30 13:13:41 pierre
  858. * needs asw to link correctly wprt0 or wdllprt0 file
  859. Revision 1.25 1998/11/30 09:16:58 pierre
  860. + added the changes from Pavel Ozerski after several modifications
  861. to be able to create DLLs
  862. Revision 1.24 1998/11/16 15:48:54 peter
  863. * fixed longbool returns for api calls
  864. Revision 1.23 1998/11/16 14:14:58 pierre
  865. * changed getdir(byte,string) to getdir(byte,shortstring)
  866. Revision 1.22 1998/10/27 15:07:16 florian
  867. + Is* flags added
  868. + IsLibrary works also
  869. Revision 1.21 1998/10/15 16:26:19 peter
  870. + fpuinit
  871. + end of backtrace indicator
  872. Revision 1.20 1998/09/14 10:48:33 peter
  873. * FPC_ names
  874. * Heap manager is now system independent
  875. Revision 1.19 1998/09/02 09:03:46 pierre
  876. * do_open sometimes returns -1 as handle on fail
  877. was not checked correctly
  878. Revision 1.16 1998/08/24 14:45:22 pierre
  879. * sbrk was wrong
  880. heap growing now works for win32
  881. Revision 1.15 1998/08/21 10:10:16 peter
  882. * winheap turned off by default
  883. Revision 1.14 1998/07/30 13:27:19 michael
  884. + Added support for errorproc. Changed runerror to HandleError
  885. Revision 1.13 1998/07/13 21:19:15 florian
  886. * some problems with ansi string support fixed
  887. Revision 1.12 1998/07/07 12:37:28 carl
  888. * correct mapping of error codes for TP compatibility
  889. + implemented stack checking in ifdef dummy
  890. Revision 1.11 1998/07/02 12:33:18 carl
  891. * IOCheck/InOutRes check for mkdir,rmdir and chdir like in TP
  892. Revision 1.10 1998/07/01 15:30:02 peter
  893. * better readln/writeln
  894. Revision 1.9 1998/06/10 10:39:17 peter
  895. * working w32 rtl
  896. Revision 1.8 1998/06/08 23:07:47 peter
  897. * dos interface is now 100% compatible
  898. * fixed call PASCALMAIN which must be direct asm
  899. Revision 1.7 1998/05/06 12:36:51 michael
  900. + Removed log from before restored version.
  901. Revision 1.6 1998/04/27 18:29:09 florian
  902. + do_open implemented, the file-I/O should be now complete
  903. Revision 1.5 1998/04/27 13:58:21 florian
  904. + paramstr/paramcount implemented
  905. Revision 1.4 1998/04/26 22:37:22 florian
  906. * some small extensions
  907. Revision 1.3 1998/04/26 21:49:57 florian
  908. + more stuff added (??dir procedures etc.)
  909. }