I have an application that was previously written in .NET Framework. Robert Giesecke UnmanagedExport worked perfect for adding the [DllExport]
attribute to my functions to expose them to native languages.
Now that we have upgraded to .Net6.0 this doesn't seem to work. The functions export and are visible using dumpbin
. They do load in the native language, and function address can be found. Although, when executing the function I am given an exception Unhandled exception at 0x00007FFB928CCD29 (KernelBase.dll) in NativeExportTest.exe: 0xE0434352
If I switch back to .NET Framework or .NET Standard, everything works again. I have tried the following packages;
- UnmanagedExports.Repack
- UnmanagedExports.Repack.Updgrade
- DllExport
- NativeAOT (Works, but doesn't support all the features used in .NET 6.0).
I also looked at [UnmanagedCallersOnlyAttribute]
for .NET6.0 but the underlying native language is something wacky that gets compiled into c++ at runtime. I do not believe it supports callbacks.
Is there someone way to export the .NET6.0 c# functions to native languages without relying on a callback?
I only need a solution for windows.
C#
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Platforms>AnyCPU;x64</Platforms>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="UnmanagedExports.Repack.Upgrade" Version="1.2.1" />
</ItemGroup>
</Project>
public class Class1
{
[DllExport]
public static int _add(int a, int b)
{
return a + b;
}
}
Some C++ that works with .NET Framework but not .NET Core.
const TCHAR* pemodule = _T("path to release dll");
HMODULE lib = LoadLibrary(pemodule);
typedef int( *_add)(int a, int b);
auto pAdd = (_add)GetProcAddress(lib, "_add");
int c = pAdd(1,2); // Exception here
Following Hans Passants suggestion of enabling mixed debugging lead me to a new exception. Now the exception is shown in the console before any code gets to load:
Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.