|
|
There are many cases, when version information extracted from a
file's version
resource could be useful. One example is to test for needed DLL
versions. Another one is to simply use the version information from an
application to display it in the
about-dialog of the application. If your application contains more than
one language
resources, changing the version number becomes a boring task if you
have to change the version label in each
about-dialog. Using CFileVersionInfo
you could simply
read the version dynamically during runtime and insert it into the
dialog. So you only will have to change the version
resource when the version number of your application has changed.
Using the class CFileVersionInfo
is very easy. Simply construct an object, call the Create()
method to initialize the object with a file's version information and
call the attribute operations to retrieve the required information.
The following short example writes the application's version information to stdout
:
CFileVersionInfo fvi; // Retrieve version information for this module if (fvi.Create()) { // Print version information cout << _T("Product: ") <<fvi.GetProductName() << _T("\n") << _T("Company: ") << fvi.GetCompanyName() << _T("\n") << _T("File Version Label: ") << fvi.GetFileVersion() << _T("\n") << _T("File Version Number: ") << fvi.GetFileVersion(3) << _T('.') << fvi.GetFileVersion(2) << _T('.') << fvi.GetFileVersion(1) << _T('.') << fvi.GetFileVersion(0); }
Most of the methods are self-explanatory. Here are the references for the methods expected parameters:
Initializes the object.
BOOL Create(HMODULE hModule = NULL); BOOL Create(LPCTSTR lpszFileName);
hModule
lpszFileName
Returns TRUE
if the version information has been extracted successfully and FALSE
otherwise.
Retrieves binary file version.
WORD GetFileVersion(int nIndex) const;
nIndex
Returns the binary product version.
WORD GetProductVersion(int nIndex) const;
nIndex
This should run on Windows 95 or later and Windows NT 3.1 or later.
You will need to link your software with version.lib.
Though I haven't tested it, there is no reason, why this should not work with Unicode.
The class simply uses the following Windows' "File Installation Library Functions" for retrieving the version information:
GetFileVersionInfo()
GetFileVersioninfoSize()
VerQueryValue
Retrieving the strings of the version information is a little bit tricky. Different from other resource's, where Windows chooses the language that best fits the user's preferences, for the version resource the developer is responsible for choosing the best matching language.
The datablock returned by GetFileVersionInfo()
(lpData
)
contains a list of all language-codepage-combinations included in the
datablock. One can receive this list using the following sequence:
LPVOID lpInfo; UINT unInfoLen; VerQueryValue(lpData, _T("\\"), &lpInfo, &unInfoLen); VerQueryValue(lpData, _T("\\VarFileInfo\\Translation"), &lpInfo, &unInfoLen);
CFileVersionInfo
uses the following tests to get the best matching strings:
LANG_NEUTRAL
) is available.LANG_ENGLISH
) is available.The first matching language is used. The users's main and sub language are retrieved by using the GetUserDefaultLangID()
function.
19 Jun 2002 - Initial Revision
19 Jun 2002 - Reformatted and rewrote some sections
![]() | Sven
Wiegand (1976), IT professional living in Berlin (Germany), develops
open source software in his free time. His most successfull project is
the LaTeX IDE TeXnicCenter which is distributed under the terms of the GNU-GPL and has more than 100,000 users all about the world. "The picture shows me with my racing bike on the top of the Roque de los Muchachos (2426m) - the highest point of the canarian island La Palma." Click here to view Sven Wiegand's online profile. |
![]() |
15 comments have been posted for this article. Visit http://www.codeproject.com/file/fileversioninfo.asp to post and view comments on this article.
All Topics, MFC / C++ >> Files and Folders >> General
Updated: 19 Jun 2002 |
Article content copyright Sven Wiegand, 2001 everything else Copyright © CodeProject, 1999-2004. |