Errors with DllExport 1.8.1 , .Net 8.0 and Delphi: Could not load type ... from assembly ...
DllExport version
1.8.1
Environment
Visual Studio IDE
Ask the .NET DllExport project
DLL native export works with DllExport 1.7.4 - Delphi code calls .Net v4.8 function.
I updated the project and it does not work anymore - .Net 8.0, DllExport to 1.8.1. Please, find attached a demo project.
DllExport options in this demo are from here: https://github.com/3F/DllExport/blob/master/src/DllExport/assets/NetCoreAsset/NetCoreAsset.csproj
-
It works when the exported function is empty, without parameters, without result.
-
The attached demo calls
System.Diagnostics.Process.Start. Similar with the real project.
2.1. Most of the time I get exception:
EExternalException: External exception E0434352
There is try/catch, but it does not help. I suspect an assembly is not found, but I do not see any details. This is the error I get with attached demo project.
2.2. Rarely, there is another error:
Could not load type 'System.Diagnostics.Process' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
I wanted to prepare a demo for this second error, but I could not.
In this case, I tried to add System.Diagnostics.Process to DllExportRefPackages list. It did not help.
Please note the "README-files-list" - dependent assembly DLLs are found Only in the Publish folder. I suspect, that is after ILRepack step.
During the tests I used SelfContained publish:
Properties\PublishProfiles\FolderProfile.pubxml: <SelfContained>true</SelfContained>
Thank you.
DllExport configuration
Code of Conduct
- [x] I agree to follow this project's Code of Conduct
I have the same problem, version 1.7.4 with .net Framework 4.8 works, but on 1.8.1 and on 1.8.0 I also get the exception E0434352 in debug session. When I start the application directly from console I get the mscorlib error.
System.TypeLoadException: Could not load type "System.Uri" from assembly "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089".
Hello,
@plamenjm, Thanks for the prepared example!
Depending on your environment and goal, you can configure your project as follows:
- [Asm]
- +
System | b77a5c561934e089 | 4:0:0:0
- +
- [Types]
- +
System.Diagnostics.Process | | at | System - +
System.ComponentModel. | v | at | System
- +
Then Apply and try again,
Process.Start("notepad.exe"); // net8.0, ok
Or,
for example, using System.Diagnostics.Process package ([Ref] merging is optional starting with 1.8.1):
- [Ref]
- +
System.Diagnostics.Process | 4.3.0 | runtimes/win/lib/netstandard1.4/System.Diagnostics.Process.dll
- +
- [Types]
- +
System.Diagnostics.Process | | assert | - +
System.ComponentModel. | v | at | System
- +
Or,
if you don't have any specific needs from netcore regarding this issue and don't want the extra steps of trying to configure it: change net8.0 as net480 and set LangVersion=latest
etc.
@irs-mima1, Please follow the same steps as above depending on your needs. Or please clarify
1.7.4 with .net Framework 4.8 works, but on 1.8.1 and on 1.8.0 I also get the exception E0434352
Did you also change TFM from the mentioned netfx on netcore? If so, please check this out, does exactly net480 (or like) produce the mentioned problem for 1.8+ ?
For everyone, please note:
If your task does not require netcore targeting, the easiest way to use without complex reconfiguration like above is to simply use any modern netfx-based TFM and the latest version of the language to access modern syntactic sugar etc. I mean that's what I was talking about when I said "... problems that need to be solved specifically for specific projects (totally not a general solution at all) ..." here
I think there was a misunderstanding in my case. It is still a .NET Framework 4.8 DLL (no core), I just upgraded the DllExport library to the latest and I got the error.
I can try what you mentioned with [Asm] and [Types] but then I need to find all types I used in that project or?
Edit: If I add all assemblies and types then it works until that point the DLL reference to a further DLL writen by me. I can not add that DLL to the [Asm] tab, because it doesn't have a public key. Why the new DLLExport redirects all types to mscorlib by default? Do I miss an option I need now?
@irs-mima1 It is impossible to say anything about the problem without minimal information. And it is obvious to me your configuration is incorrect; you do NOT need to configure [Asm] and [Types] like above for .NET Framework 4.8; not in your case.
This is different from this issue, please open a new one as a bug. I need to know at least the configuration and info about the environment you are using for both cases 1.7.4 and 1.8.x. Thanks!
Hello,
im sorry for following up on this. I have a project that i build before as 4.7 .net project. This i decided to change this to .net 8.0, because we want to change to linux enviroments. The DLL i plan to build will be run on Windows.
In my case the process process using the dll is a third party app to me. i rebased the system objects though the dllexport.bat gui. This gets the dll getting detected from the delphi host programm.
When i now start a function i get:
Externe Exception E0434352
i assume its a problem with the loading of dependencies. After that i run in powershell 7 the following. PS C:\Users\Robert> $assembly = [System.Reflection.Assembly]::LoadFrom($assemblyPath) PS C:\Users\Robert> $assembly.getTypes() MethodInvocationException: Exception calling "GetTypes" with "0" argument(s): "Unable to load one or more of the requested types. Could not load type 'System.Net.Http.DelegatingHandler' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. Could not load type 'System.Dynamic.DynamicObject' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. Could not load type 'System.Dynamic.DynamicObject' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. Could not load type 'System.Dynamic.DynamicObject' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. Could not load type 'System.Dynamic.DynamicObject' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. Could not load type 'System.Dynamic.DynamicObject' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. Could not load type 'System.Dynamic.DynamicObject' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. Could not load type 'System.Dynamic.DynamicObject' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'."
I tried to solve this with the advice that you gave the issue creator, sadly i was not able to figure this out. I would be happy if someone could give me advice to get this working.
here maybe relevant code/configuration parts:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Platforms>x86;x64</Platforms>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x86'">
<Prefer32Bit>True</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x86'">
<Prefer32Bit>True</Prefer32Bit>
<Optimize>False</Optimize>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Optimize>False</Optimize>
</PropertyGroup>
<PropertyGroup>
<DllExportIdent>D245DC41-03E7-483B-BB91-EBA956199FE0</DllExportIdent>
<DllExportMetaLibName>DllExport.dll</DllExportMetaLibName>
<DllExportNamespace>SL_integration</DllExportNamespace>
<DllExportDDNSCecil>true</DllExportDDNSCecil>
<PlatformTarget>x86</PlatformTarget>
<DllExportOrdinalsBase>1</DllExportOrdinalsBase>
<DllExportGenExpLib>false</DllExportGenExpLib>
<DllExportOurILAsm>true</DllExportOurILAsm>
<DllExportSysObjRebase>true</DllExportSysObjRebase>
<DllExportLeaveIntermediateFiles>false</DllExportLeaveIntermediateFiles>
<DllExportTimeout>30000</DllExportTimeout>
<DllExportPeCheck>3</DllExportPeCheck>
<DllExportPatches>3</DllExportPatches>
<DllExportRefreshObj>true</DllExportRefreshObj>
<DllExportILAsmExternAsm />
<DllExportILAsmTypeRef />
<DllExportTypeRefOptions>0</DllExportTypeRefOptions>
<DllExportRefPackages />
<DllExportPreProcType>218</DllExportPreProcType>
<DllExportILMerge />
<DllExportPostProcType>0</DllExportPostProcType>
<DllExportDir>$(MSBuildProjectDirectory)\..\</DllExportDir>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Conari" Version="1.5.0" />
<PackageReference Include="Dapper" Version="2.1.66" />
<PackageReference Include="DllExport" Version="1.8.1" />
<PackageReference Include="ILRepack">
<Version>2.0.43</Version>
<Wz>1</Wz>
</PackageReference>
<PackageReference Include="System.Data.SqlClient" Version="4.9.0" />
</ItemGroup>
<Target Name="DllExportPreProc" BeforeTargets="DllExportMod" Label="8337224c9ad9e356:PreProc">
<Copy SourceFiles="$(DllExportMetaLibFullPath)" DestinationFolder="$(TargetDir)" SkipUnchangedFiles="true" OverwriteReadOnlyFiles="true" ContinueOnError="true" />
<Move SourceFiles="$(TargetPath)" DestinationFiles="$(TargetDir).ilm0\$(TargetName).dll" OverwriteReadOnlyFiles="true" ContinueOnError="true" />
<Move SourceFiles="$(TargetDir)$(TargetName).pdb" DestinationFiles="$(TargetDir).ilm0\$(TargetName).pdb" OverwriteReadOnlyFiles="true" ContinueOnError="true" />
<Exec Command="$(ILRepack) .ilm0\$(TargetName).dll Conari.dll /out:$(TargetFileName)" WorkingDirectory="$(TargetDir)" ContinueOnError="true" />
<RemoveDir Directories="$(TargetDir).ilm0" ContinueOnError="true" />
<Delete Files="$(TargetDir)$(DllExportMetaLibName);$(TargetDir)Conari.dll" ContinueOnError="true" />
</Target>
<ImportGroup Label=".NET DllExport">
<Import Project="$(DllExportDir)packages\DllExport.1.8.1\tools\net.r_eg.DllExport.targets" Condition="Exists($([MSBuild]::Escape('$(DllExportDir)packages\DllExport.1.8.1\tools\net.r_eg.DllExport.targets')))" Label="8337224c9ad9e356" />
</ImportGroup>
<Target Name="DllExportRestorePkg" BeforeTargets="PrepareForBuild">
<Error Condition="'$(DllExportNoRestore)'!='true' And !Exists('$(DllExportDir)DllExport.bat')" Text="DllExport.bat was not found in $(DllExportDir); https://github.com/3F/DllExport" />
<Exec Condition="'$(DllExportNoRestore)'!='true' And ('$(DllExportModImported)'!='true' Or !Exists('$(DllExportDir)packages\DllExport.1.8.1\tools\net.r_eg.DllExport.targets') Or !Exists('$(DllExportDir)packages\DllExport.1.8.1\gcache')) And Exists('$(DllExportDir)DllExport.bat')" Command=".\DllExport.bat -action Restore" WorkingDirectory="$(DllExportDir)" />
<MSBuild Condition="'$(DllExportModImported)'!='true'" Projects="$(DllExportDir)packages\DllExport.1.8.1\tools\net.r_eg.DllExport.targets" Targets="DllExportMetaXBaseTarget" Properties="TargetFramework=$(TargetFramework)">
<Output TaskParameter="TargetOutputs" PropertyName="DllExportMetaXBase" />
</MSBuild>
<ItemGroup>
<Reference Include="DllExport, PublicKeyToken=8337224c9ad9e356">
<HintPath>$(DllExportDir)packages\DllExport.1.8.1\gcache\$(DllExportMetaXBase)\$(DllExportNamespace)\$(DllExportMetaLibName)</HintPath>
<Private>False</Private>
<SpecificVersion>False</SpecificVersion>
</Reference>
</ItemGroup>
</Target>
<Target Name="DllExportRPkgDynamicImport" BeforeTargets="PostBuildEvent" DependsOnTargets="GetFrameworkPaths" Condition="'$(DllExportModImported)'!='true' And '$(DllExportRPkgDyn)'!='false'">
<MSBuild BuildInParallel="true" UseResultsCache="true" Projects="$(MSBuildProjectFullPath)" Properties="Configuration=$(Configuration);DllExportRPkgDyn=true" Targets="Build" />
</Target>
</Project>
using System.Data;
using System.Globalization;
using System.Reflection.Metadata;
using System.Runtime.InteropServices;
using net.r_eg.Conari.Types;
using SL_integration.Models;
using static SL_integration.helper;
using static SL_integration.SLMobileAPI;
using static SL_integration.SQLServerConnection;
namespace SL_integration;
public static class FunctionsToExport
{
[DllExport("TransferPreorderToOrder", CallingConvention.StdCall)]
public unsafe static void TransferPreorderToOrder(int aCount, WCharPtr* aNames, WCharPtr* aValues)
{
Console.Write("filler");
}
}
@cv-prod-github Hello,
we want to change to linux enviroments. The DLL i plan to build will be run on Windows.
Linux is not supported yet; including final modified modules.
And you're getting an error because the rebase feature is configured to specify mscorlib by default. Although .typeref ([Types] in DllExport Wizard) can be used to control the environment including custom host wrapping (#243), there is no official solution yet due to its complexity as a common solution for all