简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM