Skip to content

Instantly share code, notes, and snippets.

@Tak
Created January 23, 2012 13:55
Show Gist options
  • Save Tak/1663276 to your computer and use it in GitHub Desktop.
Save Tak/1663276 to your computer and use it in GitHub Desktop.
public static bool IsManagedAssembly (string file)
{
try {
using (Stream fs = new FileStream (file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
BinaryReader reader = new BinaryReader(fs);
//The 4-byte PE Header indicator is at 0x3C
fs.Position = 0x3C;
uint peHeader = reader.ReadUInt32();
//Jump to the CLR header start location at 0xE8 past the PE header
fs.Position = peHeader + 0xe8;
// These two bytes will be nonzero for managed assemblies,
// and zero for native dlls
return (0UL != reader.ReadUInt64 ());
}// using fs
} catch { } // Don't care
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment