Use Windows API to dynamically obtain the version of a program file during running

Source: Internet
Author: User

The existence of the file version information simplifies the installation file of the application and enables the installer to analyze the current installation status of the file. Generally, the version information should include the file version number, file function description, and file author.

On the application layer, programmers can determine whether a file should be installed by judging the version information stored in the application file or the dynamic link library file, and determine the conflict of the current installation file. After the file has the version information attribute, the program we write can implement the following functions:

1. Avoid installing the same components of the old version on the new version;

2. In a multi-language system environment, the operating system determines the correct language to use when starting the program based on the language information provided in the file version information;

3. Avoid copying multiple files in different paths;

4. When the application is running, it can determine whether the file version is correct;

5. the version number of the executable file is displayed in the application's dialog box;

6. The online update program can determine whether a file is upgraded because the version is old.

Because of the inherent design defects of Windows systems, the FSO used does not support the file version information function. To manage the file version information, Microsoft uses APIs (Programmable interfaces) in the Platform SDK (platform software development kit) provides software developers with a method to manage version information. These APIs are getfileversioninfo, getfileversioninfo, verfindfile, verinstallfile, and verqueryvalue. They are stored in the dynamic link library file named version. dll under the Windows System directory.

In Windows, any file that can contain windows resources can contain version information, such as dynamic link library files, executable files, and font files. Version Information is encapsulated into a versioninfo structure resource and packaged into these files through the compiler.

This article tries to explain the usage of these Apis separately and introduce to readers how to dynamically obtain the version information of program files at runtime through programming in Delphi.

  I. usage of information functions of several key versions

In Windows, the language identifier is used to distinguish different natural languages. The language identifier is a 32-bit unsigned integer, and a value uniquely corresponds to a natural language. Therefore, in addition to the five functions listed above, we often use the verlanguagename function when programming. by calling this function, our program can freely convert the language identifiers represented by integers and the language names represented by strings.

The following is a complete list of functions used to process version information. Note: 1. in Windows NT 3.51 or earlier versions, the version information function cannot operate 16-bit Windows File images. 2.
In the Windows 95/98/ME/NT4/2000 operating system, these functions can operate both 16-bit and 32-bit file images. 3. in the Windows XP operating system, these functions can simultaneously operate on 16-bit, 32-bit, and 64-bit file images.


[Insert table 1]

In this article, we will focus on the usage of the four functions: getfileversioninfo, getfileversioninfosize, verstmagename, and verqueryvalue. In actual applications, the remaining two functions are rarely used, so they are not described as the focus.

1.1. getfileversioninfo Function

The getfileversioninfo function is used to obtain the version information contained in the specified file. Its Delphi function declaration is as follows:

Function getfileversioninfo (
Lptstrfilename: pchar; // file name
Dwhandle: DWORD; // ignore
Dwlen: DWORD; // buffer size
Lpdata: pointer //
Version Information Buffer
): Bool; stdcall;

Parameter description:

Lptstrfilename, a null end string, which specifies the name of the file from which you want to obtain the detailed version. If the file name does not contain the complete path, the function uses the default search order of the loadlibrary function to search. In Windows 95/98/Me, the path name cannot exceed 126 characters.

Dwhandle. this parameter is not used and will be ignored.

Dwlen: Call the getfileversioninfosize function to determine the size of the file version. Dwlen must be equal to or greater than this value. If the lpdate point to the buffer space is insufficient, the function will cut out the file version information based on the actual size.

Lpdata points to a buffer for saving the version information of the file returned after the function call.

If the function is successfully called, true is returned; otherwise, false is returned. You can use the getlasterror function to obtain extended error information.

Before calling the getfileversioninfo function, you must call getfileversionfilesize. To obtain useful information from the file version information, you must use the verqueryvalue function.

1.2. getfileversioninfosize Function

The getfileversioninfosize function is used to determine whether the operating system can obtain version information from a specified file. If version information exists, the size of the space occupied by the information in bytes is returned. Its Delphi function declaration is as follows:

