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

Posted on Posted in Qt Tutorials, Tutorials

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() > 16 ) & 0xffff ) + “.” +
QString::number( ( lpBuffer->dwFileVersionMS) & 0xffff ) + “.” +
QString::number( ( lpBuffer->dwFileVersionLS >> 16 ) & 0xffff ) + “.” +
QString::number( ( lpBuffer->dwFileVersionLS) & 0xffff );
}





Leave a Reply

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