Question

I've been using some code I've found on the net to query the GAC using the fusion.dll however I've recently been getting a few error reports back complaining of an 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;
    }

The offending code is when its trying to convert the IntPtr to a Int32 when its actually an Int64, but the problem is the Marshal.ThrowExceptionForHR only accepts an Int32 so I'm a bit stuck for what to do. At the moment I'm just handling the exception but I'd like to know what is the correct way of doing it?

Marlon

Was it helpful?

Solution

Check the signature on your DllImport for CreateAssemblyCache. It looks like it should be int, not IntPtr

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

OTHER TIPS

Why are you using an IntPtr to hold the value of an HRESULT? The size of an HRESULT is not platform dependent, it is always 32 bits, so you should use either int or uint to hold the value. Change the code to use one of these instead, and the problem will go away.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top