File version

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

Does anyone know how to check a version of a file in C++? Any WIN32 API
that does this?

Please advice, thanks!
-P
 
Hi Paul!
Does anyone know how to check a version of a file in C++? Any WIN32 API
that does this?

Here is susch a function:

<code>
// Returns
// - a negative value if an error occured (more information can be
retrived by calling "GetLastError")
// - "0" if the version is the same
// - "1" if the file-version is lower than the provided version
// - "2" if the file-version is higher than the provided version
int CompareFileVersion(LPCTSTR szFileName, WORD v1, WORD v2, WORD v3,
WORD v4)
{
LPVOID vData = NULL;
VS_FIXEDFILEINFO *fInfo = NULL;
DWORD dwHandle;
ULONGLONG ullVersion;
ullVersion = v4;
ullVersion |= (((ULONGLONG) v3) << 16);
ullVersion |= (((ULONGLONG) v2) << 32);
ullVersion |= (((ULONGLONG) v1) << 48);

DWORD dwSize = GetFileVersionInfoSize(szFileName, &dwHandle);
if (dwSize <= 0)
return -1;

// try to allocate memory
ULONGLONG fileVersion = 0;
vData = malloc(dwSize);
if (vData == NULL)
{
SetLastError(ERROR_OUTOFMEMORY);
return -1;
}

// try to the the version-data
if (GetFileVersionInfo(szFileName, dwHandle, dwSize, vData) == 0)
{
DWORD gle = GetLastError();
free(vData);
SetLastError(gle);
return -1;
}

// try to query the root-version-info
UINT len;
if (VerQueryValue(vData, _T("\\"), (LPVOID*) &fInfo, &len) == 0)
{
DWORD gle = GetLastError();
free(vData);
SetLastError(gle);
return -1;
}

// build the file-version
fileVersion = ((ULONGLONG)fInfo->dwFileVersionLS) +
((ULONGLONG)fInfo->dwFileVersionMS << 32);

free(vData);

// compare if the installed version is newer than this hotfix
if (fileVersion > ullVersion)
{
// The file is newer than the provided version
return 2;
}
else if (fileVersion < ullVersion)
{
// The file is older than the provided version
return 1;
}
return 0;
}


int _tmain()
{
CompareFileVersion(_T("c:\\DOTNETFX_v11.EXE"), 1, 1, 4322, 572);
}
</code>


--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
Back
Top