Skip to content

Commit

Permalink
Use other overload if module has no known address
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfsck committed Apr 18, 2018
1 parent 7ae4ce7 commit 9b86179
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/DotNet/ModuleDefMD.cs
Expand Up @@ -212,14 +212,10 @@ public sealed class ModuleDefMD : ModuleDefMD2, IInstructionOperandResolver {
if (GetHINSTANCE == null)
throw new NotSupportedException("System.Reflection.Module loading is not supported on current platform");

var addr = (IntPtr)GetHINSTANCE.Invoke(null, new[] { mod });
return (IntPtr)GetHINSTANCE.Invoke(null, new[] { mod });
#else
var addr = Marshal.GetHINSTANCE(mod);
return Marshal.GetHINSTANCE(mod);
#endif
if (addr == IntPtr.Zero || addr == new IntPtr(-1))
throw new ArgumentException("It is not possible to get address of module");

return addr;
}

/// <summary>
Expand All @@ -231,9 +227,12 @@ public sealed class ModuleDefMD : ModuleDefMD2, IInstructionOperandResolver {
/// <returns>A new <see cref="ModuleDefMD"/> instance</returns>
public static ModuleDefMD Load(System.Reflection.Module mod, ModuleCreationOptions options, ImageLayout imageLayout) {
var addr = GetModuleHandle(mod);
if (addr == new IntPtr(-1))
if (addr != IntPtr.Zero && addr != new IntPtr(-1))
return Load(addr, options, imageLayout);
var location = mod.FullyQualifiedName;
if (string.IsNullOrEmpty(location) || location[0] == '<')
throw new InvalidOperationException($"Module {mod} has no HINSTANCE");
return Load(addr, options, imageLayout);
return Load(location, options);
}

/// <summary>
Expand Down Expand Up @@ -1375,7 +1374,7 @@ public sealed class ModuleDefMD : ModuleDefMD2, IInstructionOperandResolver {
return null;
ModuleDefMD module;
try {
module = ModuleDefMD.Load(fileName);
module = Load(fileName);
}
catch {
module = null;
Expand Down

0 comments on commit 9b86179

Please sign in to comment.