1

I got following errors when tried to calling .dll (made from delphi) function from unity3d C# code.

Here is pictures,

enter image description here

and errors says,

enter image description here

and .dll codes are,

enter image description here

So why error occurs and how to solve?

Thanks a lot!

TLama
  • 75,147
  • 17
  • 214
  • 392
creator
  • 671
  • 11
  • 28

1 Answers1

6

Where you write

error = GetRequestResult(code);

you need to write

error = GetRequestResult(out code);

which is precisely what the second error message states.


Looking at your code, returning a PChar from the Delphi DLL the way that you do is not compatible with your P/invokes. The P/invoke marshaller is assuming that your return values were allocated with CoTaskMemAlloc and will call CoTaskMemFree on the pointer that you return. That's going to lead to some problems somewhere down the line. I think you'll need to tackle that issue at some point but since it's not the subject of this question, I won't attempt to solve the problem here.

Hinek
  • 9,519
  • 12
  • 52
  • 74
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • and I don't understand well about your PChar....comments. So you mean I should not use just 'string' for 'PChar'? Isn't there a easy way to solve this communication problem, like change PChar to another type compatible with c#'s string? – creator Jul 31 '12 at 15:24
  • The easiest thing to do is to use WideString on the Delphi side and [MarshalAs(UnmanagedType.BSTR)] on the C# side. But that doesn't work for return values since Delphi has rather unusual semantics for return values (see http://stackoverflow.com/questions/9349530/why-can-a-widestring-not-be-used-as-a-function-return-value-for-interop). So I'd convert the string return values into out or ref parameters. – David Heffernan Jul 31 '12 at 15:29
  • In fact I already covered this issue in some detail in your previous question! http://stackoverflow.com/questions/11175534/how-to-call-this-delphi-dll-function-from-c – David Heffernan Jul 31 '12 at 17:13
  • Hi, Do you have MS messanger ID or Skype ID? – creator Aug 01 '12 at 01:17