Get VersionInfo from resource file

.

CVersionInfo is a tiny class, which wraps the stuff needed to get the version info from a resource file. It handles both, the fixed version information and the language dependent string version information of the resource file. It contains three CString members which hold the retrieved information:


    m_strFixedFileVersion:  the fixed file version info (language independent)
    m_strFixedProductVersion: the fixed product version info (language independent)
    m_strVersionInfo:   the desired version info string (language dependent)

To get the language dependent string version info you need to set the following parameters as follows:

strLangID:


strLangID is the language ID for which the version info is desired
040904E4 means e.g.:
04------ = SUBLANG_ENGLISH_USA
--09---- = LANG_ENGLISH
----04E4 = 1252 = Codepage for Windows:Multilingual

strInfoType:


strInfoType is the desired version entry, eg.:"ProductName", or "ProductVersion"

hModule:


hModule ist the Instance handle of the module for which the version info should be retrieved
with EXE's: Get hModule calling AfxGetInstanceHandle.
with DLL's: Get hModule calling ::GetModuleHandle ("DLLName"), where "DLLName" is the Name of the module

Important!

CVersionInfo requires to be linked with VERSION.LIB!

Here's the code:
 
// VersionInfo.h 
 
#ifndef VERSIONINFO_H
#define VERSIONINFO_H 
#include "stdafx.h"
class CVersionInfo
{
public:
	CString m_strVersionInfo;
	CString m_strFixedFileVersion;
	CString m_strFixedProductVersion;
	CVersionInfo ();
	~CVersionInfo ();
	CVersionInfo (HMODULE hModule, LPCTSTR strLangID = NULL, LPCTSTR strInfoType = NULL);
	CVersionInfo (LPTSTR szFilename, LPCTSTR strLangID = NULL, LPCTSTR strInfoType = NULL);
	void GetVersionInfo (LPTSTR szFilename, LPCTSTR strLangID = NULL, LPCTSTR strInfoType = NULL);
	void GetVersionInfo (HMODULE hModule, LPCTSTR strLangID = NULL, LPCTSTR strInfoType = NULL);
}; 

#endif // VERSIONINFO_H
 
// VersionInfo.cpp
 
//**********************************************************************************************
// CVersionInfo (c)1997 Roberto Rocco 
//----------------------------------------------------------------------------------------------
// CVersionInfo is a tiny class, which wraps the stuff needed to get the version info from a
// resource file.
// It handles both, the fixed version information and the language dependent string version
// information of the resource file.
//
// It contains three CString members which hold the retrieved information:
// m_strFixedFileVersion:  the fixed file version info (language independent)
// m_strFixedProductVersion: the fixed product version info (language independent)
// m_strVersionInfo:   the desired version info string (language dependent)
//
// CVersionInfo requires to be linked with VERSION.LIB!
//
//********************************************************************************************** 
#include "VersionInfo.h"
// Standard-Constructor. Does nothing particular
CVersionInfo::CVersionInfo ()
{
} 

// Standard-Destructor. Does nothing particular
CVersionInfo::~CVersionInfo ()
{
} 

