|
-
August 5th, 2004, 03:37 AM
#1
How do I get my App's version info
Hi all,
I have developed an application with MS VC++ 6.0
I have set the version resource and want to know how to acquire this version info in my application so I can display it in a dialog. Is there an API for this, my search of msdn failed... any assistance will be much obliged
thanks
The most knowledgeable people are those who know that they know nothing.
-
August 5th, 2004, 03:45 AM
#2
To retrieve the version information you can use the following functions...
Code:
DWORD GetFileVersionInfoSize(LPTSTR lptstrFilename, LPDWORD lpdwHandle)
BOOL GetFileVersionInfo(LPTSTR lptstrFilename,
DWORD dwHandle,
DWORD dwLen,
LPVOID lpData)
BOOL VerQueryValue(const LPVOID pBlock,
LPTSTR lpSubBlock,
LPVOID *lplpBuffer,
PUINT puLen)
DWORD VerLanguageName(DWORD wLang, LPTSTR szLang, DWORD nSize)
'GetFileVersionInfoSize()' returns the size of the version information. 'GetFileVersionInfo()' uses information retrieved by 'GetFileVersionInfoSize()' to retrieve a structure that contains the version information. 'VerQueryValue()' retrieves a specific member from that structure. Your setup program needs to call those function depending on the error code returned by e.g. 'VerInstallFile()'.
The following is an example class which wraps the whole thing...
Code:
#ifndef VERSION
#define VERSION
#pragma warning(disable : 4505 4710)
#pragma comment(lib, "version.lib")
#include <string>
#include <sstream>
#include <iomanip>
#include <exception>
#include <new>
#include <windows.h>
namespace Version
{
struct Language
{
WORD m_wLanguage;
WORD m_wCodePage;
Language()
{
m_wLanguage = 0;
m_wCodePage = 0;
}
};
}
class CVersion
{
public:
CVersion()
{
// Get application name
TCHAR szBuffer[MAX_PATH] = "";
if(::GetModuleFileName(0, szBuffer, sizeof(szBuffer) - 1))
{
std::string strAppName = szBuffer;
strAppName = strAppName.substr(strAppName.rfind("\\") + 1);
// Get version info
DWORD dwHandle = 0;
DWORD dwResourceSize = ::GetFileVersionInfoSize(const_cast<char *>(strAppName.c_str()), &dwHandle);
if(dwResourceSize)
{
m_pucResourceData = new unsigned char[dwResourceSize];
if(m_pucResourceData)
{
if(::GetFileVersionInfo(const_cast<char *>(strAppName.c_str()),
0,
dwResourceSize,
static_cast<LPVOID>(m_pucResourceData)) != FALSE)
{
UINT uiSize = 0;
// Get language information
if(::VerQueryValue(static_cast<LPVOID>(m_pucResourceData),
"\\VarFileInfo\\Translation",
reinterpret_cast<LPVOID *>(&m_pLanguageInformation),
&uiSize) == FALSE)
throw std::exception("Requested localized version information not available");
}
else
{
std::stringstream strsException;
strsException << "Could not get version information (Windows error: " << ::GetLastError() << ")";
throw std::exception(strsException.str().c_str());
}
}
else
throw std::bad_alloc();
}
else
{
std::stringstream strsException;
strsException << "No version information found (Windows error: " << ::GetLastError() << ")";
throw std::exception(strsException.str().c_str());
}
}
else
throw std::exception("Could not get application name");
}
~CVersion() { delete [] m_pucResourceData; }
std::string GetProductName() const { return GetValue("ProductName"); }
std::string GetInternalName() const { return GetValue("InternalName"); }
std::string GetProductVersion() const { return GetValue("ProductVersion"); }
std::string GetSpecialBuild() const { return GetValue("SpecialBuild"); }
std::string GetPrivateBuild() const { return GetValue("PrivateBuild"); }
std::string GetCopyright() const { return GetValue("LegalCopyright"); }
std::string GetTrademarks() const { return GetValue("LegalTrademarks"); }
std::string GetComments() const { return GetValue("Comments"); }
std::string GetCompanyName() const { return GetValue("CompanyName"); }
std::string GetFileVersion() const { return GetValue("FileVersion"); }
std::string GetFileDescription() const { return GetValue("FileDescrition"); }
private:
unsigned char *m_pucResourceData; // Resource data
Version::Language *m_pLanguageInformation; // Language information
std::string GetValue(const std::string &refcstrKey) const
{
if(m_pucResourceData)
{
UINT uiSize = 0;
std::stringstream strsTemp;
LPVOID pvValue = 0;
// Build query string
strsTemp << "\\StringFileInfo\\" << std::setw(4) << std::setfill('0') << std::hex
<< m_pLanguageInformation->m_wLanguage << std::setw(4) << std::hex
<< m_pLanguageInformation->m_wCodePage << "\\" << refcstrKey;
if(::VerQueryValue(static_cast<LPVOID>(m_pucResourceData),
const_cast<LPTSTR>(strsTemp.str().c_str()),
static_cast<LPVOID *>(&pvValue),
&uiSize) != FALSE)
return static_cast<LPTSTR>(pvValue);
}
return "";
}
};
#endif
Last edited by Andreas Masur; August 5th, 2004 at 10:24 AM.
-
August 5th, 2004, 04:26 AM
#3
Thank you
You reply is very useful
The most knowledgeable people are those who know that they know nothing.
-
August 5th, 2004, 06:03 AM
#4
-
August 5th, 2004, 06:25 AM
#5
strsException << "Could not get version information (Windows error: " << ::GetLastError() >> ")";
I think there was a typing error there,
strsException << "Could not get version information (Windows error: " << ::GetLastError() << ")";
thanks
The most knowledgeable people are those who know that they know nothing.
-
August 5th, 2004, 10:25 AM
#6
 Originally Posted by Tamerocyte
I think there was a typing error there,
Yep...sorry about that...
-
August 5th, 2004, 10:56 AM
#7
 Originally Posted by Tamerocyte
Hi all,
I have developed an application with MS VC++ 6.0
I have set the version resource and want to know how to acquire this version info in my application so I can display it in a dialog. Is there an API for this, my search of msdn failed... any assistance will be much obliged
thanks
You could also take a look at this article: Get VersionInfo from resource file
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|