使用以下代码我正在查询GAC以查看是否安装了某个程序集。代码本身工作正常,但我只得到x86 GAC的结果。该程序集安装在GAC GAC_64和GAC_32中。
我需要做什么才能让'QueryAssemblyInfo'检查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;
}
答案 0 :(得分:2)
[DllImport("fusion.dll")]
public static extern IntPtr CreateAssemblyCache(...)
此函数的返回类型为HRESULT。这是 int ,而不是C#中的IntPtr。 QueryAssemblyInfo()声明中的内容相同。
当您定位AnyCPU时,这可能会导致随机失败。除此之外,代码很好,并且在我的机器上的GAC_64中查找程序集也没有问题。
答案 1 :(得分:0)
自.NET 1.1以来,这一切都可通过System.EnterpriseServices程序集获得
var publisher = new System.EnterpriseServices.Internal.Publish();
publisher.GacInstall(@"C:\Temp\MyDLL.dll")
或者
publisher.GacRemove(@"C:\Temp\MyDLL.dll")