c# - 为什么我没有得到 "PE\0\0"?

标签 c# .net portable-executable coff

来自 PE 规范:

At location 0x3c, the stub has the file offset to the PE signature. This information enables Windows to properly execute the image file, even though it has an MS DOS stub. This file offset is placed at location 0x3c during linking.

2.2. Signature (Image Only)
After the MS DOS stub, at the file offset specified at offset 0x3c, is a 4-byte signature that identifies the file as a PE format image file. This signature is “PE\0\0” (the letters “P” and “E” followed by two null bytes).

我尝试读取这些字节:

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字节(第一个和第二个不是 PE 字符)...为什么我得到这样的结果?

最佳答案

PE header 本身并不从偏移量 0x3C 开始 - 相反,那里有一个指向 PE header 开始位置的指针(从文件开头开始的 32 位文件偏移量)。

关于c# - 为什么我没有得到 "PE\0\0"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31728031/

相关文章:

c# - 如何计算特定 block 的 AES GCM IV

c# - 搜索全局地址列表/书籍

c# - 设置/更新 aspxauth 和 asp.net_sessionid cookie 的过期时间

c++ - PE中的 "Isolated Image"属性是什么?

windows - 为什么段虚拟地址需要连续?

c# - 按钮 IsEnabled 触发器不起作用

c# - 从包含一系列基于文件名进行版本控制的文件的文件夹中查找文件

c# - 如何将 C++ 代码包装到具有大量 float*(代表点)的 CLI 中?

c# - .net 4 string[]参数默认值设置

c++ - 将字符数组转换为整数值