3F / DllExport

.NET DllExport with .NET Core support (aka 3F/DllExport aka DllExport.bat)
MIT License
962 stars 133 forks source link

exported function present when building debug, missing from published #235

Open ggegoge opened 1 month ago

ggegoge commented 1 month ago

tldr

The issue is the following: I manage to create a DLL with my c# functions exposed to C++. But I miss the runtime then, When I try to publish the dll as self contained to generate other DLLs, I am missing the function from the DLL. I believe that the Dll in publish doesn't get modified. Path: to the published dll: \bin\Release\net8.0-windows\publish\win-x64\*

Question

  1. am I doing something wrong to miss the runtime? when running the c# function from c# it works
  2. for the published version: am I doing something wrong to miss the function there or is it a bug that this dll doesn't get modified?

more details

I have simple C# code:

public class EntryPoint
{
    [DllExport(CallingConvention = CallingConvention.StdCall)]
    public static void StartApplication()
    {
        Console.WriteLine("Application started from C# DLL!! !");
    }
}

it is in a project targeting .NET 8.0. When running the following c++ code:

int main()
{
    HMODULE appModule = LoadLibraryEx(L"Dummy.dll", nullptr, 0);
    auto startApplicationFunction = GetProcAddress(appModule, "StartApplication");
    startApplicationFunction();
    return 0;
}

I was using the DLL by simply building the project in visual studio

I am missing the runtime: Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies

I tried to publish the DLL as self contained and then I would paste all of the required DLLs to see if then having System.Runtime.dll clearly in path fixes the issue but the problem then is that the dll created this way does not contain the StartApplication function

for the self contained build:

  <PropertyGroup>
    <Configuration>Debug</Configuration>
    <Platform>x64</Platform>
    <PublishDir>bin\Release\net8.0-windows\publish\win-x64\</PublishDir>
    <PublishProtocol>FileSystem</PublishProtocol>
    <_TargetId>Folder</_TargetId>
    <TargetFramework>net8.0-windows</TargetFramework>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <SelfContained>true</SelfContained>
    <PublishSingleFile>false</PublishSingleFile>
    <PublishReadyToRun>false</PublishReadyToRun>
  </PropertyGroup>
</Project>

the csproj for the project itself is dotnet new classlib + DllExport's additions


The question is related to:

3F commented 1 month ago

Hello,

The publish directory has already been discussed earlier https://github.com/3F/DllExport/issues/224#issuecomment-1545023216

It seems I need to add something like this later for automatic generation in future versions.

But this is not the problem in your case:

Could not load file or assembly 'System.Runtime, Version=8.0.0.0,

3F/DllExport project does not officially support the modern .NET 8 platform yet (1.7.4). I apologize for the inconvenience.

FYI: you can temporarily add <LangVersion>latest</LangVersion> to support some modern language features on older target platforms