Search code examples
c#methods

Methods not showing as exported functions


I am trying to use the UnmanagedCallersOnly to export some methods for use by native DLL's. I have been following these instructions on how to do this. This is using .Net 7.

When I try and see if there are any exported functions using dumpbin.exe /EXPORTS <file path> It shows nothing is exported. Example code is below:

using System.Runtime.InteropServices;

namespace lib1
{
    public unsafe class test
    {
        [UnmanagedCallersOnly(EntryPoint = "test1")]
        public static int test1()
        {
            Console.WriteLine("Test1 works!");
            return 0;
        }

        [UnmanagedCallersOnly(EntryPoint = "test2")]
        public static int test2()
        {
            Console.WriteLine("Test2 works!");
            return 0;
        }
    }
}

What is the correct way to export a function from a C# dll?

EDIT: Adding the .csproj file

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <AllowUnsafeBlocks>True</AllowUnsafeBlocks>
    <Platforms>AnyCPU;x64;x86</Platforms>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Diagnostics.Runtime" Version="3.1.512801" />
    <PackageReference Include="Microsoft.Diagnostics.Runtime.Utilities" Version="3.0.442202" />
  </ItemGroup>

</Project>

Solution

  • In order to get my methods into the export table I needed to add the <PublishAot>true</PublishAot> property to the .csproj file in the <PropertyGroup> section and compile with the following command:

    dotnet publish -r win-x64 -c Release

    From the folder with the .sln file. Then when I ran the following command I could see the exported functions.

    dumpbin.exe /exports <dll path>