I am trying to build a DLL in Delphi and consume that in C#. I have the below simple code Delphi code
library Project1;
uses
System.SysUtils,
System.Classes;
{$R *.res}
function DelphiFunction(A: Integer; B: Integer; out outputInt : integer): integer; stdcall; export;
begin
if A < B then
outputInt := B
else
outputInt := A;
DelphiFunction := outputInt;
end;
exports DelphiFunction;
begin
end.
C# Code
[DllImport("Project1.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern bool
DelphiFunction(int a, int b);
private void button3_Click(object sender, EventArgs e)
{
var a = 2;
var b = 3;
var result = DelphiFunction(a, b);
}
However, I am getting an error at line var result = DelphiFunction(a, b);
System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'