2

我一直在使用我在网上找到的一些代码来使用 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,所以我有点不知道该怎么做。目前我只是在处理异常,但我想知道正确的方法是什么?

马龙

4

2 回答 2

5

检查您的DllImportfor上的签名CreateAssemblyCache。看起来应该是int,不是IntPtr

[DllImport("fusion.dll")]
internal static extern int CreateAssemblyCache(
    out IAssemblyCache ppAsmCache, int reserved);
于 2010-12-09T17:08:26.247 回答
4

为什么要使用 anIntPtr来保存 HRESULT 的值?HRESULT 的大小与平台无关,它始终为 32 位,因此您应该使用intuint来保存该值。更改代码以使用其中之一,问题就会消失。

于 2010-12-09T17:06:37.487 回答