intptr.toint32 () Marshal.ThrowExceptionforhr () - الاستعلام عن GAC
سؤال
لقد كنت أستخدم بعض التعليمات البرمجية التي وجدتها على الشبكة للاستعلام عن GAC باستخدام Fusion.dll ، ومع ذلك ، فقد حصلت مؤخرًا على بعض تقارير الأخطاء التي تشكو من الفائض.
// 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 فعليًا ، لكن المشكلة هي المارشال. في الوقت الحالي ، أتعامل مع الاستثناء ، لكنني أود أن أعرف ما هي الطريقة الصحيحة للقيام بذلك؟
مارلون
المحلول
تحقق من التوقيع الخاص بك DllImport
ل CreateAssemblyCache
. يبدو أنه ينبغي أن يكون int
, ، ليس IntPtr
[DllImport("fusion.dll")]
internal static extern int CreateAssemblyCache(
out IAssemblyCache ppAsmCache, int reserved);
نصائح أخرى
لماذا تستخدم IntPtr
لعقد قيمة hresult؟ لا يعتمد حجم hresult على النظام الأساسي ، فهو دائمًا 32 بت ، لذلك يجب عليك استخدامك أيضًا int
أو uint
لعقد القيمة. قم بتغيير الكود لاستخدام أحد هذه الأشياء بدلاً من ذلك ، وستذهب المشكلة.
لا تنتمي إلى StackOverflow