我一直在使用我在網上找到的一些代碼來使用fusion.dll查詢GAC,但是我最近得到了一些錯誤回報抱怨OverflowException。IntPtr.ToInt32()Marshal.ThrowExceptionForHR() - 查詢GAC
// 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,所以我堅持了什麼,做了一下。目前我只是處理異常,但我想知道做這件事的正確方法是什麼?
馬龍
您的測試是錯誤太多,一個HRESULT是僅當值<0 – 2010-12-09 18:04:47