tags:

views:

1358

answers:

4
+5  Q: 

What's safecall?

I'm working on the creation of an ActiveX EXE using VB6, and the only example I got is all written in Delphi.

Reading the example code, I noticed there are some functions whose signatures are followed by the safecall keyword. Here's an example:

function AddSymbol(ASymbol: OleVariant): WordBool; safecall;

So, here's the question: what is the purpose of this keyword?

Thanks.

A: 

http://en.wikipedia.org/wiki/X86_calling_conventions#safecall

Greg Hewgill
No offence, but : http://stackoverflow.com/questions/99996/whats-the-most-effective-way-of-answering-with-a-link
PatrickvL
+3  A: 

Safecall passes parameters from right to left, instead of the pascal or register (default) from left to right

With safecall, the procedure or function removes parameters from the stack upon returning (like pascal, but not like cdecl where it's up to the caller)

Safecall implements exception 'firewalls'; esp on Win32, this implements interprocess COM error notification. It would otherwise be identical to stdcall (the other calling convention used with the win api)

François
+2  A: 

What Francois said and if it wasn't for safecall your COM method call would have looked like below and you would have to do your own error checking instead of getting exceptions.

function AddSymbol(ASymbol: OleVariant; out Result: WordBool): HResult; stdcall;
Lars Truijens
+3  A: 

Additionally, the exception firewalls work by calling SetErrorInfo() with an object that supports IErrorInfo, so that the caller can get extended information about the exception. This is done by the TObject.SafeCallException override in both TComObject and TAutoIntfObject. Both of these types also implement ISupportErrorInfo to mark this fact.

In the event of an exception, the safecall method's caller can query for ISupportErrorInfo, then query that for the interface whose method resulted in a failure HRESULT (high bit set), and if that returns S_OK, GetErrorInfo() can get the exception info (description, help, etc., in the form of the IErrorInfo implementation that was passed to SetErrorInfo() by the Delphi RTL in the SafeCallException overrides).

Barry Kelly