tsafecall2.pp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. { %TARGET=win32,win64,wince,linux}
  2. program tsafecall2;
  3. {$mode objfpc}{$H+}
  4. uses
  5. Classes, SysUtils;
  6. // On Windows, safecall should behave like a stdcall, so it can be used for COM/
  7. // ActiveX. On Unix systems, it should be cdecl so that it could be used with
  8. // xpcom.
  9. function SafecallProcedureAlias(AParam1,AParam2: integer):HRESULT; {$IFDEF windows}stdcall{$ELSE}cdecl{$ENDIF}; [external name '_SAFECALLPROCEDURE'];
  10. procedure SafecallProcedure(AParam1,AParam2: integer); safecall; [public, alias: '_SAFECALLPROCEDURE'];
  11. begin
  12. if (AParam1<>$123456) or (AParam2<>$654321) then
  13. halt(1);
  14. end;
  15. function SafecallFunctionAlias(AParam1,AParam2: integer; out _result: string):HRESULT; {$IFDEF windows}stdcall{$ELSE}cdecl{$ENDIF}; [external name '_SAFECALLFUNCTION'];
  16. function SafecallFunction(AParam1,AParam2: integer): string; safecall; [public, alias: '_SAFECALLFUNCTION'];
  17. begin
  18. if (AParam1<>$123456) or (AParam2<>$654321) then
  19. halt(2)
  20. else
  21. result := 'hello';
  22. end;
  23. var s: string;
  24. begin
  25. try
  26. // The call is in a try..finally block. This is to test if the stack is
  27. // cleaned succesfully after the call. Without the try..finally it could
  28. // be that the stack is corrupted, but that this does not lead to any
  29. // detectable problems (exception)
  30. if SafecallProcedureAlias($123456,$654321) <> 0 then
  31. halt(11);
  32. if SafecallFunctionAlias($123456,$654321,s) <> 0 then
  33. halt(12);
  34. if s<>'hello' then halt(13);
  35. finally
  36. writeln('Ok');
  37. end;
  38. end.