Windows API tour-getfileversioninfo function and verqueryvalue Function

Source: Internet
Author: User

The vs_fixedfileinfo structure contains the file version information:

Typedef struct tagvs_fixedfileinfo {<br/> DWORD dwsignature; // The value is 0xfeef04bd <br/> DWORD dwstrucversion; // The 32-bit binary version of the structure, the high 16-bit version is the primary version, and the low 16-bit is the minor version <br/> DWORD dwfileversionms; // The High 32 bits of the binary version of the file <br/> DWORD dwfileversionls; // 32 bits of the binary version of the file <br/> DWORD dwproductversionms; // The binary version of the product that publishes the file is 32 bits <br/> DWORD dwproductversionls; // The binary version of the product that publishes this file is 32 bits <br/> DWORD dwfileflagsmask; // bit mask, which indicates the valid bit of dwfileflags <br/> DWORD dwfileflags; // vs_ff_debug --- this file contains debugging information or the version structure of the <br/> // vs_ff_infoinferred compiled by the debug version --- file is dynamically created. <br/> // Therefore, some members in the structure are empty or incorrect <br/> // vs_ff_patched --- the file has been modified <br/> // vs_ff_prerelease --- the file is a development version, not commercial release version <br/> // vs_ff_privatebuild --- this file is not built by the standard release Step <br/> // vs_ff_specialbuild --- this file is built by the standard release Step, <br/> // file variants with the same version number <br/> DWORD dwfileos; // operating system designed for use in this file <br/> DWORD dwfiletype; // file type: vft_app --- the file contains an application <br/> vft_dll --- the file contains a DLL <br/> vft_drv --- the file contains a device driver <br/> vft_font --- the file contains a font file <br/> vft_static_lib --- the file contains a static Link Library <br/> vft_unknown --- the file type is unknown <br/> vft_vxd --- the file contains a virtual device <br/> DWORD dwfilesubtype; // file sub-type, determined by dwfiletype <br/> DWORD dwfiledatems; // The 32 bits of the binary file creation date and time stamp <br/> DWORD dwfiledatels; // 32 bits of the binary file creation date and timestamp <br/>} vs_fixedfileinfo; 

The getfileversioninfosize function is used to determine whether the system can retrieve the version information of a specified file. If yes, the function returns the size of the version information in bytes:

DWORD winapi getfileversioninfosize (<br/> _ in lpctstr lptstrfilename, // specify the file name <br/> _ out_opt lpdword lpdwhandle // pointer to a variable, this function sets this variable to 0 <br/> ); 

The getfileversioninfo function is used to obtain the version information of a specified file:

Bool winapi getfileversioninfo (<br/> _ in lpctstr lptstrfilename, // file name <br/> _ reserved DWORD dwhandle, // reserved value <br/> _ in DWORD dwlen, // lpdata points to the buffer size, use the getfileversioninfosize function to obtain the <br/> _ out lpvoid lpdata // pointer to the buffer where the file version information is stored <br/> );

Verqueryvalue is used to obtain version information from the specified version information source. Before calling this function, you must call the getfileversioninfosize and getfileversioninfo functions in sequence:

Bool winapi verqueryvalue (<br/> _ in lpcvoid pblock, // version information source obtained by the function getfileversioninfo <br/> _ in lpctstr lpsubblock, // "/" indicates that the function obtains the vs_fixedfileinfo structure information. <br/> // "/varfileinfo/translation" indicates that the function obtains the translation table of the file. <br/> // "/stringfileinfo/lang-codePage/string-name" indicates that the function obtains the string information of the file. <br/> _ out lpvoid * lplpbuffer, // returns the address directed to pblock. When pblock is released, this parameter is also released <br/> _ out puint pulen // the size of the data in bytes pointed to by lplpbuffer <br/> ); 

The string-name parameter in the value of the lpsubblock must be one of the predefined strings in the following system:

The following code example encapsulates a file version information class. You can use the function described above to conveniently obtain the file version information. The header file is defined as fileversion. h:

// Fileversion. h: interface for the cfileversion class. <br/> // by Manuel Laflamme <br/> /////////////////////////// //////////////////////////////////////// /// <br/> # ifndef _ fileversion_h _ <br/> # DEFINE _ fileversion_h _ <br/> # If _ msc_ver> = 1000 <br/> # pragma once <br/> # endif // _ msc_ver> = 1000 <br/> class cfileversion <br/>{< br/> // construction <br/> public: <br/> cfileversion (); <br/> // Operations <Br/> Public: <br/> bool open (lpctstr lpszmodulename); <br/> void close (); <br/> cstring queryvalue (lpctstr lpszvaluename, DWORD dwlangcharset = 0); <br/> cstring getfiledescription () {return queryvalue (_ T ("filedescription") ;}; <br/> cstring getfileversion () {return queryvalue (_ T ("fileversion") ;}; <br/> cstring getinternalname () {return queryvalue (_ T ("internalname "));}; <br/> cstring getcompan Yname () {return queryvalue (_ T ("companyName") ;}; <br/> cstring getlegalcopyright () {return queryvalue (_ T ("legalcopyright") ;}; <br/> cstring getoriginalfilename () {return queryvalue (_ T ("originalfilename "));}; <br/> cstring getproductname () {return queryvalue (_ T ("productname") ;}; <br/> cstring getproductversion () {return queryvalue (_ T ("productversion") ;}; <br/> bool getfixedinfo (vs_fixedfilei Nfo & vsffi); <br/> cstring getfixedfileversion (); <br/> cstring getfixedproductversion (); <br/> // attributes <br/> protected: <br/> lpbyte m_lpversiondata; <br/> DWORD m_dwlangcharset; <br/> // implementation <br/> Public: <br/> ~ Cfileversion (); <br/>}; <br/> # endif/_ fileversion_h _ 

The implementation of the header file is as follows: fileversion. cpp:

// Fileversion. CPP: Implementation of the cfileversion class. <br/> // by Manuel Laflamme <br/> /////////////////////////// //////////////////////////////////////// /// <br/> # include "fileversion. H "<br/> # pragma comment (Lib," version ") <br/> # ifdef _ debug <br/> # UNDEF this_file <br/> static char this_file [] =__ file __; <br/> # define new debug_new <br/> # endif <br/> ////////////////////// /////////////////////// /// // <Br/> cfileversion: cfileversion () <br/>{< br/> m_lpversiondata = NULL; <br/> m_dwlangcharset = 0; <br/>}< br/> cfileversion ::~ Cfileversion () <br/>{< br/> close (); <br/>}< br/> void cfileversion: Close () <br/>{< br/> Delete [] m_lpversiondata; <br/> m_lpversiondata = NULL; <br/> m_dwlangcharset = 0; <br/>}< br/> bool cfileversion: open (lpctstr lpszmodulename) <br/>{< br/> assert (_ tcslen (lpszmodulename)> 0 ); <br/> assert (m_lpversiondata = NULL); <br/> // get the version information size for allocate the buffer <br/> DWORD DW Handle; <br/> DWORD dwdatasize =: getfileversioninfosize (lptstr) lpszmodulename, & dwhandle); <br/> If (dwdatasize = 0) <br/> return false; <br/> // allocate buffer and retrieve Version Information <br/> m_lpversiondata = new byte [dwdatasize]; <br/> If (!: Getfileversioninfo (lptstr) lpszmodulename, dwhandle, dwdatasize, <br/> (void **) m_lpversiondata) <br/>{< br/> close (); <br/> return false; <br/>}< br/> // retrieve the first language and character-Set Identifier <br/> uint nquerysize; <br/> DWORD * ptranstable; <br/> If (!: Verqueryvalue (m_lpversiondata, _ T ("// varfileinfo // translation"), <br/> (void **) & ptranstable, & nquerysize )) <br/>{< br/> close (); <br/> return false; <br/>}< br/> // swap the words to have Lang-charset in the correct format <br/> m_dwlangcharset = makelong (hiword (ptranstable [0]), loword (ptranstable [0]); <br/> return true; <br/>}< br/> cstring cfileversion: queryvalue (lpctstr lpszvaluename, <br /> DWORD dwlangcharset/* = 0 */) <br/> {<br/> // must call open () First <br/> assert (m_lpversiondata! = NULL); <br/> If (m_lpversiondata = NULL) <br/> return (cstring) _ T (""); <br/> // if no Lang-charset specified use default <br/> If (dwlangcharset = 0) <br/> dwlangcharset = m_dwlangcharset; <br/> // query version information value <br/> uint nquerysize; <br/> lpvoid lpdata; <br/> cstring strvalue, strblockname; <br/> strblockname. format (_ T ("// stringfileinfo // % 08lx // % s"), <br/> dwlangcharset, Lpszvaluename); <br/> If (: verqueryvalue (void **) m_lpversiondata, strblockname. getbuffer (0), <br/> & lpdata, & nquerysize) <br/> strvalue = (lpctstr) lpdata; <br/> strblockname. releasebuffer (); <br/> return strvalue; <br/>}< br/> bool cfileversion: getfixedinfo (vs_fixedfileinfo & vsffi) <br/> {<br/> // must call open () First <br/> assert (m_lpversiondata! = NULL); <br/> If (m_lpversiondata = NULL) <br/> return false; <br/> uint nquerysize; <br/> vs_fixedfileinfo * pvsffi; <br/> If (: verqueryvalue (void **) m_lpversiondata, _ T ("//"), <br/> (void **) & pvsffi, & nquerysize) <br/>{< br/> vsffi = * pvsffi; <br/> return true; <br/>}< br/> return false; <br/>}< br/> cstring cfileversion: getfixedfileversion () <br/>{< br/> cstring strversion; <br/> vs_fixedfileinfo vsffi; <br/> If (getfixedinfo (vsffi) <br/>{< br/> strversion. format (_ T ("% u, % u"), hiword (vsffi. dwfileversionms), <br/> loword (vsffi. dwfileversionms), <br/> hiword (vsffi. dwfileversionls), <br/> loword (vsffi. dwfileversionls); <br/>}< br/> return strversion; <br/>}< br/> cstring cfileversion: getfixedproductversion () <br/>{< br/> cstring strversion; <br/> vs_fixedfileinfo vsffi; <br/> If (getfixedinfo (vsffi )) <br/>{< br/> strversion. format (_ T ("% u, % u"), hiword (vsffi. dwproductversionms), <br/> loword (vsffi. dwproductversionms), <br/> hiword (vsffi. dwproductversionls), <br/> loword (vsffi. dwproductversionls); <br/>}< br/> return strversion; <br/>}

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.