4

I've an exe that will need to retrieve version infomation from a specific dll (ex : FileDescription). My codes already called the GetFileVersionInfoSize and GetFileVersionInfo. But I'm not sure how to apply the VerQueryValue, even after going through http://msdn.microsoft.com/en-us/library/ms647464(v=vs.85) and other examples.

Can someone explain/shed some light on how to apply VerQueryValue and its usage? Thanks.

4
  • What part of the example do you not understand? The sample code works just fine to get the FileDescription, which sounds like exactly what you're trying to do. So what isn't working for you?
    – abarnert
    Commented Jun 28, 2012 at 4:48
  • For the second parameter ( \, \VarFileInfo\Translation, \StringFileInfo\lang-codepage\string-name ), which form should be used?
    – eastwest
    Commented Jun 28, 2012 at 5:37
  • The first form should be used when you want the VS_FIXEDFILEINFO (which has things like the major version number and the type flags). The second form should be used when you want the translation array, the list of translations you can ask for strings in. The third form should be used when you want one of the actual strings. As you can see from the example, the usual procedure is to first use the VarFileInfo form to get the translation array, then use the StringFileInfo form to get the particular string you want in the appropriate language.
    – abarnert
    Commented Jun 28, 2012 at 5:42
  • I use the third form in my test program, and I managed to get the desired result. Since I just want to get the FileDescription only, I think it should be ok (for now). Thank you for your explanation, abarnet.
    – eastwest
    Commented Jun 28, 2012 at 7:00

2 Answers 2

7

To get the FileDescription via VerQueryValue, just copy and paste the example code from the VerQueryValue documentation, and modify it as appropriate.

The basic idea behind that example code is:

  • Use the second form (\VarFileInfo\Translation) to get the list of translations.
  • Then use the third form (\StringFileInfo\lang-codepage\string-name) to get the string(s).

(The first form () is just for the VS_FIXEDFILEINFO, a set of numerical values for parts of the version number, the flags, etc.)

The example code gets the FileDescription for each language. If you know you only have one language (e.g., because you're looking at your own app, and it isn't translated), you can skip the loop and just return the first one. For more general use, you want to pick the best match for the user's language and return that one.

5
  • Thanks abarnert. For my case, the dll is my own file(english version only), so there is no need for me to use the second form, and I can straight away call VerQueryValue with third form, right? Is this the right way to implement such code?
    – eastwest
    Commented Jun 29, 2012 at 1:09
  • I found this link, but not sure whether it's relevent to my case or not :blogs.msdn.com/b/oldnewthing/archive/2012/03/15/10283336.aspx
    – eastwest
    Commented Jun 29, 2012 at 2:03
  • Yes, if you know the hex "lang" and "codepage" for your language (I believe it's "040904b0", but don't quote me on that), and you're sure that this language is there, you can just go straight to the third form. The blog post you found is only indirectly relevant; it's about the auto-localization that Vista does if you use the new GetFileVersionInfoEx API and/or use the new-style .NET internationalization. You're doing it the classic way.
    – abarnert
    Commented Jun 29, 2012 at 17:55
  • Their code sample no longer builds, at least not with VS2015 and VS2017... Commented Mar 15, 2017 at 18:52
  • The documentation for VerQueryValue doesn't seem to work, half the code is missing even Commented Nov 11, 2019 at 21:04
1

This is a working example, after many try and errors. I'm using Borland C++, so minor details may need to be changed for incompabible environments.

#include <Windows.h>

std::string GetAppVersion()
{
    DWORD dwHandle;
    TCHAR fileName[MAX_PATH];

    GetModuleFileName(NULL, fileName, MAX_PATH);
    DWORD dwSize = GetFileVersionInfoSize(fileName, &dwHandle );
    TCHAR buffer[dwSize];

    VS_FIXEDFILEINFO* pvFileInfo = NULL;
    UINT fiLen = 0;

    if ((dwSize > 0) && GetFileVersionInfo(fileName, dwHandle, dwSize, &buffer))
    {
        VerQueryValue(&buffer, L"\\", (LPVOID*)&pvFileInfo, &fiLen);
    }

    if (fiLen > 0)
    {
        char buf[25];
        int len = sprintf(buf, "%hu.%hu.%hu.%hu",
            HIWORD(pvFileInfo->dwFileVersionMS),
            LOWORD(pvFileInfo->dwFileVersionMS),
            HIWORD(pvFileInfo->dwFileVersionLS),
            LOWORD(pvFileInfo->dwFileVersionLS)
        );

        return std::string(buf, len);
    }
    else
    {
        return std::string("(Unknown)");
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.