Calling a .net dll from delphi2006 to show a wpf f

2019-02-07 14:41发布

问题:

I'm using Robert Gieseckes great Unmanaged Exports to call a c#-Dll from Delphi2006. All works well if I use simple procedures and functions with input and output. But now I would like to show a Wpf-Window via the call to OpenMyWindow(). Here I get an "External Exception E0434352". I have no idea why this is not working. Anyway I think it has something to do with Initialization on the wpf side.

Here is the Delphi Code:

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Menus, StdCtrls;

type
  TForm2 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  end;


  procedure OpenMyWindow(); stdcall; external 'ClassLibraryToDelphi.dll';

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
begin
  OpenMyWindow();
end;

end.

and now the c# part (it's a ClassLibrary with an UserControl changed to window):

using System.Linq;
using System.Runtime.InteropServices;
using RGiesecke.DllExport;

namespace ClassLibraryToDelphi
{
    public static class Test
    {
        private static UserControl1 myWindow;

        [DllExport]
        public static void OpenMyWindow()
        {
            myWindow = new UserControl1();
                myWindow.ShowDialog();
        }
    }
}

Here's the xaml:

<Window x:Class="ClassLibraryToDelphi.UserControl1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        mc:Ignorable="d" 
        d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Label Content="WPF Window called from Delphi!" />      
    </Grid>
</Window>

and Codebehind:

using System.Windows;

namespace ClassLibraryToDelphi
{
    public partial class UserControl1 : Window
    {
        public UserControl1()
        {
            InitializeComponent();
        }
    }
}

I think it's nothing special or too complicated to reproduce.

It would be great if Robert see this question (any other answer are appreciated too).

Thank you

回答1:

It's quite simple to do that. Under the Build-Tab in your Project-Options you have to select 'Register for COM interop' and under Signing you must enter a strong name. Than you get a tlb-file for your dll. In Delphi you have to go to 'Component import' and add the tlb-file to your Project. In the dpr-file you have to add Set8087CW($133F) bevor Application.Initialize to disable floating point exceptions.

That's it!



回答2:

Based on the answer from user3016412 I have tried to add the Set8087CW($133F) procedure call. That was enough for me.