1 {$DEFINE ASM_VERSION}
\r
2 //{$DEFINE VARIANT_USED}
\r
10 {=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
\r
12 KKKKK KKKKK OOOOOOOOO LLLLL
\r
13 KKKKK KKKKK OOOOOOOOOOOOO LLLLL
\r
14 KKKKK KKKKK OOOOO OOOOO LLLLL
\r
15 KKKKK KKKKK OOOOO OOOOO LLLLL
\r
16 KKKKKKKKKK OOOOO OOOOO LLLLL
\r
17 KKKKK KKKKK OOOOO OOOOO LLLLL
\r
18 KKKKK KKKKK OOOOO OOOOO LLLLL
\r
19 KKKKK KKKKK OOOOOOOOOOOOO LLLLLLLLLLLLL
\r
20 KKKKK KKKKK OOOOOOOOO LLLLLLLLLLLLL
\r
22 Key Objects Library (C) 2000 by Kladov Vladimir.
\r
24 mailto: bonanzas@xcl.cjb.net
\r
25 Home: http://kol.nm.ru
\r
29 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-}
\r
31 This code is grabbed mainly from standard SysUtils.pas unit,
\r
32 provided by Borland Delphi. This unit is for handling exceptions,
\r
33 and to use it just place a reference to exceptions unit in
\r
34 uses clause of any of your unit or dpr-file.
\r
37 { Copyright (C) 1995,99 Inprise Corporation }
\r
38 { Copyright (C) 2001, Kladov Vladimir }
\r
41 {* Unit to provide error handling for KOL programs using efficient
\r
42 exceptions mechanism. To use it, just place a reference to it into
\r
43 uses clause of any unit of the project (or dpr-file).
\r
45 It is possible to use standard SysUtils instead, but it increases
\r
46 size of executable at least by 10K. Using this unit to handle exceptions
\r
47 increases executable only by 6,5K.
\r
55 {$IFDEF _D6orHigher}
\r
56 //{$WARN SYMBOL_DEPRECATED OFF}
\r
58 {$IFDEF _D7orHigher}
\r
59 {$WARN UNSAFE_TYPE OFF}
\r
60 {$WARN UNSAFE_CODE OFF}
\r
63 {+} // These resource strings are grabbed from SysConst and changed a bit to make it smaller.
\r
72 SUnknown = '<unknown>';
\r
73 //SInvalidInteger = '''%s'' is not a valid integer value';
\r
74 //SInvalidFloat = '''%s'' is not a valid floating point value';
\r
75 //SInvalidDate = '''%s'' is not a valid date';
\r
76 //SInvalidTime = '''%s'' is not a valid time';
\r
77 //SInvalidDateTime = '''%s'' is not a valid date and time';
\r
78 //STimeEncodeError = 'Invalid argument to time encode';
\r
79 //SDateEncodeError = 'Invalid argument to date encode';
\r
80 SOutOfMemory = 'Out of memory';
\r
81 SInOutError = 'I/O error %d';
\r
82 SFileNotFound = 'File not found';
\r
83 SInvalidFilename = 'Invalid filename';
\r
84 STooManyOpenFiles = 'Too many open files';
\r
85 SAccessDenied = 'File access denied';
\r
86 SEndOfFile = //'Read beyond end of file';
\r
88 SDiskFull = 'Disk full';
\r
89 //SInvalidInput = 'Invalid numeric input'; // {-} Seems for console input only
\r
90 SDivByZero = 'Division by zero';
\r
91 SRangeError = 'Range check error';
\r
92 SIntOverflow = 'Integer overflow';
\r
93 SInvalidOp = 'Invalid floating point operation';
\r
94 SZeroDivide = 'Floating point division by zero';
\r
95 SOverflow = 'Floating point overflow';
\r
96 SUnderflow = 'Floating point underflow';
\r
97 SInvalidPointer = 'Invalid pointer operation';
\r
98 SInvalidCast = 'Invalid class typecast';
\r
99 SAccessViolation = 'Access violation at address %p. %s of address %p';
\r
100 SStackOverflow = 'Stack overflow';
\r
101 SControlC = //'Control-C hit';
\r
102 '^C'; // {-} for console applications only
\r
103 SPrivilege = 'Privileged instruction';
\r
104 SOperationAborted = 'Operation aborted';
\r
105 SException = 'Exception %s in module %s at %p.'#10'%s%s';
\r
106 //SExceptTitle = 'Application Error';
\r
107 //SInvalidFormat = 'Format ''%s'' invalid or incompatible with argument';
\r
108 //SArgumentMissing = 'No argument for format ''%s''';
\r
109 SInvalidVarCast = 'Invalid variant type conversion';
\r
110 SInvalidVarOp = 'Invalid variant operation';
\r
111 SDispatchError = 'Variant method calls not supported';
\r
112 SVarArrayCreate = 'Error creating variant array';
\r
113 SVarNotArray = 'Variant is not an array';
\r
114 SVarArrayBounds = 'Variant array index out of bounds';
\r
116 SReadAccess = 'Read';
\r
117 SWriteAccess = 'Write';
\r
118 //SResultTooLong = 'Format result longer than 4096 characters';
\r
119 //SFormatTooLong = 'Format string too long';
\r
120 SExternalException = 'External exception %x';
\r
121 SAssertionFailed = 'Assertion failed';
\r
122 SIntfCastError = 'Interface not supported';
\r
123 SSafecallException = 'Exception in safecall method';
\r
124 SAssertError = '%s (%s, line %d)';
\r
125 SAbstractError = 'Abstract Error';
\r
126 SModuleAccessViolation = 'Access violation at address %p in module ''%s''. %s of address %p';
\r
127 {SCannotReadPackageInfo = 'Cannot access package information for package ''%s''';
\r
128 sErrorLoadingPackage = 'Can''t load package %s.'#13#10'%s';
\r
129 SInvalidPackageFile = 'Invalid package file ''%s''';
\r
130 SInvalidPackageHandle = 'Invalid package handle';
\r
131 SDuplicatePackageUnit = 'Cannot load package ''%s.'' It contains unit ''%s,''' +
\r
132 ';which is also contained in package ''%s''';}
\r
133 SWin32Error = 'Win32 Error. Code: %d.'#10'%s';
\r
134 SUnkWin32Error = 'A Win32 API function failed';
\r
135 SNL = 'Application is not licensed to use this feature';
\r
140 { Generic procedure pointer }
\r
142 TProcedure = procedure;
\r
144 { Generic filename type }
\r
146 TFileName = type string;
\r
150 TDestroyException = procedure( Sender: Exception ) of object;
\r
152 TError = ( e_Abort, e_Heap, e_OutOfMem, e_InOut, e_External, e_Int,
\r
153 e_DivBy0, e_Range, e_IntOverflow, e_Math, e_Math_InvalidArgument,
\r
154 e_InvalidOp, e_ZeroDivide, e_Overflow, e_Underflow, e_InvalidPointer,
\r
155 e_InvalidCast, e_Convert, e_AccessViolation, e_Privilege,
\r
156 e_StackOverflow, e_CtrlC, e_Variant, e_PropReadOnly,
\r
157 e_PropWriteOnly, e_Assertion, e_Abstract, e_IntfCast,
\r
158 e_InvalidContainer, e_InvalidInsert, e_Package, e_Win32,
\r
159 e_SafeCall, e_License, e_Custom, e_Com, e_Ole, e_Registry );
\r
160 {* Main error codes. These are to determine which exception occure. You
\r
161 can use e_Custom code for your own exceptions. }
\r
163 Exception = class(TObject)
\r
164 {* Exception class. In KOL, there is a single exception class is used.
\r
165 Instead of inheriting new exception classes from this ancestor, an
\r
166 instance of the same Exception class should be used. The difference
\r
167 is only in Code property, which contains a kind of exception. }
\r
172 FExceptionRecord: PExceptionRecord;
\r
174 FOnDestroy: TDestroyException;
\r
175 procedure SetData(const Value: Pointer);
\r
177 constructor Create(ACode: TError; const Msg: string);
\r
178 {* Use this constructor to raise exception, which does dot require of
\r
179 argument formatting. }
\r
180 constructor CreateFmt(ACode: TError; const Msg: string; const Args: array of const);
\r
181 {* Use this constructor to raise an exception with formatted Message string.
\r
182 Take into attention, that Format procedure defined in KOL, uses API wvsprintf
\r
183 function, which can understand a restricted set of format specifications. }
\r
184 constructor CreateCustom(AError: DWORD; const Msg: String);
\r
185 {* Use this constructor to create e_Custom exception and to assign AError to
\r
186 its ErrorCode property. }
\r
187 constructor CreateCustomFmt(AError: DWORD; const Msg: String; const Args: array of const);
\r
188 {* Use this constructor to create e_Custom exception with formatted message
\r
189 string and to assign AError to its ErrorCode property. }
\r
190 constructor CreateResFmt(ACode: TError; Ident: Integer; const Args: array of const);
\r
192 destructor Destroy; override;
\r
194 property Message: string read FMessage; // write FMessage;
\r
195 {* Text string, containing descriptive message about the exception. }
\r
196 property Code: TError read FCode;
\r
197 {* Main exception code. This property can be used to determine, which exception
\r
199 property ErrorCode: DWORD read FErrorCode write FErrorCode;
\r
200 {* This code is to detailize error. For Code = e_InOut, ErrorCode contains
\r
201 more detail description of input/output error. For e_Custom, You can
\r
202 assign it to any value You want. }
\r
203 property ExceptionRecord: PExceptionRecord read FExceptionRecord;
\r
204 {* This property is only for e_External exception. }
\r
205 property Data: Pointer read FData write SetData;
\r
206 {* Custom defined pointer. Use it in your custom exceptions. }
\r
207 property OnDestroy: TDestroyException read FOnDestroy write FOnDestroy;
\r
208 {* This event is to allow to do something when custom Exception is
\r
212 With err unit, it is possible to use all capabilities of Delphi exception
\r
213 handling almost in the same way as usual. The difference only in that the
\r
214 single exception class should be used. To determine which exception occure,
\r
215 use property Code. So, code to handle exception can be written like follow:
\r
218 ! except on E: Exception do
\r
220 ! e_DivBy0: HandleDivideByZero;
\r
221 ! e_Overflow: HandleOverflow;
\r
225 To raise an error, create an instance of Exception class object, but
\r
226 pass a Code to its constructor:
\r
227 ! var E: Exception;
\r
229 ! E := Exception.Create( e_Custom, 'My custom exception' );
\r
230 ! E.ErrorCode := MY_MAGIC_CODE_FOR_CUSTOM_EXCEPTION;
\r
234 ExceptClass = class of Exception;
\r
236 { Exit procedure handling }
\r
238 { AddExitProc adds the given procedure to the run-time library's exit
\r
239 procedure list. When an application terminates, its exit procedures are
\r
240 executed in reverse order of definition, i.e. the last procedure passed
\r
241 to AddExitProc is the first one to get executed upon termination. }
\r
243 procedure AddExitProc(Proc: TProcedure);
\r
245 { System error messages }
\r
247 function SysErrorMessage(ErrorCode: Integer): string;
\r
249 { Exception handling routines }
\r
251 function ExceptObject: TObject;
\r
252 function ExceptAddr: Pointer;
\r
254 function ExceptionErrorMessage(ExceptObject: TObject; ExceptAddr: Pointer;
\r
255 Buffer: PChar; Size: Integer): Integer;
\r
257 procedure ShowException(ExceptObject: TObject; ExceptAddr: Pointer);
\r
261 //procedure OutOfMemoryError;
\r
263 { RaiseLastWin32Error calls the GetLastError API to retrieve the code for }
\r
264 { the last occuring Win32 error. If GetLastError returns an error code, }
\r
265 { RaiseLastWin32Error then raises an exception with the error code and }
\r
266 { message associated with with error. }
\r
268 procedure RaiseLastWin32Error;
\r
270 { Win32Check is used to check the return value of a Win32 API function }
\r
271 { which returns a BOOL to indicate success. If the Win32 API function }
\r
272 { returns False (indicating failure), Win32Check calls RaiseLastWin32Error }
\r
273 { to raise an exception. If the Win32 API function returns True, }
\r
274 { Win32Check returns True. }
\r
276 function Win32Check(RetVal: BOOL): BOOL;
\r
278 { Termination procedure support }
\r
281 TTerminateProc = function: Boolean;
\r
283 { Call AddTerminateProc to add a terminate procedure to the system list of }
\r
284 { termination procedures. Delphi will call all of the function in the }
\r
285 { termination procedure list before an application terminates. The user- }
\r
286 { defined TermProc function should return True if the application can }
\r
287 { safely terminate or False if the application cannot safely terminate. }
\r
288 { If one of the functions in the termination procedure list returns False, }
\r
289 { the application will not terminate. }
\r
291 procedure AddTerminateProc(TermProc: TTerminateProc);
\r
293 { CallTerminateProcs is called by VCL when an application is about to }
\r
294 { terminate. It returns True only if all of the functions in the }
\r
295 { system's terminate procedure list return True. This function is }
\r
296 { intended only to be called by Delphi, and it should not be called }
\r
299 function CallTerminateProcs: Boolean;
\r
302 function GDAL: LongWord;
\r
308 { SafeLoadLibrary calls LoadLibrary, disabling normal Win32 error message
\r
309 popup dialogs if the requested file can't be loaded. SafeLoadLibrary also
\r
310 preserves the current FPU control word (precision, exception masks) across
\r
311 the LoadLibrary call (in case the DLL you're loading hammers the FPU control
\r
312 word in its initialization, as many MS DLLs do)}
\r
315 function SafeLoadLibrary(const Filename: string;
\r
316 ErrorMode: UINT = SEM_NOOPENFILEERRORBOX): HMODULE;
\r
321 {procedure ConvertError(const Ident: string);
\r
323 raise Exception.Create(e_Convert, Ident);
\r
326 procedure ConvertErrorFmt(ResString: PResStringRec; const Args: array of const);
\r
328 raise Exception.CreateFmt(e_Convert, LoadResString(ResString), Args);
\r
331 { Memory management routines }
\r
333 function AllocMem(Size: Cardinal): Pointer;
\r
335 GetMem(Result, Size);
\r
336 FillChar(Result^, Size, 0);
\r
339 { Exit procedure handling }
\r
342 PExitProcInfo = ^TExitProcInfo;
\r
343 TExitProcInfo = record
\r
344 Next: PExitProcInfo;
\r
350 ExitProcList: PExitProcInfo = nil;
\r
352 procedure DoExitProc;
\r
358 ExitProcList := P^.Next;
\r
359 ExitProc := P^.SaveExit;
\r
365 procedure AddExitProc(Proc: TProcedure);
\r
370 P^.Next := ExitProcList;
\r
371 P^.SaveExit := ExitProc;
\r
374 ExitProc := @DoExitProc;
\r
377 { System error messages }
\r
379 function SysErrorMessage(ErrorCode: Integer): string;
\r
382 Buffer: array[0..255] of Char;
\r
384 Len := FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM or
\r
385 FORMAT_MESSAGE_ARGUMENT_ARRAY, nil, ErrorCode, 0, Buffer,
\r
386 SizeOf(Buffer), nil);
\r
387 while (Len > 0) and (Buffer[Len - 1] in [#0..#32, '.']) do Dec(Len);
\r
388 SetString(Result, Buffer, Len);
\r
391 { Exception handling routines }
\r
394 OutOfMemory: EOutOfMemory;
\r
395 InvalidPointer: EInvalidPointer;}
\r
398 PRaiseFrame = ^TRaiseFrame;
\r
399 TRaiseFrame = record
\r
400 NextRaise: PRaiseFrame;
\r
401 ExceptAddr: Pointer;
\r
402 ExceptObject: TObject;
\r
403 ExceptionRecord: PExceptionRecord;
\r
406 { Return current exception object }
\r
408 function ExceptObject: TObject;
\r
410 if RaiseList <> nil then
\r
411 Result := PRaiseFrame(RaiseList)^.ExceptObject else
\r
415 { Return current exception address }
\r
417 function ExceptAddr: Pointer;
\r
419 if RaiseList <> nil then
\r
420 Result := PRaiseFrame(RaiseList)^.ExceptAddr else
\r
424 { Convert physical address to logical address }
\r
426 function ConvertAddr(Address: Pointer): Pointer; assembler;
\r
428 TEST EAX,EAX { Always convert nil to nil }
\r
430 SUB EAX, $1000 { offset from code start; code start set by linker to $1000 }
\r
434 { Format and return an exception error message }
\r
436 {$IFDEF _D2} // this code is luck in D2 system.pas
\r
438 PLibModule = ^TLibModule;
\r
439 TLibModule = record
\r
442 ResInstance: Longint;
\r
446 function FindResourceHInstance(Instance: Longint): Longint;
\r
448 Result := Instance;
\r
453 PStrData = ^TStrData;
\r
461 function EnumStringModules(Instance: Longint; Data: Pointer): Boolean;
\r
463 with PStrData(Data)^ do
\r
465 nChars := LoadString(Instance, Ident, Buffer, BufSize);
\r
466 Result := nChars = 0;
\r
471 function FindStringResource(Ident: Integer; Buffer: PChar; BufSize: Integer): Integer;
\r
475 StrData.Ident := Ident;
\r
476 StrData.Buffer := Buffer;
\r
477 StrData.BufSize := BufSize;
\r
478 StrData.nChars := 0;
\r
479 EnumResourceModules(EnumStringModules, @StrData);
\r
480 Result := StrData.nChars;
\r
485 function LoadStr(Ident: Integer): string;
\r
487 Buffer: array[0..1023] of Char;
\r
489 SetString(Result, Buffer, LoadString(HInstance, Ident, Buffer,
\r
493 function LoadStr(Ident: Integer): string;
\r
495 Buffer: array[0..1023] of Char;
\r
497 SetString(Result, Buffer, FindStringResource(Ident, Buffer, SizeOf(Buffer)));
\r
501 function FmtLoadStr(Ident: Integer; const Args: array of const): string;
\r
503 //FmtStr(Result, LoadStr(Ident), Args);
\r
504 Result := Format(LoadStr(Ident), Args);
\r
507 function ExceptionErrorMessage(ExceptObject: TObject; ExceptAddr: Pointer;
\r
508 Buffer: PChar; Size: Integer): Integer;
\r
513 ModuleName: array[0..MAX_PATH] of Char;
\r
514 //Temp: array[0..MAX_PATH] of Char;
\r
515 Fmt: array[0..255] of Char;
\r
516 Info: TMemoryBasicInformation;
\r
517 ConvertedAddress: Pointer;
\r
519 VirtualQuery(ExceptAddr, Info, sizeof(Info));
\r
520 if (Info.State <> MEM_COMMIT) or
\r
521 (GetModuleFilename( THandle(Info.AllocationBase), {Temp} ModuleName,
\r
522 SizeOf({Temp} ModuleName)) = 0) then
\r
524 GetModuleFileName(HInstance, {Temp} ModuleName, SizeOf({Temp} ModuleName));
\r
525 ConvertedAddress := ConvertAddr(ExceptAddr);
\r
528 Integer(ConvertedAddress) := Integer(ExceptAddr) - Integer(Info.AllocationBase);
\r
529 //StrLCopy(ModuleName, AnsiStrRScan(Temp, '\') + 1, SizeOf(ModuleName) - 1);
\r
530 {-} // Why to extract unit name from a path? Isn't it well to show complete path
\r
531 // and to economy code for the extraction.
\r
534 if ExceptObject is Exception then
\r
536 MsgPtr := PChar(Exception(ExceptObject).Message);
\r
537 //MsgLen := StrLen(MsgPtr);
\r
538 //if (MsgLen <> 0) and (MsgPtr[MsgLen - 1] <> '.') then MsgEnd := '.';
\r
539 {-} // Isn't it too beautiful - devote ~40 bytes of code just to decide,
\r
540 // add or not a point at the end of the message.
\r
543 StrCopy( Fmt, SException );
\r
545 LoadString(FindResourceHInstance(HInstance),
\r
546 PResStringRec(@SException).Identifier, Fmt, SizeOf(Fmt));
\r
548 //MsgOK( ModuleName );
\r
549 StrCopy( Buffer, PChar( Format( Fmt, [ ExceptObject.ClassName,
\r
550 ModuleName, ConvertedAddress, MsgPtr, '' {MsgEnd}]) ) );
\r
551 Result := StrLen(Buffer);
\r
554 { Display exception message box }
\r
556 procedure ShowException(ExceptObject: TObject; ExceptAddr: Pointer);
\r
558 //Title: array[0..63] of Char;
\r
559 Buffer: array[0..1023] of Char;
\r
561 ExceptionErrorMessage(ExceptObject, ExceptAddr, Buffer, SizeOf(Buffer));
\r
566 {LoadString(FindResourceHInstance(HInstance), PResStringRec(@SExceptTitle).Identifier,
\r
567 Title, SizeOf(Title));}
\r
568 MessageBox(0, Buffer, {Title} nil, MB_OK or MB_ICONSTOP or MB_TASKMODAL);
\r
572 { Raise abort exception }
\r
576 function ReturnAddr: Pointer;
\r
578 // MOV EAX,[ESP + 4] !!! codegen dependant
\r
583 raise Exception.Create(e_Abort, SOperationAborted) at ReturnAddr;
\r
586 { Raise out of memory exception }
\r
588 {procedure OutOfMemoryError;
\r
593 { Exception class }
\r
595 constructor Exception.CreateResFmt(ACode: TError; Ident: Integer;
\r
596 const Args: array of const);
\r
598 FMessage := Format(LoadStr(Ident), Args);
\r
601 destructor Exception.Destroy;
\r
603 if Assigned( FOnDestroy ) then
\r
604 FOnDestroy( Self );
\r
608 procedure Exception.SetData(const Value: Pointer);
\r
613 constructor Exception.Create(ACode: TError; const Msg: string);
\r
617 //FAllowFree := TRUE;
\r
620 constructor Exception.CreateCustom(AError: DWORD; const Msg: String);
\r
624 FErrorCode := AError;
\r
627 constructor Exception.CreateCustomFmt(AError: DWORD; const Msg: String;
\r
628 const Args: array of const);
\r
631 FErrorCode := AError;
\r
632 FMessage := Format(Msg, Args);
\r
635 constructor Exception.CreateFmt(ACode: TError; const Msg: string;
\r
636 const Args: array of const);
\r
639 FMessage := Format(Msg, Args);
\r
642 { EHeapException class }
\r
644 {procedure EHeapException.FreeInstance;
\r
647 inherited FreeInstance;
\r
650 { Create I/O exception }
\r
652 function CreateInOutError: Exception;
\r
659 ErrorMap: array[0..5] of TErrorRec = (
\r
660 (Code: 2; Ident: SFileNotFound),
\r
661 (Code: 3; Ident: SInvalidFilename),
\r
662 (Code: 4; Ident: STooManyOpenFiles),
\r
663 (Code: 5; Ident: SAccessDenied),
\r
664 (Code: 100; Ident: SEndOfFile),
\r
665 (Code: 101; Ident: SDiskFull){,
\r
666 (Code: 106; Ident: SInvalidInput)} );
\r
671 I := Low(ErrorMap);
\r
672 InOutRes := IOResult; // resets IOResult to zero
\r
673 while (I <= High(ErrorMap)) and (ErrorMap[I].Code <> InOutRes) do Inc(I);
\r
674 if I <= High(ErrorMap) then
\r
675 Result := Exception.Create(e_InOut, ErrorMap[I].Ident)
\r
677 Result := Exception.CreateFmt(e_InOut, SInOutError, [InOutRes]);
\r
678 //Result := Exception.Create(e_InOut, SInOutError + Int2Str( InOutRes ) );
\r
679 Result.ErrorCode := InOutRes;
\r
682 { RTL error handler }
\r
685 TExceptMapRec = packed record
\r
691 ExceptMap: array[1..24] of TExceptMapRec = (
\r
692 (ECode: e_OutOfMem; EIdent: SOutOfMemory),
\r
693 (ECode: e_InvalidPointer; EIdent: SInvalidPointer),
\r
694 (ECode: e_DivBy0; EIdent: SDivByZero),
\r
695 (ECode: e_Range; EIdent: SRangeError),
\r
696 (ECode: e_IntOverflow; EIdent: SIntOverflow),
\r
697 (ECode: e_InvalidOp; EIdent: SInvalidOp),
\r
698 (ECode: e_ZeroDivide; EIdent: SDivByZero),
\r
699 (ECode: e_Overflow; EIdent: SOverflow),
\r
700 (ECode: e_Underflow; EIdent: SUnderflow),
\r
701 (ECode: e_InvalidCast; EIdent: SInvalidCast),
\r
702 (ECode: e_AccessViolation;EIdent: SAccessViolation),
\r
703 (ECode: e_Privilege; EIdent: SPrivilege),
\r
704 (ECode: e_CtrlC; EIdent: SControlC),
\r
705 // {-} Only for console applications
\r
706 (ECode: e_StackOverflow; EIdent: SStackOverflow),
\r
707 {$IFDEF VARIANT_USED}
\r
708 (ECode: e_Variant; EIdent: SInvalidVarCast),
\r
709 (ECode: e_Variant; EIdent: SInvalidVarOp),
\r
710 (ECode: e_Variant; EIdent: SDispatchError),
\r
711 (ECode: e_Variant; EIdent: SVarArrayCreate),
\r
712 (ECode: e_Variant; EIdent: SVarNotArray),
\r
713 (ECode: e_Variant; EIdent: SVarArrayBounds),
\r
715 (ECode: e_Variant; EIdent: SVar),
\r
716 (ECode: e_Variant; EIdent: SVar),
\r
717 (ECode: e_Variant; EIdent: SVar),
\r
718 (ECode: e_Variant; EIdent: SVar),
\r
719 (ECode: e_Variant; EIdent: SVar),
\r
720 (ECode: e_Variant; EIdent: SVar),
\r
722 (ECode: e_Assertion; EIdent: SAssertionFailed),
\r
723 (ECode: e_External; EIdent: SExternalException),
\r
724 (ECode: e_IntfCast; EIdent: SIntfCastError),
\r
725 (ECode: e_SafeCall; EIdent: SSafecallException));
\r
727 procedure ErrorHandler(ErrorCode: Integer; ErrorAddr: Pointer);
\r
732 1: E := OutOfMemory;
\r
733 2: E := InvalidPointer;
\r
734 3..24: with ExceptMap[ErrorCode] do E := EClass.Create(EIdent);
\r
736 E := CreateInOutError;
\r
740 if ErrorCode <= 24 then
\r
741 with ExceptMap[ErrorCode] do E := Exception.Create(ECode, EIdent)
\r
742 else E := CreateInOutError;
\r
745 raise E at ErrorAddr;
\r
748 { Assertion error handler }
\r
750 { This is complicated by the desire to make it look like the exception }
\r
751 { happened in the user routine, so the debugger can give a decent stack }
\r
752 { trace. To make that feasible, AssertErrorHandler calls a helper function }
\r
753 { to create the exception object, so that AssertErrorHandler itself does }
\r
754 { not need any temps. After the exception object is created, the asm }
\r
755 { routine RaiseAssertException sets up the registers just as if the user }
\r
756 { code itself had raised the exception. }
\r
758 function CreateAssertException(const Message, Filename: string;
\r
759 LineNumber: Integer): Exception;
\r
763 if Message <> '' then S := Message else S := SAssertionFailed;
\r
764 Result := Exception.CreateFmt(e_Assertion, SAssertError,
\r
765 [S, Filename, LineNumber]);
\r
768 { This code is based on the following assumptions: }
\r
769 { - Our direct caller (AssertErrorHandler) has an EBP frame }
\r
770 { - ErrorStack points to where the return address would be if the }
\r
771 { user program had called System.@RaiseExcept directly }
\r
772 procedure RaiseAssertException(const E: Exception; const ErrorAddr, ErrorStack: Pointer);
\r
777 JMP System.@RaiseExcept
\r
780 { If you change this procedure, make sure it does not have any local variables }
\r
781 { or temps that need cleanup - they won't get cleaned up due to the way }
\r
782 { RaiseAssertException frame works. Also, it can not have an exception frame. }
\r
783 procedure AssertErrorHandler(const Message, Filename: string;
\r
784 LineNumber: Integer; ErrorAddr: Pointer);
\r
788 E := CreateAssertException(Message, Filename, LineNumber);
\r
789 RaiseAssertException(E, ErrorAddr, PChar(@ErrorAddr)+4);
\r
792 { Abstract method invoke error handler }
\r
794 procedure AbstractErrorHandler;
\r
796 raise Exception.Create(e_Abstract, SAbstractError);
\r
799 {$IFDEF ASM_VERSION}
\r
800 function MapException(P: PExceptionRecord): Byte;
\r
802 MOV EAX, [EAX].TExceptionRecord.ExceptionCode
\r
808 MOV EDX, offset @@cvTable - 1
\r
824 DB 6, $8F, 6, $90, 6, $92
\r
837 function MapException(P: PExceptionRecord): Byte;
\r
839 case P.ExceptionCode of
\r
840 STATUS_INTEGER_DIVIDE_BY_ZERO:
\r
842 STATUS_ARRAY_BOUNDS_EXCEEDED:
\r
844 STATUS_INTEGER_OVERFLOW:
\r
846 STATUS_FLOAT_INEXACT_RESULT,
\r
847 STATUS_FLOAT_INVALID_OPERATION,
\r
848 STATUS_FLOAT_STACK_CHECK:
\r
850 STATUS_FLOAT_DIVIDE_BY_ZERO:
\r
852 STATUS_FLOAT_OVERFLOW:
\r
854 STATUS_FLOAT_UNDERFLOW,
\r
855 STATUS_FLOAT_DENORMAL_OPERAND:
\r
857 STATUS_ACCESS_VIOLATION:
\r
859 STATUS_PRIVILEGED_INSTRUCTION:
\r
861 STATUS_CONTROL_C_EXIT:
\r
863 STATUS_STACK_OVERFLOW:
\r
866 Result := 22; { must match System.reExternalException }
\r
871 function GetExceptionClass(P: PExceptionRecord): ExceptClass;
\r
872 //var ErrorCode: Byte;
\r
874 //ErrorCode := MapException(P);
\r
875 Result := Exception; {ExceptMap[ErrorCode].EClass;}
\r
878 function GetExceptionObject(P: PExceptionRecord): Exception;
\r
880 ErrorCode: Integer;
\r
882 function CreateAVObject: Exception;
\r
884 AccessOp: string; // string ID indicating the access type READ or WRITE
\r
885 AccessAddress: Pointer;
\r
886 MemInfo: TMemoryBasicInformation;
\r
887 ModName: array[0..MAX_PATH] of Char;
\r
891 if ExceptionInformation[0] = 0 then
\r
892 AccessOp := SReadAccess else
\r
893 AccessOp := SWriteAccess;
\r
894 AccessAddress := Pointer(ExceptionInformation[1]);
\r
895 VirtualQuery(ExceptionAddress, MemInfo, SizeOf(MemInfo));
\r
896 if (MemInfo.State = MEM_COMMIT) and (GetModuleFileName(THandle(MemInfo.AllocationBase),
\r
897 ModName, SizeOf(ModName)) <> 0) then
\r
898 Result := Exception.CreateFmt(e_AccessViolation, sModuleAccessViolation,
\r
899 [ExceptionAddress, ExtractFileName(ModName), AccessOp,
\r
901 else Result := Exception.CreateFmt(e_AccessViolation, sAccessViolation,
\r
902 [ExceptionAddress, AccessOp, AccessAddress]);
\r
907 ErrorCode := MapException(P);
\r
910 with ExceptMap[ErrorCode] do Result := Exception.Create(ECode, EIdent);
\r
911 11: Result := CreateAVObject;
\r
914 Result := Exception.CreateFmt(e_External, SExternalException, [P.ExceptionCode]);
\r
915 Result.FExceptionRecord := P;
\r
920 { RTL exception handler }
\r
922 procedure ExceptHandler(ExceptObject: TObject; ExceptAddr: Pointer); far;
\r
924 ShowException(ExceptObject, ExceptAddr);
\r
929 function InitAssertErrorProc: Boolean;
\r
931 AssertErrorProc := @AssertErrorHandler;
\r
936 procedure InitExceptions;
\r
938 {OutOfMemory := EOutOfMemory.Create(SOutOfMemory);
\r
939 InvalidPointer := EInvalidPointer.Create(SInvalidPointer);}
\r
940 ErrorProc := @ErrorHandler;
\r
941 ExceptProc := @ExceptHandler;
\r
942 ExceptionClass := Exception;
\r
944 ExceptClsProc := @GetExceptionClass;
\r
946 ExceptObjProc := @GetExceptionObject;
\r
948 {AssertErrorProc := @AssertErrorHandler;}
\r
949 {+} // Initialize Assert only when "Assertions" option is turned on in Compiler:
\r
950 Assert( InitAssertErrorProc, '' );
\r
953 //AbstractErrorProc := @AbstractErrorHandler;
\r
954 // {-} KOL does not use classes, so EAbstractError should never be raised.
\r
958 procedure DoneExceptions;
\r
960 {OutOfMemory.AllowFree := True;
\r
961 OutOfMemory.FreeInstance;
\r
962 OutOfMemory := nil;
\r
963 InvalidPointer.AllowFree := True;
\r
964 InvalidPointer.Free;
\r
965 InvalidPointer := nil;}
\r
968 ExceptionClass := nil;
\r
969 //ExceptClsProc := nil; --see InitExceptions
\r
970 ExceptObjProc := nil;
\r
971 AssertErrorProc := nil;
\r
974 { RaiseLastWin32Error }
\r
976 procedure RaiseLastWin32Error;
\r
981 LastError := GetLastError;
\r
982 if LastError <> ERROR_SUCCESS then
\r
983 Error := Exception.CreateFmt(e_Win32, SWin32Error, [LastError,
\r
984 SysErrorMessage(LastError)])
\r
986 Error := Exception.Create(e_Win32, SUnkWin32Error );
\r
987 Error.ErrorCode := LastError;
\r
993 function Win32Check(RetVal: BOOL): BOOL;
\r
995 if not RetVal then RaiseLastWin32Error;
\r
1000 PTerminateProcInfo = ^TTerminateProcInfo;
\r
1001 TTerminateProcInfo = record
\r
1002 Next: PTerminateProcInfo;
\r
1003 Proc: TTerminateProc;
\r
1007 TerminateProcList: PTerminateProcInfo = nil;
\r
1009 procedure AddTerminateProc(TermProc: TTerminateProc);
\r
1011 P: PTerminateProcInfo;
\r
1014 P^.Next := TerminateProcList;
\r
1015 P^.Proc := TermProc;
\r
1016 TerminateProcList := P;
\r
1019 function CallTerminateProcs: Boolean;
\r
1021 PI: PTerminateProcInfo;
\r
1024 PI := TerminateProcList;
\r
1025 while Result and (PI <> nil) do
\r
1027 Result := PI^.Proc;
\r
1032 procedure FreeTerminateProcs;
\r
1034 PI: PTerminateProcInfo;
\r
1036 while TerminateProcList <> nil do
\r
1038 PI := TerminateProcList;
\r
1039 TerminateProcList := PI^.Next;
\r
1046 function AL1(const P): LongWord;
\r
1048 MOV EDX,DWORD PTR [P]
\r
1049 XOR EDX,DWORD PTR [P+4]
\r
1050 XOR EDX,DWORD PTR [P+8]
\r
1051 XOR EDX,DWORD PTR [P+12]
\r
1055 function AL2(const P): LongWord;
\r
1057 MOV EDX,DWORD PTR [P]
\r
1059 XOR EDX,DWORD PTR [P+4]
\r
1061 XOR EDX,DWORD PTR [P+8]
\r
1063 XOR EDX,DWORD PTR [P+12]
\r
1068 AL1s: array[0..2] of LongWord = ($FFFFFFF0, $FFFFEBF0, 0);
\r
1069 AL2s: array[0..2] of LongWord = ($42C3ECEF, $20F7AEB6, $D1C2F74E);
\r
1073 raise Exception.Create(e_License, SNL);
\r
1077 function ALR: Pointer;
\r
1079 LibModule: PLibModule;
\r
1081 if MainInstance <> 0 then
\r
1082 Result := Pointer(LoadResource(MainInstance, FindResource(MainInstance, 'DVCLAL',
\r
1087 LibModule := LibModuleList;
\r
1088 while LibModule <> nil do
\r
1090 with LibModule^ do
\r
1092 Result := Pointer(LoadResource(Instance, FindResource(Instance, 'DVCLAL',
\r
1094 if Result <> nil then Break;
\r
1096 LibModule := LibModule.Next;
\r
1099 if Result = nil then ALV;
\r
1102 function GDAL: LongWord;
\r
1104 TDVCLAL = array[0..3] of LongWord;
\r
1105 PDVCLAL = ^TDVCLAL;
\r
1109 PAL1s, PAL2s: PDVCLAL;
\r
1118 ALOK := ((A1 = PAL1s[0]) and (A2 = PAL2s[0])) or
\r
1119 ((A1 = PAL1s[1]) and (A2 = PAL2s[1])) or
\r
1120 ((A1 = PAL1s[2]) and (A2 = PAL2s[2]));
\r
1121 FreeResource(Integer(P));
\r
1122 if not ALOK then ALV;
\r
1131 ALOK := (AL1(P^) = AL1s[2]) and (AL2(P^) = AL2s[2]);
\r
1132 FreeResource(Integer(P));
\r
1133 if not ALOK then ALV;
\r
1141 if (AL <> AL1s[1]) and (AL <> AL1s[2]) then ALV;
\r
1146 function SafeLoadLibrary(const Filename: string; ErrorMode: UINT): HMODULE;
\r
1149 FPUControlWord: Word;
\r
1151 OldMode := SetErrorMode(ErrorMode);
\r
1154 FNSTCW FPUControlWord
\r
1157 Result := LoadLibrary(PChar(Filename));
\r
1161 FLDCW FPUControlWord
\r
1165 SetErrorMode(OldMode);
\r
1170 {procedure Exception.FreeInstance;
\r
1172 if FAllowFree then
\r
1182 FreeTerminateProcs;
\r