【问题标题】:Retrieve Version Info from large files从大文件中检索版本信息
【发布时间】:2012-01-11 15:43:28
【问题描述】:

我们有包含自定义版本信息的大型可执行文件 (>1,2 GB)。 我尝试使用“FileVersionInfo”类的“GetVersionInfo”从这些文件中检索此“版本信息”。 出于某种原因,此方法不会返回 Windows XP 中较大文件(已测试 > 1 GB)的版本信息。它确实适用于大小为 18 MB 的文件。 当我在 Windows 7(x86 和 X64)中尝试相同时,它适用于所有文件,甚至是更大的文件!

我使用 Reflector 工具查看 FileVersionInfo 类,并创建了一个小型控制台应用程序来检索“文件版本信息”-Size, 就像“GetVersionInfo”方法一样。在 Windows XP 中返回大小为 0(零),在 Windows 7 中为相同文件返回大小 1428。 XP 中的最后一个错误是 1812('指定的图像文件不包含资源部分')。

这在 Windows XP 中不起作用而在 Windows 7 中起作用的原因是什么? 是否有办法检索版本信息?

在我测试过的代码下方:

class Program
{
    [DllImport("version.dll", BestFitMapping = false, CharSet = CharSet.Auto, SetLastError = true)]
    public static extern int GetFileVersionInfoSize(string lptstrFilename, out int handle);

    static void Main(string[] args)
    {
        Console.Write("Path which contains the executables: ");
        string path = Console.ReadLine();

        foreach (string fileName in Directory.EnumerateFiles(path))
        {
            int num;
            int fileVersionInfoSize = GetFileVersionInfoSize(fileName, out num);
            int error = Marshal.GetLastWin32Error();

            Console.WriteLine("File Version Info Size: " + fileVersionInfoSize);

            if (error != 0)
            {
                Console.WriteLine("Last Error: " + error);
            }
        }

        Console.ReadKey();
    }
}

【问题讨论】:

  • 确认一下,这个 18mb 的文件可以在 XP 上运行吗?
  • 是的,18 mb 可以在 XP 上运行!

标签: c# .net


【解决方案1】:

加载程序可能无法将整个文件映射到其地址空间。请随意阅读 PE 文件格式以查找版本资源。

但是,你在用这么大的 PE 图像做什么?如果是自定义资源,最好将它们附加到 .exe 中,这样加载器只需映射一点点,然后直接访问它们。这需要您知道自己的大小(偏移 124 处有 4 个字节可以安全地覆盖,因为它们是在“此程序无法在 MS-DOS 模式下运行。”错误消息存根之后的填充)。

【讨论】:

    猜你喜欢
    • 2021-02-09
    • 2018-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-16
    • 1970-01-01
    • 2015-11-22
    • 1970-01-01
    相关资源
    最近更新 更多