I am creating a DLL using C++ and importing it using C#. I have no problem compiling the DLL, and it works fine when calling it from .NET 4.7.1. However, when I try to call it from .NET 6.0, I get an EntryPointNotFoundException
error:
Unhandled exception. System.EntryPointNotFoundException:
Unable to find an entry point named 'E' in DLL 'test.dll'.
at Program.E(int a, int b)
at Program.Main(String[] args) in G:\C++\Test\Program.cs:line 6
Test.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!--<TargetFramework>net4.7.1</TargetFramework>-->
<TargetFramework>net6.0</TargetFramework>
<OutputType>Exe</OutputType>
<Configuration>Debug</Configuration>
</PropertyGroup>
</Project>
Test.cpp
extern "C"__declspec(dllexport) int E(int a, int b)
{
return x + y;
}
Program.cs
public class Program
{
[DllImport("test.dll")] public static extern int E(int a, int b);
public static void Main(string [] args)
{
float r = E(11, 26);
System.Console.WriteLine(r);
}
}
My compiler flags are as follows:
g++ -shared -o test.dll Test.cpp -Wl,--out-implib,test.dll
Research:
Because it is clear that this is an issue with .NET 6, I performed some specific searches using Google:
"Unable to find an entry point named" in DLL ".net 6.0" 8 results, 0 valid.
"Unable to find an entry point named" in DLL "net6.0" 10 results, 0 valid.
"Unable to find an entry point named" "net6.0" 47 results, 0 valid.
"Unable to find an entry point named" ".net 6.0" 9 results, 0 valid.
"Unable to find an entry point named" ".net 6" 1370 results, 0 valid.
"Unable to find an entry point named" " dot net 6" 3 results, 0 valid
I performed several other variations of the searches, none of which provided any link to .NET 6 and this error.
I also tried rebooting, using a second computer, and reinstalling .NET 6 on both computers.
I also looked through the .NET 6 documentation, but I could not find a reason this could be happening.