// Constructor with hModule, strLangId and strInfoType as parameter
CVersionInfo::CVersionInfo (HMODULE hModule, LPCTSTR strLangID/*=NULL*, LPCTSTR strInfoType/*=NULL*)
{
	GetVersionInfo (hModule, strLangID, strInfoType);
}
 
// Constructor with szFilename, strLangId and strInfoType as parameter
CVersionInfo::CVersionInfo (LPTSTR szFilename, LPCTSTR strLangID/*=NULL*, LPCTSTR strInfoType/*=NULL*)
{
	GetVersionInfo (szFilename, strLangID, strInfoType);
}

void CVersionInfo::GetVersionInfo (HMODULE hModule, LPCTSTR strLangID/*=NULL*, LPCTSTR strInfoType/*=NULL*)
{
	TCHAR szExeName[MAX_PATH];  
	if(hModule == NULL) 
		  return;  
	GetModuleFileName(hModule, szExeName, sizeof (szExeName)); 
	GetVersionInfo(szExeName, strLangID, strInfoType); 
} 

//**********************************************************************************************
// GetVersionInfo (requires VERSION.LIB!!!)
//----------------------------------------------------------------------------------------------
// Retrieves the desired version entry from the VERSIONINFO structure of the resource file.
//
// strLangID is the language ID for which the version info is desired
// 040904E4 means e.g.:
// 04------        = SUBLANG_ENGLISH_USA
// --09----        = LANG_ENGLISH
// ----04E4 = 1252 = Codepage for Windows:Multilingual
//
// strInfoType  is the desired version entry, eg.:"ProductName", or "ProductVersion"
//
// hModule ist the Instance handle of the module for which the version info should be retrieved
// with EXE's: Get hModule calling AfxGetInstanceHandle.
// with DLL's: Get hModule calling  ::GetModuleHandle ("DLLName"), where "DLLName" is the Name of the module
//
// the version information is stored in the member variables:
// m_strFixedFileVersion, m_strFixedProductVersion and m_strVersionInfo, where:
// m_strFixedFileVersion and m_strFixedProductVersion contain the fixed version info (language independent) and
// m_strVersionInfo contains the desired version info string (language dependent)
//
//**********************************************************************************************
void CVersionInfo::GetVersionInfo (LPTSTR szFilename, LPCTSTR strLangID/*=NULL*, LPCTSTR strInfoType/*=NULL*)
{
	DWORD dwVerInfoSize;
	DWORD dwHnd;
	void* pBuffer; 
	VS_FIXEDFILEINFO *pFixedInfo; // pointer to fixed file info structure
	LPVOID  lpVersion;    // String pointer to 'version' text
	UINT    uVersionLen;   // Current length of full version string
	TCHAR szGetName[500]; 
	 dwVerInfoSize = GetFileVersionInfoSize(szFilename, &dwHnd); 
	 if (dwVerInfoSize) 
	{ 
		  pBuffer = malloc(dwVerInfoSize); 
		  if (pBuffer == NULL)
			   return; 
		  GetFileVersionInfo(szFilename, dwHnd, dwVerInfoSize, pBuffer); 
		  // get the fixed file info (language-independend) 
		  VerQueryValue(pBuffer,_T("\\"),(void**)&pFixedInfo,(UINT *)&uVersionLen);  
		  m_strFixedProductVersion.Format ("%u,%u,%u,%u", HIWORD (pFixedInfo->dwProductVersionMS),
			              LOWORD (pFixedInfo->dwProductVersionMS),
			              HIWORD (pFixedInfo->dwProductVersionLS),
			              LOWORD (pFixedInfo->dwProductVersionLS)); 
		  m_strFixedFileVersion.Format ("%u,%u,%u,%u",HIWORD (pFixedInfo->dwFileVersionMS),
			             LOWORD (pFixedInfo->dwFileVersionMS),
			             HIWORD (pFixedInfo->dwFileVersionLS),
			             LOWORD (pFixedInfo->dwFileVersionLS)); 
		  // get the string file info (language-dependend) 
		  if (strLangID != NULL || strInfoType != NULL)
		  {
			   lstrcpy(szGetName, "\\StringFileInfo\\");  
			   lstrcat (szGetName, strLangID);
			   lstrcat (szGetName, "\\");
			   lstrcat (szGetName, strInfoType);
			   // copy version info, if desired entry exists
			   if (VerQueryValue(pBuffer,szGetName,(void**)&lpVersion,(UINT *)&uVersionLen) != 0)
				    m_strVersionInfo = (LPTSTR)lpVersion;
		  } 
		  if (pBuffer != NULL)
			   free(pBuffer); 
	} 
} 



Comments

  • Doesn't work for a Service

    Posted by Legacy on 12/02/2002 12:00am

    Originally posted by: BaliDawg

    When I drop this code into my CComModule-based ATL Service, GetFileVersionInfoSize() always returns 0. I even tried running this code from an MFC-based test app and it still returns 0.

    I'm guessing the VS_VERSION_INFO structure is not making it into the Service build, because if I right click on the Service.exe file from windows explorer and select Properties, there is no Version tab.

    Any one know why?

    Reply
  • thanks much! works great 4 me!

    Posted by Legacy on 10/10/2002 12:00am

    Originally posted by: Jim S

    Once again, if I woulda just looked here to start, I might have been able to even more time.

    Thanks for posting this!

    Reply
  • fatal error C1010

    Posted by Legacy on 08/04/2002 12:00am

    Originally posted by: Ch. Bock

    I'm desperatly try to get this piece of code compile, I get always "fatal error C1010 unexpected end of file while looking for precompiled header directive
    Error executing cl.exe. What is wrong?

    Reply
  • There is no finer investment for a community than putting useful stuff

    Posted by Legacy on 01/17/2002 12:00am

    Originally posted by: giulio corradi

    I had almost finished the implementation of a very similar class, but your proved much useful. Thanks a lot

    Reply
  • Use #pragma in header to include version.lib!

    Posted by Legacy on 01/21/2000 12:00am

    Originally posted by: Peter Kling

    Insert this pragma statement into the header file to ensure that it will always be included in the build!
    
    

    #pragma comment(lib, "version.lib") // We must have that lib!

    Reply
  • Version info. in binary files?

    Posted by Legacy on 06/09/1999 12:00am

    Originally posted by: Carsten Pedersen

    Does anyone know how to add version info. to a binary file i MS Windows. Is it at all possible to add version info. to a non-executable file, so that the OS can read the info. in the properties of the file?

    Regards
    Carsten Pedersen

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Available On-Demand Today's changing workforce dynamics, economic challenges, and technological advances are placing immense pressure on business leaders to turn their focus on people – their most valuable asset – in order to remain competitive. Research shows that a significant number of new employees quit within one year of taking a new job*. Whether it's through a merger and acquisition, or standard hiring process, like any first impression, early experiences shape their opinions of their new …

  • Do you know where your data is? Consumer cloud-based file sharing services store your sensitive company data on servers outside of your control, outside of your policy and regulatory guidelines – maybe even outside your country – and not managed by you. The potential for data leakage, security breaches, and harm to your business is enormous. Download this white paper to learn about file sync and share alternatives that allow you to manage and protect your sensitive data while integrating and …

Most Popular Programming Stories

More for Developers

RSS Feeds