Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning an Int32 from managed code to Delphi

Tags:

c#

delphi

I've used the information in this post Delphi - Accessing data from dynamic array that is populated from an untyped Pointer and others, plus Robert Giesecke's Unmanaged Exports to write some great code - thanks everyone. For example, I've written methods which return a complicated byte stream from C# back to Delphi. I'm having trouble, however, doing the simplest thing - returning an integer from a function call.

So, I define this prototype in Delphi:

TReturnIntFunc = function(AnInteger: Integer): Integer; safecall;

and this in C#:

[DllExport("NegateInt", CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.I4)]
static Int32 NegateInt([MarshalAs(UnmanagedType.I4)] Int32 AnInteger)
{
  return AnInteger * -1;
}

As with the other methods I've written, I dynamically load the C# DLL, find the function just fine, but when I execute it from Delphi, it throws an Exception with the message "Exception in safecall method".

Any clues anyone? I can and have marshal back an out or ref integer, just not as a return value.

like image 561
Stephen Walter Avatar asked Dec 29 '25 18:12

Stephen Walter


1 Answers

Safecall and StdCall is not the same thing - try using StdCall on the Delphi side as well.

like image 108
500 - Internal Server Error Avatar answered Jan 01 '26 08:01

500 - Internal Server Error



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!