static bool IsClrImage( string fileName ) {
FileStream fs = null;
try {
fs = new FileStream( fileName, FileMode.Open, FileAccess.Read,
FileShare.Read );
byte[] dat = new byte[300];
fs.Read( dat, 0, 128 );
if( (dat[0] != 0x4d) || (dat[1] != 0x5a) ) // "MZ" DOS header
return false;
int lfanew = BitConverter.ToInt32( dat, 0x3c );
fs.Seek( lfanew, SeekOrigin.Begin );
fs.Read( dat, 0, 24 ); // read signature & PE file header
if( (dat[0] != 0x50) || (dat[1] != 0x45) ) // "PE" signature
return false;
fs.Read( dat, 0, 96 + 128 ); // read PE optional header
if( (dat[0] != 0x0b) || (dat[1] != 0x01) ) // magic
return false;
int clihd = BitConverter.ToInt32( dat, 208 ); // get IMAGE_COR20_HEADER
rva-address
return clihd != 0;
}
catch( Exception ) {
return false;
}
finally {
if( fs != null )
fs.Close();
}
} |