P/Invoke Fusion.dll from a .NET Core application

0

I'm migrating a .NET application to .NET Core, and one of the tasks that the app needs to do, is to enumerate assemblies the .NET Global Assembly Cache (yes, I know my .NET Core app itself doesn't use the GAC, but one of the purposes of my app is to create an inventory of what is in the full .NET Framework GAC)

Any method that I try to P/Invoke in the Fusion.dll gives me the same error:

Unhandled Exception: System.DllNotFoundException: Unable to load DLL 'Fusion.dll' or one of its dependencies: The specified module could not be found. (Exception from HRESULT: 0x8007007E)

Code example:

class Program
{
    static void Main(string[] args)
    {
        GacUtility.CreateAssemblyCache(out var x, 0);
    }
}

public class GacUtility
{
    [DllImport("Fusion.dll")]
    public static extern int CreateAssemblyCache(out object asmCache, int reserved);
}

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>

</Project>

NB: I know that correct signature of CreateAssemblyCache expects an IAssemblyName and I have that right in my code, which gives me the same error. I'm using object above to simplify a reproduceable example.

c#
.net-core
pinvoke
gac
asked on Stack Overflow Jul 16, 2019 by Lindsey1986 • edited Jul 16, 2019 by Lindsey1986

1 Answer

0

Changing platform to x64, and specifying full path to the dllImport, and the signiture to IntPtr instead of object worked for me,

class Program
{
    static void Main(string[] args)
    {
        GacUtility.CreateAssemblyCache(out IntPtr x, 0);
    }
}

public class GacUtility
{
    [DllImport(@"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\fusion.dll")]
    public static extern int CreateAssemblyCache(out IntPtr asmCache, int reserved);
}

yet, for x86 it strangely did not

answered on Stack Overflow Jul 16, 2019 by Sohaib Jundi

User contributions licensed under CC BY-SA 3.0