We have a 32-bit application written in Delphi. We also have a Windows Forms Class Library written in C#. To export the library functions we use NuGet DllExport package. Everything works fine with .Net Framework 4.8.1 library. But in .Net 8 library an exception occurs if function shows form.
To localize the problem, we created test projects.
Delphi code:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; function AddOne(n: integer): integer; cdecl; external 'DllExportTest.dll'; cdecl; function AddOneAndShowMessage(n: integer): integer; cdecl; external 'DllExportTest.dll'; cdecl; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var n: integer; begin n := AddOne(1); ShowMessage(inttostr(n)); end; procedure TForm1.Button2Click(Sender: TObject); var n: integer; begin n := AddOneAndShowMessage(1); ShowMessage(inttostr(n)); end; end. .Net Framework 4.8.1 Class Library code
using System.Runtime.InteropServices; using System.Windows.Forms; namespace DllExportTest { public class Class1 { [DllExport(CallingConvention = CallingConvention.Cdecl)] public static int AddOne(int n) { return n + 1; } [DllExport(CallingConvention = CallingConvention.Cdecl)] public static int AddOneAndShowMessage(int n) { MessageBox.Show("AddOneAndShowMessage function called"); return n + 1; } } } We have created a Class Library (.Net Framework) and added a dependency on System.Windows.Forms. Then we installed the NuGet package DllExport. DllExport settings for this project:
<DllExportIdent>5F111A67-5D02-4672-9983-C3F0762C3FE6</DllExportIdent> <DllExportMetaLibName>DllExport.dll</DllExportMetaLibName> <DllExportNamespace>DllExportTest</DllExportNamespace> <DllExportDDNSCecil>true</DllExportDDNSCecil> <DllExportSkipOnAnyCpu>false</DllExportSkipOnAnyCpu> <DllExportPlatform>Auto</DllExportPlatform> <DllExportOrdinalsBase>1</DllExportOrdinalsBase> <DllExportGenExpLib>false</DllExportGenExpLib> <DllExportOurILAsm>true</DllExportOurILAsm> <DllExportSysObjRebase>false</DllExportSysObjRebase> <DllExportLeaveIntermediateFiles>false</DllExportLeaveIntermediateFiles> <DllExportTimeout>30000</DllExportTimeout> <DllExportPeCheck>6</DllExportPeCheck> <DllExportPatches>0</DllExportPatches> <DllExportRefreshObj>false</DllExportRefreshObj> <DllExportILAsmExternAsm /> <DllExportILAsmTypeRef /> <DllExportTypeRefOptions>0</DllExportTypeRefOptions> <DllExportRefPackages /> <DllExportPreProcType>0</DllExportPreProcType> <DllExportPostProcType>0</DllExportPostProcType> <DllExportDir>$(MSBuildProjectDirectory)\..\</DllExportDir> Both functions (AddOn and AddOneAndShowMessage) work fine. When calling AddOneAndShowMessage, a MessageBox appears with a message "AddOneAndShowMessage function called".
We then created a .Net8 Windows Forms Class Library with the same functions. DllExport settings for this project:
<DllExportIdent>6E6B4E9D-2474-49CD-B8F9-5BA6C3B7AD59</DllExportIdent> <DllExportMetaLibName>DllExport.dll</DllExportMetaLibName> <DllExportNamespace>DllExportTest</DllExportNamespace> <DllExportDDNSCecil>true</DllExportDDNSCecil> <DllExportSkipOnAnyCpu>false</DllExportSkipOnAnyCpu> <DllExportPlatform>Auto</DllExportPlatform> <DllExportOrdinalsBase>1</DllExportOrdinalsBase> <DllExportGenExpLib>false</DllExportGenExpLib> <DllExportOurILAsm>true</DllExportOurILAsm> <DllExportSysObjRebase>true</DllExportSysObjRebase> <DllExportLeaveIntermediateFiles>false</DllExportLeaveIntermediateFiles> <DllExportTimeout>30000</DllExportTimeout> <DllExportPeCheck>6</DllExportPeCheck> <DllExportPatches>0</DllExportPatches> <DllExportRefreshObj>false</DllExportRefreshObj> <DllExportILAsmExternAsm /> <DllExportILAsmTypeRef /> <DllExportTypeRefOptions>0</DllExportTypeRefOptions> <DllExportRefPackages /> <DllExportPreProcType>0</DllExportPreProcType> <DllExportPostProcType>0</DllExportPostProcType> <DllExportDir>$(MSBuildProjectDirectory)\..\</DllExportDir> The AddOne function works. But calling the AddOneAndShowMessage function raises the еxternal exception E0434352.
Logging has shown that the exception occurs before entering the function.
If the "DllExportSysObjRebase" parameter set to false, the "AddOne" function also raises external exception E0434352.
Question: how to use DllExport correctly with .NET8.0 Windows Forms Class Library, if there is a function showing a form?
Development tools used: Embarcadero® Delphi® 2010 Microsoft Visual Studio Community 2022


Rebase System Objectoption,<DllExportOurILAsm>false</DllExportOurILAsm>,<DllExportSysObjRebase>true</DllExportSysObjRebase>,<DllExportRefreshObj>true</DllExportRefreshObj>, the other options are the same -- As a note, I used__stdcall<DllExportOurILAsm>is set tofalse. But I don't really think that it matters. As mentioned, I used__stdcalland I haven't tested__cdecl, but what matters most - and of course I forgot to say it, probably because it felt obvious - in thepre-processingtab I've selectedILMerge. Without that, it doesn't work (generates the same exception you've described)