0

Good day!

I have strange problem. On Delphi side we have:

Function Func(str: String; res: double) : double; export; stdcall;
Begin
    Result := res;
End;

And on C# side:

[DllImport("Project1.dll")]
static extern double Func(string str, double res);

It's OK, if i will write like this:

Console.WriteLine(Func("this is my function", 0.1));

Result will be 0.1.

But if I will replace 0.1 with 0 (zero, and 0d, and 0.0 too), I will get SEHException (0x80004005).

Any ideas?

UPD.

Delphi 2007 (no way to change, too many to rebuild ^_^)

VS 2013 (.NET 4.5.1)

OS: Windows 8.1

Platform target x86 (in x64 it does not work at all).

Spawn
  • 935
  • 1
  • 13
  • 32
  • Delphi version, Windows version (32- or 64-bits)... – kludg Nov 02 '14 at 08:25
  • user246408, i have updated post – Spawn Nov 02 '14 at 09:07
  • 2
    [Calling a Delphi DLL from a C# .NET application](http://stackoverflow.com/questions/4163364/calling-a-delphi-dll-from-a-c-sharp-net-application) and [Calling a Delphi method in a dll from c#](http://stackoverflow.com/questions/16601423/calling-a-delphi-method-in-a-dll-from-c-sharp) and [How to call this delphi .dll function from C#?](http://stackoverflow.com/questions/11175534/how-to-call-this-delphi-dll-function-from-c) – bummi Nov 02 '14 at 09:12
  • Whatever the reason why your code seems to work OK with `0.1` and fails with `0` (hard to explain without seeing the disassembled code) - the real problem with your code is passing a C# string as a Delphi string. – kludg Nov 02 '14 at 11:37

2 Answers2

0
Function Func(str: String; res: double): double; stdcall;

This function can only be called from Delphi because it uses the native Delphi string type. Indeed you can only call it from a Delphi version that has a binary compatible string type.

If you wish to interop with C# you will need to change your signature to use types that are valid for interop. For instance:

Function Func(str: PAnsiChar; res: double): double; stdcall;

On the C# side this is:

[DllImport(dllname, CharSet = CharSet.Ansi)]
static extern double Func(string str, double res);

As an alternative to using null-terminated character arrays, you could use the COM BSTR type if you preferred. I won't demonstrate that here. There are plenty of examples already available.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
-1

try

Console.WriteLine(Func("this is my function", Convert.ToDouble(0.0)));
  • With `Convert.ToDouble(0.0)` **AccessViolationException**: Attempted to read or write protected memory. But it's still all right with `Convert.ToDouble(0.1)` – Spawn Nov 02 '14 at 09:05
  • how about double dbl_value = 0; Console.WriteLine(Func("this is my function", dbl_value)); – Alexey Shumeyko Nov 02 '14 at 10:22
  • The C# compiler is perfectly capable of passing a double value of 0.0. – David Heffernan Nov 02 '14 at 12:58
  • As I understand, Delphi gets params from stack by reference, and 0 is Zero reference. So [IntPtr](http://msdn.microsoft.com/en-us/library/ms146633.aspx) should help. – Alexey Shumeyko Nov 03 '14 at 09:44
  • No. The problem is nothing to do with passing the double. You've presented multiple ways to do exactly what is done in the Q. The problem is the other parameter. – David Heffernan Nov 04 '14 at 17:17