Exemplo n.º 1
0
 public static MachineType GetMachineType(string path)
 {
     // Open the PE File as a binary file, and parse just enough information to determine the
     // machine type.
     //http://www.microsoft.com/whdc/system/platform/firmware/PECOFF.mspx
     using (SafeFileHandle safeHandle =
                NativeMethods.CreateFile(
                    path,
                    NativeAccessFlags.GenericRead,
                    FileShare.Read,
                    IntPtr.Zero,
                    FileMode.Open,
                    FileAttributes.Normal,
                    IntPtr.Zero)) {
         FileStream fs = new FileStream(safeHandle, FileAccess.Read);
         using (BinaryReader br = new BinaryReader(fs)) {
             fs.Seek(0x3c, SeekOrigin.Begin);
             Int32 peOffset = br.ReadInt32();
             fs.Seek(peOffset, SeekOrigin.Begin);
             UInt32 peHead = br.ReadUInt32();
             if (peHead != 0x00004550) // "PE\0\0", little-endian
             {
                 throw new Exception("Can't find PE header");
             }
             return((MachineType)br.ReadUInt16());
         }
     }
 }
Exemplo n.º 2
0
 private static void ReadFileAttribues(FullPath path, out WIN32_FILE_ATTRIBUTE_DATA data, out int win32Error)
 {
     win32Error = 0;
     data       = default(WIN32_FILE_ATTRIBUTE_DATA);
     if (!NativeMethods.GetFileAttributesEx(path.Value, 0, ref data))
     {
         win32Error = Marshal.GetLastWin32Error();
     }
 }