Why is the imported Type Library function different from the original source?

I am trying to use a third-party library with a COM interface. Attached is a C ++ example application that uses a function declared as:

HRESULT __stdcall IMyInterface::DoSomething (BSTR id, long State) 

After importing the type library, the resulting Delphi code:

 procedure DoSomething (const id: WideString; State: Integer); safecall; 

In the C ++ application example, the result (HRESULT) is used to determine the correct execution of the function.
Why does Delphi convert this declaration into a procedure so that I cannot get the result?
What can I do to fix this?

+6
source share
1 answer

The safecall calling safecall is simply a HResult return type HResult . If the function returns a failure (something other than S_OK or S_False , as a rule), it throws an exception and throws it (or turns into a reSafeCallError time error if SysUtils is not used anywhere). See System._CheckAutoResult .

Similarly, if you implement the safecall function, any exception is caught and converted to the value HResult ( E_Unexpected , unless TObject.SafeCallException is redefined to return something else). See System._HandleAutoException how this works.

You can return the procedure back to the function returning HResult if you want. The calling convention in this case should be stdcall . Using your example:

 function DoSomething(const id: WideString; State: Integer): HResult; stdcall; 
+7
source

Source: https://habr.com/ru/post/917299/


All Articles