Function getfileversioninfosize (
Lptstrfilename: pchar; // file name
VaR lpdwhandle: DWORD // set to zero
): DWORD; stdcall;

Parameter description:

Lptstrfilename, a null ending string that specifies the name of the file from which the version information is to be obtained.

Lpdwhandle, a pointer to a variable set to 0 by the function.

If the function is called successfully, it returns the size of the file version. Otherwise, it returns 0. You can use the getlasterror function to obtain the extended error information.

Call the getfileversioninfosize function before calling the getfileversioninfo function. The Return Value of the getfileversioninfosize function determines the size of the version information buffer used by the getfileversioninfo function.

1.3. verlanguagename Function

The verrentagename function is used to obtain the language description string associated with the specified binary Microsoft language identifier. Its Delphi function declaration is as follows:

Function verlanguagename (
Wlang: DWORD; // Microsoft language identifier
Szlang: pchar; // language description Buffer
Nsize: DWORD // buffer size
): DWORD; stdcall;

Parameter description:

Wlang, a language identifier, is a binary number. Specifies the binary language identifier. For a complete list of language identifiers, see the language identifiers section. For example, the description string associated with the language identifier 0x040a is "Castile Spanish ". If it is an unknown identifier, the szlang parameter points to a default string-"language neutral ".

Szlang. This parameter points to a buffer zone. This buffer is used to store strings identified by the wlang parameter, used to describe the language, and ended with null.

Nsize specifies the buffer size, in the unit of characters.

The function returns the size of the string stored in the buffer in characters. The return value does not contain the ending null character. If the description string is smaller than or equal to the buffer size, the entire description string is stored in the buffer; otherwise, the buffer retains the part whose front size is equal to the buffer size.

If an error occurs, the returned value is 0. Unknown Language identifiers do not produce errors.

Generally, the installer uses this function to translate the language identifier returned from the varquery function. When a language conflict occurs, the resulting text string can be used in a dialog box asking users how to handle the problem and prompting them to handle the problem.

1.4. verqueryvalue Function

The verqueryvalue function is used to obtain the specified version information from the specified version information resource. The most common logical process for obtaining version information is to call the getfileversioninfosize function first, then call the getfileversioninfo function, and then call the verqueryvalue function. Its Delphi function declaration is as follows:

Function verqueryvalue (
Pblock: pointer; // buffer for storing version Resources
Lpsubblock: pchar; // Expected Value
VaR lplpbuffer: pointer; // pointer to the buffer for storing version values
VaR pulen: uint //
Version Information Length
): Bool; stdcall;

Parameter description:

Pblock, a pointer to the buffer used to store version information resources. This version information resource is returned from the getfileversioninfo function.

Lpsubblock, pointing to a zero-ending string, specifying which version information value is obtained. This string must be separated by the backslash () character to form one of the following formats:

→ "", Specifying the root region. The function returns a version information resource pointing to the vs_fixedfieldfileinfo structure.

→ "Varfileinfotranslation" specifies a conversion array stored in the structure of variable information. The function returns a pointer to the array of language and code page identifiers. Applications can use these identifiers to access the string table structure in a specific language stored in version information resources.

→ "Stringfileinfolang-codepagestring-Name", which specifies the structure value stored in the string table of a specific language. Among them, the Lang-codePage writing format is: the conversion array language represented by DWORD and saved in the resource and the code page identifier pair, the string must be written as a hexadecimal string. String-name must be one of the pre-defined strings in the subsequent annotations. The function returns a related String Based on the specified language and code page.

Lplpbuffer, a pointer to a variable used to save the buffer to the requested version information. In short, it is a pointer to a pointer.

Pulen, pointing to a buffer that saves the length of version information.

If the specified version information structure exists and is valid, the function returns a non-zero value. If the length buffer address is equal to 0, the specified version information name is invalid.

In addition, when the specified name does not exist or the specified resource is invalid, the return value of the function is equal to 0.

The following lists the predefined standard string for unified character encoding of version information:

Comments, internalname, productname, companyName, legalcopyright, productversion, filedescription, legaltrademarks, privatebuild, fileversion, originalfilename, specialbuild

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.