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

  • Live Event Date: April 22, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Database professionals — whether developers or DBAs — can often save valuable time by learning to get the most from their new or existing productivity tools. Whether you're responsible for managing database projects, performing database health checks and reporting, analyzing code, or measuring software engineering metrics, it's likely you're not taking advantage of some of the lesser-known features of Toad from Dell. Attend this live …

  • Confused by all the agile advice? Relax! With the Agile for Dummies eBook by your side you'll learn the fundamentals of agile and how to increase the productivity of your software teams while enabling them to produce higher-quality solutions that better fulfill customer needs much faster.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds