IntPtr.ToInt32()Marshal.ThrowExceptionForHR() - 查询GAC

我一直在使用我在网上找到的一些代码来使用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('',
        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只接受一个I​​nt32所以我有点卡住了该做什么。目前我只是在处理异常,但我想知道这样做的正确方法是什么? 马龙     
已邀请:
检查
DllImport
上的签名是否为
CreateAssemblyCache
。它看起来应该是
int
,而不是
IntPtr
[DllImport("fusion.dll")]
internal static extern int CreateAssemblyCache(
    out IAssemblyCache ppAsmCache, int reserved);
    
你为什么用
IntPtr
来保存HRESULT的值? HRESULT的大小不依赖于平台,它总是32位,因此您应该使用
int
uint
来保存该值。更改代码以使用其中一个代码,问题就会消失。     

要回复问题请先登录注册