Consume DLL made in Delphi 7 in C #

3

I need to create a DLL in Delphi 7 and consume it in C #, when the parameters and return are int there are no problems, the problem occurs when I try to use string in return, I put in Delphi a MessageDlg and I saw that the input parameter is going correctly, BreakPoint does not generate exception, simply the test console application dies when I make the call.

Delphi

function Mensagem(texto: string): PAnsiChar; stdcall;
var
  msg: str
begin
  msg := texto;
  Result := PAnsiChar(msg);
end;

exports
  Mensagem;

C #

[DllImport(@"teste.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
 static extern string Mensagem([MarshalAs(UnmanagedType.AnsiBStr)]string msg);
    
asked by anonymous 12.09.2018 / 21:19

1 answer

2

I found the solution in this post Here

I just needed to add the Return type and the method that I imported from the DLL was like this.

    [return: MarshalAsAttribute(UnmanagedType.AnsiBStr)]
    [DllImport(@"teste.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
    static extern string Mensagem([MarshalAs(UnmanagedType.AnsiBStr)]string msg);
    
13.09.2018 / 13:43