首页 > 解决方案 > 从 .NET Core 应用程序 P/Invoke Fusion.dll

问题描述

我正在将 .NET 应用程序迁移到 .NET Core,该应用程序需要执行的任务之一是枚举 .NET 全局程序集缓存的程序集(是的,我知道我的.NET Core 应用程序本身不使用GAC,但我的应用程序的目的之一是创建完整 .NET Framework GAC 中的内容的清单

我尝试 P/Invoke 的任何方法都会Fusion.dll给我同样的错误:

未处理的异常:System.DllNotFoundException:无法加载 DLL 'Fusion.dll' 或其依赖项之一:找不到指定的模块。(来自 HRESULT 的异常:0x8007007E)

代码示例:

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>

注意:我知道CreateAssemblyCache期望 an 的正确签名IAssemblyName并且我在我的代码中拥有该权利,这给了我同样的错误。我在object上面使用来简化可复制的示例。

标签: c#.net-corepinvokegac

解决方案


将平台更改为 x64,并指定 dllImport 的完整路径,以及对 IntPtr 而不是对象的签名对我有用,

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);
}

然而,对于 x86,它奇怪地没有


推荐阅读