Mega Code Archive

 
Categories / Delphi / Examples
 

Custom Error Handler (asm approach)

Title: Custom Error Handler (asm approach) Question: Fast Error Handler Answer: //PUT Button1 on Form procedure TForm1.Button1Click(Sender: TObject); label cHandler,cResume; var ErrAddress:pointer; ErrCode:cardinal; begin //HANDLER PROLOGUE asm and dword ptr [ErrAddress],0 and dword ptr [ErrCode],0 push offset cHandler push dword ptr fs:[0] mov dword ptr fs:[0],esp end; asm cli //EXECUTE PRIVILEGED INSTRUCTION end; cResume: //RESUME HERE if ErrCode0 then begin messagebox(0,pansichar('Error Code:' + inttohex(ErrCode,1) +'h' + #13 + #10 +'At Address:' + inttohex(Cardinal(ErrAddress),1) + 'h'), pansichar('Error'),0); end; //HANDLER EPILOGUE asm pop dword ptr fs:[0] lea esp,[esp+4] end; exit; cHandler: asm mov eax,dword ptr [esp+12] mov edx,ebp mov ebp,dword ptr [eax+$b4] mov ecx,CONTEXT[eax].eip mov dword ptr [ErrAddress],ecx mov ecx,dword ptr [esp+4] push EXCEPTION_RECORD[ecx].ExceptionCode pop dword ptr [ErrCode] mov ebp,edx mov CONTEXT[eax].eip,offset cResume xor eax,eax ret 16 end; end;