GetFileVersion

Gets file (application, DLL, …) version information.

Um verschiedene Versionen einer Programmdatei (Anwendung oder Bibliothek) auseinanderzuhalten, werden in diesen Dateien Versionsangaben gespeichert. Die können im Explorer auch in den Dateieigenschaften eingesehen werden. Um in einem Programm auf diese Daten zuzugreifen, gibt es im Windows-API die GetFileVersionInfo-Funktion. Der folgende Code zeigt, wie man sie verwendet, und nimmt dem C++-Programmierer einen Großteil der üblichen Schreibarbeit ab.

Note on the code quality: The content on this page is possibly a bit outdated. I’m not using C++ anymore for quite some time, but I would like to keep the existing and once useful functions available.

Download

GetFileVersion.cpp1.4 KiBQuelltext der GetFileVersion-Funktion

Contents of the file GetFileVersion.cpp:

// Gets file (application, DLL, …) version information.
//
// Dependencies: CString
//
// Web: http://dev.unclassified.de/source/getfileversion
//
CString GetFileVersionInfo(CString strFile, CString strProperty)
{
    int rc;
    UINT nLen;
    DWORD nSize;
    DWORD dwHandle = 0;
    CString strBuffer;
    CString strValue;
    CString strBlock;
    void *lpPropertyBuffer;

    struct LANGANDCODEPAGE
    {
        WORD wLanguage;
        WORD wCodePage;
    } *lpTranslate;

    nSize = GetFileVersionInfoSize(strFile.GetBuffer(), &dwHandle);
    ::GetFileVersionInfo(strFile.GetBuffer(), 0, nSize, strBuffer.GetBuffer(nSize));

    // Read the list of languages and code pages.
    if (VerQueryValue(strBuffer.GetBuffer(),
                      "\\VarFileInfo\\Translation",
                      (LPVOID *) &lpTranslate,
                      &nLen))
    {
        strBlock.Format("\\StringFileInfo\\%04x%04x\\%s",
            lpTranslate->wLanguage,
            lpTranslate->wCodePage,
            strProperty);
        rc = VerQueryValue(strBuffer.GetBuffer(),
                           strBlock.GetBuffer(nSize),
                           &lpPropertyBuffer,
                           &nLen);
        if (rc != 0 && nLen > 0)
        {
            strncpy(strValue.GetBuffer(nLen + 1), (char *) lpPropertyBuffer, nLen);
            strValue.ReleaseBuffer(nLen);
        }
    }

    return strValue;
}

// Gets the version of the shell32 system library.
//
CString GetShell32Version()
{
    return GetFileVersionInfo("shell32.dll", "ProductVersion");
}

Licence and terms of use

This software is freely available as source code and compiled version, without restrictions (“public domain”). There is no warranty, not even for merchantability or fitness for a particular purpose. I am not liable for any damage caused through appropriate or inappropriate use.

Statistic data

  • Created on 2007-04-02.