为什么我没有得到“PE \ 0 \ 0”?

时间:2015-07-30 15:30:44

标签: c# .net portable-executable coff

来自PE规范:

  

在位置0x3c,存根具有PE签名的文件偏移量。   此信息使Windows能够正确执行图像文件,   即使它有一个MS DOS存根。此文件偏移位于   链接期间的位置0x3c

     

2.2。签名(仅图像)
在MS DOS存根之后,在偏移0x3c指定的文件偏移处,是一个4字节的签名,用于标识   文件作为PE格式的图像文件。这个签名是“PE \ 0 \ 0”(   字母“P”和“E”后跟两个空字节)。

我尝试读取这些字节:

using System;
using System.IO;

class Program {
  const String fileName = @".\some_application.exe";
  const Int64 peMarkerPosition = 0x3c;

  static void Main(string[] args) {
    using (FileStream fs = new FileStream(fileName, FileMode.Open,
      FileAccess.Read)) {
      Byte[] marker = new Byte[4];
      fs.Position = peMarkerPosition;
      fs.Read(marker, 0, marker.Length);
      // Now I expect 'marker'has such bytes: "PE\0\0".
      fs.Close();

      foreach (Byte b in marker) {
        Console.Write(Convert.ToChar(b)); // But I see other values...
      }

      Console.WriteLine("\nPress any key for exit...");
      Console.ReadKey();
    }
  }
}

marker变量有0x080x010x00x0x00个字节(第一个和第二个不是P和{{ 1}} chars)...为什么我得到这样的结果?

1 个答案:

答案 0 :(得分:4)

PE头本身不会在偏移0x3C处开始 - 而是在那里有一个指针(从文件开头偏移32位文件)到PE头开始的位置。