3

i'm having problems calling a delphi function from C# (attempted to read or write protected memory), and was wondering what the correct way of calling the method should be. The Delphi function signature is as follows:

procedure methodToCall(
    aFirstParameter: Widestring; 
    var aSecondParameter: Widestring
    ); stdcall;

What is the correct way of calling this method from C#?

oɔɯǝɹ
  • 7,219
  • 7
  • 58
  • 69
  • Hi, how are you calling this from C#? with DllImport or with COM interoperability? Did you add the reference? Can we see the C# side as well? – Davide Piras Mar 15 '11 at 09:52

1 Answers1

8

The WideString is compatible with COM BSTR and so the .net marshaller should be able to consume it quite happily:

[DllImport(@"test.dll")]
private static extern void methodToCall(
    [MarshalAs(UnmanagedType.BStr)]
    string aFirstParameter,
    [MarshalAs(UnmanagedType.BStr)]
    ref string aSecondParameter
);
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490