Stephen Walter
Stephen Walter

Reputation: 269

Returning an Int32 from managed code to 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.

Upvotes: 3

Views: 355

Answers (1)

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

Upvotes: 4

Related Questions