Einzelnen Beitrag anzeigen

Elvis

Registriert seit: 25. Nov 2005
Ort: München
1.909 Beiträge
 
Delphi 2010 Professional
 
#6

Re: Parameterprobleme beim Aufruf einer Delphi DLL aus C#

  Alt 27. Okt 2009, 09:27
Interfaces zwischen .Net und Delphi auszutauschen ist gar nicht so schwer wie man denkt...
Delphi Library:
Delphi-Quellcode:
library CSharpInterop;

uses SysUtils;

type
  ISampleInterface = interface
  ['{4B82AD42-D2B9-4ED7-82D9-87E8EA471923}']
    function GetSomeValue : Integer; safecall;
  end;

  function TakesManagedInterface(const aInstance : IUnknown) : Integer; stdcall; export;
  var
    typedInstance : ISampleInterface;
  begin
    if Supports(aInstance, ISampleInterface, typedInstance) then
      result := typedInstance.GetSomeValue()
    else
      result := -1;
  end;

exports
  TakesManagedInterface;
end.
C# App:
Code:
[ComVisible(true)]
[Guid("4B82AD42-D2B9-4ED7-82D9-87E8EA471923"),
 InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface ISampleInterface
{
   int GetSomeValue();
}

class SampleClass : ISampleInterface
{
   public int Value { get; set; }

   public int GetSomeValue()
   {
      return Value;
   }
}

class Program
{
   [DllImport("CSharpInterop",
              EntryPoint = "TakesManagedInterface",
              CallingConvention = CallingConvention.StdCall)]
   static extern int CallDelphiFunction(IntPtr instance);

   static void Main(string[] args)
   {
      var instance = new SampleClass { Value = 123456 };
      var value = CallDelphiFunction(Marshal.GetIUnknownForObject(instance));
   }
}
Wichtig sind eigentlich nur die Attribute über dem Interface.
Die GUID darf keine Curlies enthalten, und das Interface muss als IUnknown deklariert sein. Sonst springst du in die falschen Method slots.
Außerdem muss es ComVisible sein, sonst wird dir die CLR keinen RuntimecallableWrapper drumrum bauen.
Also das Ergebnis von "Marshal.GetIUnknownForObject", was du aus Delphi sehen würdest.
Robert Giesecke
I’m a great believer in “Occam’s Razor,” the principle which says:
“If you say something complicated, I’ll slit your throat.”
  Mit Zitat antworten Zitat