Get EXE version using Windows API in Qt

This function can be used to get an EXE file version. I use this class in almost all of my programs to add EXE version to Main window title.

Note 1: You need to add Version library to your project by adding the following line to your project’s PRO file:

LIBS += -lVersion

Note 2: You need to add “windows.h” to your #include list:

#include "Windows.h"


And here is the function itself:

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() << "error in VerQueryValue";
		delete[] lpData;
		return "";
	}
	else
	{
		return QString::number((lpBuffer->dwFileVersionMS >> 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 *

This site uses Akismet to reduce spam. Learn how your comment data is processed.