How to get version number of an EXE file in Qt (Using Win32 API)

I use the function below to get the version of my Windows applications and show it on the title bar or about page programmatically. It uses Win32 API so it is not cross-platform.

You also need to add the following line to your project’s PRO file:

LIBS += -lVersion





You also need to include Windows.h by adding the following line to your header file:

#include “Windows.h”

An below is the magic:

QString getVersionString(QString fName)
{
// first of all, GetFileVersionInfoSize
DWORD dwHandle;
DWORD dwLen = GetFileVersionInfoSize(fName.toStdWString().c_str(), &dwHandle);

// GetFileVersionInfo
LPVOID lpData = new BYTE[dwLen];
if(!GetFileVersionInfo(fName.toStdWString().c_str(), dwHandle, dwLen, lpData))
{
qDebug() << “error in GetFileVersionInfo”;
delete[] lpData;
return “”;
}

// VerQueryValue
VS_FIXEDFILEINFO *lpBuffer = NULL;
UINT uLen;
if(!VerQueryValue(lpData,
QString(“\”).toStdWString().c_str(),
(LPVOID*)&lpBuffer,
&uLen))
{
qDebug() <<
QString::number(( lpBuffer->dwFileVersionMS >> 16 ) & 0xffff ) + “.” +
QString::number( ( lpBuffer->dwFileVersionMS) & 0xffff ) + “.” +
QString::number( ( lpBuffer->dwFileVersionLS >> 16 ) & 0xffff ) + “.” +
QString::number( ( lpBuffer->dwFileVersionLS) & 0xffff );
}





4 Replies to “How to get version number of an EXE file in Qt (Using Win32 API)”

  1. Why at the end of function, when getting “lpBuffer->dwFileVersionLS” the application close with out printing anything

      1. I’ve followed all step, the problem is at the end of function when
        qDebug() > 16 ) & 0xffff ) + “.” +
        QString::number( ( lpBuffer->dwFileVersionMS) & 0xffff ) + “.” +
        QString::number( ( lpBuffer->dwFileVersionLS >> 16 ) & 0xffff ) + “.” +
        QString::number( ( lpBuffer->dwFileVersionLS) & 0xffff );
        the application ends. Is the script written all right. Why qDebug() > instead of qDebug() <<

        1. You are absolutely right Hassan,
          It was a typo, or perhaps something caused by pasting a code block in HTML.
          Updated the page, thanks and please check again!

Leave a Reply

Your email address will not be published. Required fields are marked *