RBDev
RBDev

Reputation: 761

Retrieve Version Info from large files

We have large executable files (>1,2 GB) which contain custom Version Info. I try to retrieve this 'Version Info' from these files by using the 'GetVersionInfo' of the 'FileVersionInfo' Class. For some reason this method doesn't return the version information for larger files (Tested with > 1 GB) in Windows XP. It did work for a file with a size of 18 MB. When I try the same in Windows 7 (x86 and X64) it works for all files, even the larger onces!

I used a Reflector tool to take a look into the FileVersionInfo class and I've created a small console app to retrieve the 'File Version Info'-Size, just as the 'GetVersionInfo' method does. A size of 0 (zero) is returned in Windows XP and in Windows 7 a size of 1428 is returned for the same files. The Last Error in XP is 1812 ('The specified image file did not contain a resource section').

What's the reason why this doesn't work in Windows XP and does work in Windows 7? Is there a work around to retrieve the Version Info?

Below the code I've tested with:

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();
    }
}

Upvotes: 7

Views: 606

Answers (1)

Joshua
Joshua

Reputation: 43280

The loader probably failed to map the entire file into its address space. Feel free to read up on the PE file format to find the version resource.

However, what are you doing with such a large PE Image? If its custom resources, it works better to append them to the .exe so the loader only has to map a little bit, then access them directly. This requires you to know your size (there are 4 bytes at offset 124 that can be overwritten safely as they are pad after the "This Program cannot be run in MS-DOS mode." error message stub).

Upvotes: 2

Related Questions