从大文件中检索版本信息



我们有包含自定义版本信息的大型可执行文件(>1,2 GB)。我尝试使用"FileVersionInfo"类的"GetVersionInfo"从这些文件中检索此"Version Info"。由于某些原因,此方法不会返回Windows XP中较大文件(已测试为>1 GB)的版本信息。它确实适用于18 MB大小的文件。当我在Windows7(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();
    }
}

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

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