Crypt Library Demo 1.00

VersionInfo.cpp

Go to the documentation of this file.
00001 /*
00002 Module : VersionInfo.CPP
00003 Purpose: Implementation for a MFC class encapsulation of Version Infos
00004 Created: PJN / 10-04-2000
00005 History: PJN / 07-07-2006 1. Updated copyright details
00006 2. Updated the code to clean compile on VC 2005
00007 3. Addition of CVERSIONINFO_EXT_CLASS and CVERSIONINFO_EXT_API macros to allow
00008 the class to be easily added to an extension DLL.
00009 4. Optimized CVersionInfo constructor code
00010 5. Reviewed all TRACE statements for correctness
00011 6. Updated the documentation to use the same style as the web site.
00012 PJN / 14-09-2008 1. Updated copyright details.
00013 2. Code now compiles cleanly using Code Analysis (/analyze)
00014 3. Updated code to compile correctly using _ATL_CSTRING_EXPLICIT_CONSTRUCTORS define
00015 4. Updated sample app to clean compile on VC 2008
00016 5. The code has now been updated to support VC 2005 or later only. 
00017 6. Removed VC 6 style AppWizard comments from the code.
00018 7. Reworked code to use ATL::CHeapPtr for required memory allocations
00019 
00020 Copyright (c) 2000 - 2008 by PJ Naughter (Web: www.naughter.com, Email: pjna@naughter.com)
00021 
00022 All rights reserved.
00023 
00024 Copyright / Usage Details:
00025 
00026 You are allowed to include the source code in any product (commercial, shareware, freeware or otherwise) 
00027 when your product is released in binary form. You are allowed to modify the source code in any way you want 
00028 except you cannot modify the copyright details at the top of each module. If you want to distribute source 
00029 code with your application, then you are only allowed to distribute versions released by the author. This is 
00030 to maintain a single distribution point for the source code. 
00031 
00032 */
00033 
00035 
00036 #include "stdafx.h"
00037 #include "VersionInfo.h"
00038 
00039 
00041 
00042 #ifdef _DEBUG
00043 #define new DEBUG_NEW
00044 #endif
00045 
00046 //Automatically pull in the win32 version Library
00047 #pragma comment(lib, "version.lib")
00048 
00049 
00051 
00052 CVersionInfo::CVersionInfo() : m_pffi(NULL),
00053 m_wLangID(0),
00054 m_wCharset(1252), //Use the ANSI code page as a default
00055 m_pTranslations(NULL),
00056 m_nTranslations(0)
00057 {
00058 }
00059 
00060 CVersionInfo::~CVersionInfo()
00061 {
00062     Unload();
00063 }
00064 
00065 void CVersionInfo::Unload()
00066 {
00067     m_pffi = NULL;
00068     if (m_VerData != NULL)
00069         m_VerData.Free();
00070     m_wLangID = 0;
00071     m_wCharset = 1252; //Use the ANSI code page as a default
00072     m_pTranslations = NULL;
00073     m_nTranslations = 0;
00074 }
00075 
00076 BOOL CVersionInfo::Load(LPCTSTR szFileName)
00077 {
00078     //Free up any previous memory lying around
00079     Unload();
00080 
00081     BOOL bSuccess = FALSE;
00082     DWORD dwHandle = 0;
00083     DWORD dwSize = GetFileVersionInfoSize(szFileName, &dwHandle);
00084     if (dwSize)
00085     {
00086         //Allocate some memory to hold the version info data
00087         ASSERT(m_VerData == NULL);
00088         if (!m_VerData.Allocate(dwSize))
00089         {
00090             SetLastError(ERROR_OUTOFMEMORY);
00091             return FALSE;
00092         }
00093         if (GetFileVersionInfo(szFileName, dwHandle, dwSize, m_VerData))
00094         {
00095             //Get the fixed size version info data
00096             UINT nLen = 0;
00097             if (VerQueryValue(m_VerData, _T("\\"), reinterpret_cast<LPVOID*>(&m_pffi), &nLen))
00098             {
00099                 //Retrieve the Lang ID and Character set ID
00100                 if (VerQueryValue(m_VerData, _T("\\VarFileInfo\\Translation"), reinterpret_cast<LPVOID*>(&m_pTranslations), &nLen) && (nLen >= sizeof(TRANSLATION))) 
00101                 {
00102                     m_nTranslations = nLen / sizeof(TRANSLATION);
00103                     m_wLangID = m_pTranslations[0].m_wLangID;
00104                     m_wCharset = m_pTranslations[0].m_wCodePage;
00105                 }
00106                 bSuccess = TRUE;
00107             }
00108             else
00109                 TRACE(_T("CVersionInfo::Load, Failed to query file size version info for file %s, LastError:%d\n"), szFileName, ::GetLastError());
00110         }
00111         else
00112             TRACE(_T("CVersionInfo::Load, Failed to read in version info for file %s, LastError:%d\n"), szFileName, ::GetLastError());
00113     }
00114     else
00115         TRACE(_T("CVersionInfo::Load, Failed to get version info for file %s, LastError:%d\n"), szFileName, ::GetLastError());
00116 
00117     return bSuccess;
00118 }
00119 
00120 VS_FIXEDFILEINFO* CVersionInfo::GetFixedFileInfo()
00121 {
00122     ASSERT(m_VerData != NULL); //Must have been loaded successfully
00123     return m_pffi;
00124 }
00125 
00126 DWORD CVersionInfo::GetFileFlagsMask()
00127 {
00128     ASSERT(m_VerData != NULL); //Must have been loaded successfully
00129     return m_pffi->dwFileFlagsMask;
00130 }
00131 
00132 DWORD CVersionInfo::GetFileFlags()
00133 {
00134     ASSERT(m_VerData != NULL); //Must have been loaded successfully
00135     return m_pffi->dwFileFlags;
00136 }
00137 
00138 DWORD CVersionInfo::GetOS()
00139 {
00140     ASSERT(m_VerData != NULL); //Must have been loaded successfully
00141     return m_pffi->dwFileOS;
00142 }
00143 
00144 DWORD CVersionInfo::GetFileType()
00145 {
00146     ASSERT(m_VerData != NULL); //Must have been loaded successfully
00147     return m_pffi->dwFileType;
00148 }
00149 
00150 DWORD CVersionInfo::GetFileSubType()
00151 {
00152     ASSERT(m_VerData != NULL); //Must have been loaded successfully
00153     return m_pffi->dwFileSubtype;
00154 }
00155 
00156 FILETIME CVersionInfo::GetCreationTime()
00157 {
00158     ASSERT(m_VerData != NULL); //Must have been loaded successfully
00159     FILETIME CreationTime;
00160     CreationTime.dwHighDateTime = m_pffi->dwFileDateMS; 
00161     CreationTime.dwLowDateTime = m_pffi->dwFileDateLS; 
00162     return CreationTime;
00163 }
00164 
00165 unsigned __int64 CVersionInfo::GetFileVersion()
00166 {
00167     ASSERT(m_VerData != NULL); //Must have been loaded successfully
00168     unsigned __int64 nFileVersion = 0;
00169     nFileVersion = m_pffi->dwFileVersionLS;
00170     nFileVersion += ((static_cast<unsigned __int64>(m_pffi->dwFileVersionMS)) << 32);
00171     return nFileVersion;
00172 }
00173 
00174 unsigned __int64 CVersionInfo::GetProductVersion()
00175 {
00176     ASSERT(m_VerData != NULL); //Must have been loaded successfully
00177     unsigned __int64 nProductVersion = 0;
00178     nProductVersion = m_pffi->dwProductVersionLS;
00179     nProductVersion += ((static_cast<unsigned __int64>(m_pffi->dwProductVersionMS)) << 32);
00180     return nProductVersion;
00181 }
00182 
00183 CString CVersionInfo::GetValue(const CString& sKey)
00184 {
00185     //Validate our parameters
00186     AFXASSUME(m_VerData != NULL);
00187 
00188     //What will be the return value from this function
00189     CString sVal;
00190 
00191     //Form the string to query with
00192     CString sQueryValue;
00193     sQueryValue.Format(_T("\\StringFileInfo\\%04x%04x\\%s"), m_wLangID, m_wCharset, sKey.operator LPCTSTR());
00194 
00195     //Note that the definition for VerQueryValue in the VC 2005 Winver.h header file is incorrectly
00196     //defined as taking a non-const 2nd parameter, so to avoid this issue, lets get a non const pointer
00197     //to the "sQueryValue" buffer
00198     LPTSTR pszQueryValue = sQueryValue.GetBuffer(sQueryValue.GetLength());
00199 
00200     //Do the query
00201     LPTSTR pVal = NULL;
00202     UINT nLen = 0;
00203     if (VerQueryValue(m_VerData, pszQueryValue, reinterpret_cast<LPVOID*>(&pVal), &nLen)) 
00204         sVal = pVal;
00205 
00206     //Release the non-const buffer now that we are finished with it    
00207     sQueryValue.ReleaseBuffer();
00208 
00209     return sVal;
00210 }
00211 
00212 CString CVersionInfo::GetCompanyName()
00213 {
00214     return GetValue(_T("CompanyName"));
00215 }
00216 
00217 CString CVersionInfo::GetFileDescription()
00218 {
00219     return GetValue(_T("FileDescription"));
00220 }
00221 
00222 CString CVersionInfo::GetFileVersionAsString()
00223 {
00224     return GetValue(_T("FileVersion"));
00225 }
00226 
00227 CString CVersionInfo::GetInternalName()
00228 {
00229     return GetValue(_T("InternalName"));
00230 }
00231 
00232 CString CVersionInfo::GetLegalCopyright()
00233 {
00234     return GetValue(_T("LegalCopyright"));
00235 }
00236 
00237 CString CVersionInfo::GetOriginalFilename()
00238 {
00239     return GetValue(_T("OriginalFilename"));
00240 }
00241 
00242 CString CVersionInfo::GetProductName()
00243 {
00244     return GetValue(_T("Productname"));
00245 }
00246 
00247 CString CVersionInfo::GetProductVersionAsString()
00248 {
00249     return GetValue(_T("ProductVersion"));
00250 }
00251 
00252 int CVersionInfo::GetNumberOfTranslations()
00253 {
00254     return m_nTranslations;
00255 }
00256 
00257 CString CVersionInfo::GetComments()
00258 {
00259     return GetValue(_T("Comments"));
00260 }
00261 
00262 CString CVersionInfo::GetLegalTrademarks()
00263 {
00264     return GetValue(_T("LegalTrademarks"));
00265 }
00266 
00267 CString CVersionInfo::GetPrivateBuild()
00268 {
00269     return GetValue(_T("PrivateBuild"));
00270 }
00271 
00272 CString CVersionInfo::GetSpecialBuild()
00273 {
00274     return GetValue(_T("SpecialBuild"));
00275 }
00276 
00277 CVersionInfo::TRANSLATION* CVersionInfo::GetTranslation(int nIndex)
00278 {
00279     ASSERT(nIndex >= 0 && nIndex < m_nTranslations);
00280     AFXASSUME(m_pTranslations);
00281     return &m_pTranslations[nIndex];
00282 }
00283 
00284 void CVersionInfo::SetTranslation(int nIndex)
00285 {
00286     TRANSLATION* pTranslation = GetTranslation(nIndex);
00287     m_wLangID = pTranslation->m_wLangID;
00288     m_wCharset = pTranslation->m_wCodePage;
00289 }
 All Classes Files Functions Variables Enumerator Defines