IntPtr.ToInt32 () Marshal.ThrowExceptionForHR () - Interrogazione GAC
Domanda
ho usato un po 'di codice che ho trovato in rete per interrogare il GAC utilizzando il fusion.dll tuttavia Ho recentemente stato sempre un paio di segnalazioni di errori indietro lamentandosi di un 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;
}
Il codice di offendere è quando il suo tentativo di convertire l'IntPtr per un Int32 quando la sua realtà un Int64, ma il problema è l'Marshal.ThrowExceptionForHR accetta solo un Int32, quindi sono un po 'bloccato su cosa fare. Al momento sto solo gestire l'eccezione, ma mi piacerebbe sapere qual è il modo corretto di farlo?
Marlon
Soluzione
Controllare la firma sul DllImport
per CreateAssemblyCache
. Sembra che dovrebbe essere int
, non IntPtr
[DllImport("fusion.dll")]
internal static extern int CreateAssemblyCache(
out IAssemblyCache ppAsmCache, int reserved);
Altri suggerimenti
Perché stai usando un IntPtr
per contenere il valore di un HRESULT? La dimensione di un HRESULT non dipende dalla piattaforma, è sempre di 32 bit, quindi è necessario utilizzare uno o int
uint
per contenere il valore. Modificare il codice per utilizzare uno di questi, invece, e il problema andrà via.