我一直在使用在网上找到的一些代码来使用 fusion.dll 查询 GAC,但是最近我收到了一些错误报告,抱怨出现 OverflowException。

    // If assemblyName is not fully qualified, a random matching may be returned!!!!
    public static String QueryAssemblyInfo(String assemblyName)
    {
        ASSEMBLY_INFO assembyInfo = new ASSEMBLY_INFO();
        assembyInfo.cchBuf = 512;
        assembyInfo.currentAssemblyPath = new String('\0',
        assembyInfo.cchBuf);
        IAssemblyCache assemblyCache = null;
        // Get IAssemblyCache pointer
        IntPtr hr = GacApi.CreateAssemblyCache(out assemblyCache, 0);
        if (hr == IntPtr.Zero)
        {
            hr = assemblyCache.QueryAssemblyInfo(1, assemblyName, ref assembyInfo);
            if (hr != IntPtr.Zero)
                Marshal.ThrowExceptionForHR(hr.ToInt32());
        }
        else
            Marshal.ThrowExceptionForHR(hr.ToInt32());
        return assembyInfo.currentAssemblyPath;
    }

有问题的代码是当它尝试将 IntPtr 转换为 Int32 时,而它实际上是 Int64,但问题是 Marshal.ThrowExceptionForHR 只接受 Int32,所以我有点不知道该怎么做。目前我正在处理异常,但我想知道正确的方法是什么?

马龙

有帮助吗?

解决方案

检查您的签名 DllImport 为了 CreateAssemblyCache. 。看起来应该是这样 int, , 不是 IntPtr

[DllImport("fusion.dll")]
internal static extern int CreateAssemblyCache(
    out IAssemblyCache ppAsmCache, int reserved);

其他提示

你为什么使用 IntPtr 保存 HRESULT 的值?HRESULT 的大小与平台无关,它始终是 32 位,因此您应该使用 int 或者 uint 来保持价值。更改代码以使用其中之一,问题就会消失。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top