Einzelnen Beitrag anzeigen

Benutzerbild von TTF
TTF

Registriert seit: 19. Mai 2013
70 Beiträge
 
Delphi XE4 Enterprise
 
#1

Delphi DLL Schnittstelle zu C#

  Alt 16. Jul 2014, 13:26
Ich habe eine Test-Schnittstelle von Delphi nach C# geschrieben.

Hier ist der Delphi Code der DLL:
Code:

type
ITest = interface
  ['{D032F796-167D-4B0D-851D-2AEEA226646A}']
  Function GetTest : Integer; stdcall;
  Procedure SetTest(value : integer); stdcall;
end;

TTest = class(TInterfacedObject, ITest)
   public Function GetTest : Integer; stdcall; export;
   public Procedure SetTest(value : integer); stdcall; export;
   public Test: integer;
end;

Function TTest.GetTest() : Integer; stdcall; export;
begin
  result := Test;
end;

Procedure TTest.SetTest(value : Integer); stdcall; export;
begin
  Test := value;
end;

function GetTest() : TTest; stdcall; export;
begin
  Result := TTest.Create;
end;

exports
  GetTest;

end.
Hier der C# Code:

Code:
namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        [ComImport, Guid("2B4AD756-C379-4BAB-9804-72E9A9E0B26F"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        public interface ITest
        {
            [MethodImplAttribute(MethodImplOptions.PreserveSig)]
            [return: MarshalAs(UnmanagedType.U1)]
            void SetTest(int value);
            [MethodImplAttribute(MethodImplOptions.PreserveSig)]
            [return: MarshalAs(UnmanagedType.U1)]
            int GetTest();
        }

        public abstract class TTest : ITest
        {
            public abstract void SetTest(int value);
            public abstract int GetTest();
        }

        [DllImport("Schnittstellen_1.dll", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
        public static extern TTest GetTest();    

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Initialized(object sender, EventArgs e)
        {
            GetTest().SetTest(2);
            MessageBox.Show(GetTest().GetTest().ToString());
        }
    }
}
Wäre extrem dankbar, wenn sich jemand den Code durchlesen und auf Fehler überpfüfen könnte.

Fehlermeldung in VisualStudio:
In System.Runtime.InteropServices.COMException ist eine Ausnahme aufgetreten.

Unbekannter Fehler(Ausnahme von HRESULT: 0x80004005(E_FAIL))
Quidquid id est, timeo perfossores machinae computatoriae et dona ferentes...
  Mit Zitat antworten Zitat