I am trying to use a third party library with a COM interface. A C++ sample application is provided, which uses a function declared as:
HRESULT __stdcall IMyInterface::DoSomething (BSTR id, long State)
After importing the Type Library, the resulting Delphi code is:
procedure DoSomething (const id: WideString; State: Integer); safecall;
In the C++ sample application, the result (HRESULT) is used to determine if the function was executed properly.
Why does Delphi convert this declaration into a procedure so that I cannot get a result back?
What can I do to fix this?
The safecall
calling convention is simply a transformation of an HResult
return type. If the function returns non-success (something other than S_OK
or S_False
, generally), it's wrapped into an exception and thrown (or turned into an reSafeCallError
run-time error, if SysUtils hasn't been used anywhere). Check out System._CheckAutoResult
for details.
Likewise, if you're implementing a safecall function, any exception is caught and translated into an HResult
value (E_Unexpected
, unless TObject.SafeCallException
is overridden to return something else). See System._HandleAutoException
for how that works.
You're welcome to turn the procedure back into a function returning HResult
if you want. The calling convention in that case should be stdcall
. Using your example:
function DoSomething(const id: WideString; State: Integer): HResult; stdcall;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With