Querying the x64-GAC

ε祈祈猫儿з 提交于 2019-12-20 01:45:44

问题


With the following code I'm querying the GAC to see if there is a certain assembly installed i it. The code itself works fine, but I only get the result for the x86 GAC. The assembly is installed in both GACs GAC_64 and GAC_32.

What do I have to do so that 'QueryAssemblyInfo' checks the x64 GAC?

  public bool IsInGac(string assemblyName)
  {
     ASSEMBLY_INFO assembyInfo = new ASSEMBLY_INFO();
     assembyInfo.cchBuf = 512;
     assembyInfo.currentAssemblyPath = new string('\0', assembyInfo.cchBuf);

     IAssemblyCache assemblyCache = null;

     IntPtr hr = NativeMethods.CreateAssemblyCache(out assemblyCache, 0);
     if (hr == IntPtr.Zero)
     {
        hr = assemblyCache.QueryAssemblyInfo(1, assemblyName, ref assembyInfo);
        if (hr != IntPtr.Zero)
        {
           return false;
        }

        return true;
     }

     return false;
  }

  internal static class NativeMethods
  {
     [DllImport("fusion.dll")]
     public static extern IntPtr CreateAssemblyCache(out IAssemblyCache ppAsmCache, int reserved);
  }

  [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("e707dcde-d1cd-11d2-bab9-00c04f8eceae")]
  internal interface IAssemblyCache
  {
     int Dummy1();
     [PreserveSig]
     IntPtr QueryAssemblyInfo(int flags, [MarshalAs(UnmanagedType.LPWStr)]string assemblyName, ref ASSEMBLY_INFO assemblyInfo);
     int Dummy2();
     int Dummy3();
     int Dummy4();
  }

  [StructLayout(LayoutKind.Sequential)]
  internal struct ASSEMBLY_INFO
  {
     public int cbAssemblyInfo;
     public int assemblyFlags;
     public long assemblySizeInKB;

     [MarshalAs(UnmanagedType.LPWStr)]
     public String currentAssemblyPath;

     public int cchBuf;
  }

回答1:


    [DllImport("fusion.dll")]
    public static extern IntPtr CreateAssemblyCache(...)

The return type for this function is HRESULT. Which is an int, not IntPtr in C#. Same thing on the QueryAssemblyInfo() declaration.

This could cause random failure when you target AnyCPU. Other than that the code is fine and it has no trouble finding assemblies in GAC_64 on my machine.




回答2:


This is all available via System.EnterpriseServices assembly since.NET 1.1

var publisher = new System.EnterpriseServices.Internal.Publish();
publisher.GacInstall(@"C:\Temp\MyDLL.dll")

Or

publisher.GacRemove(@"C:\Temp\MyDLL.dll")


来源:https://stackoverflow.com/questions/12158170/querying-the-x64-gac

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!