MiniVersion.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // MiniVersion.cpp Version 1.1
  2. //
  3. // Author: Hans Dietrich
  4. // [email protected]
  5. //
  6. // This software is released into the public domain.
  7. // You are free to use it in any way you like, except
  8. // that you may not sell this source code.
  9. //
  10. // This software is provided "as is" with no expressed
  11. // or implied warranty. I accept no liability for any
  12. // damage or loss of business that this software may cause.
  13. //
  14. ///////////////////////////////////////////////////////////////////////////////
  15. #include "MiniVersion.h"
  16. #include <strsafe.h>
  17. ///////////////////////////////////////////////////////////////////////////////
  18. // ctor
  19. CMiniVersion::CMiniVersion(LPCTSTR lpszPath)
  20. {
  21. ZeroMemory(m_szPath, sizeof(m_szPath));
  22. if (lpszPath && lpszPath[0] != 0)
  23. {
  24. lstrcpyn(m_szPath, lpszPath, sizeof(m_szPath)-1);
  25. }
  26. else
  27. {
  28. }
  29. m_pData = NULL;
  30. m_dwHandle = 0;
  31. for (int i = 0; i < 4; i++)
  32. {
  33. m_wFileVersion[i] = 0;
  34. m_wProductVersion[i] = 0;
  35. }
  36. m_dwFileFlags = 0;
  37. m_dwFileOS = 0;
  38. m_dwFileType = 0;
  39. m_dwFileSubtype = 0;
  40. ZeroMemory(m_szCompanyName, sizeof(m_szCompanyName));
  41. ZeroMemory(m_szProductName, sizeof(m_szProductName));
  42. ZeroMemory(m_szFileDescription, sizeof(m_szFileDescription));
  43. Init();
  44. }
  45. ///////////////////////////////////////////////////////////////////////////////
  46. // Init
  47. BOOL CMiniVersion::Init()
  48. {
  49. DWORD dwHandle;
  50. DWORD dwSize;
  51. BOOL rc;
  52. dwSize = ::GetFileVersionInfoSize((LPCTSTR)m_szPath, &dwHandle);
  53. if (dwSize == 0)
  54. return FALSE;
  55. m_pData = new BYTE [dwSize + 1];
  56. ZeroMemory(m_pData, dwSize+1);
  57. rc = ::GetFileVersionInfo((LPCTSTR)m_szPath, dwHandle, dwSize, m_pData);
  58. if (!rc)
  59. return FALSE;
  60. // get fixed info
  61. VS_FIXEDFILEINFO FixedInfo;
  62. if (GetFixedInfo(FixedInfo))
  63. {
  64. m_wFileVersion[0] = HIWORD(FixedInfo.dwFileVersionMS);
  65. m_wFileVersion[1] = LOWORD(FixedInfo.dwFileVersionMS);
  66. m_wFileVersion[2] = HIWORD(FixedInfo.dwFileVersionLS);
  67. m_wFileVersion[3] = LOWORD(FixedInfo.dwFileVersionLS);
  68. m_wProductVersion[0] = HIWORD(FixedInfo.dwProductVersionMS);
  69. m_wProductVersion[1] = LOWORD(FixedInfo.dwProductVersionMS);
  70. m_wProductVersion[2] = HIWORD(FixedInfo.dwProductVersionLS);
  71. m_wProductVersion[3] = LOWORD(FixedInfo.dwProductVersionLS);
  72. m_dwFileFlags = FixedInfo.dwFileFlags;
  73. m_dwFileOS = FixedInfo.dwFileOS;
  74. m_dwFileType = FixedInfo.dwFileType;
  75. m_dwFileSubtype = FixedInfo.dwFileSubtype;
  76. }
  77. else
  78. return FALSE;
  79. // get string info
  80. GetStringInfo(_T("CompanyName"), m_szCompanyName, MAX_PATH*2);
  81. GetStringInfo(_T("FileDescription"), m_szFileDescription, MAX_PATH*2);
  82. GetStringInfo(_T("ProductName"), m_szProductName, MAX_PATH*2);
  83. return TRUE;
  84. }
  85. ///////////////////////////////////////////////////////////////////////////////
  86. // Release
  87. void CMiniVersion::Release()
  88. {
  89. // do this manually, because we can't use objects requiring
  90. // a dtor within an exception handler
  91. if (m_pData)
  92. delete [] m_pData;
  93. m_pData = NULL;
  94. }
  95. ///////////////////////////////////////////////////////////////////////////////
  96. // GetFileVersion
  97. BOOL CMiniVersion::GetFileVersion(WORD * pwVersion)
  98. {
  99. for (int i = 0; i < 4; i++)
  100. *pwVersion++ = m_wFileVersion[i];
  101. return TRUE;
  102. }
  103. ///////////////////////////////////////////////////////////////////////////////
  104. // GetProductVersion
  105. BOOL CMiniVersion::GetProductVersion(WORD * pwVersion)
  106. {
  107. for (int i = 0; i < 4; i++)
  108. *pwVersion++ = m_wProductVersion[i];
  109. return TRUE;
  110. }
  111. ///////////////////////////////////////////////////////////////////////////////
  112. // GetFileFlags
  113. BOOL CMiniVersion::GetFileFlags(DWORD& rdwFlags)
  114. {
  115. rdwFlags = m_dwFileFlags;
  116. return TRUE;
  117. }
  118. ///////////////////////////////////////////////////////////////////////////////
  119. // GetFileOS
  120. BOOL CMiniVersion::GetFileOS(DWORD& rdwOS)
  121. {
  122. rdwOS = m_dwFileOS;
  123. return TRUE;
  124. }
  125. ///////////////////////////////////////////////////////////////////////////////
  126. // GetFileType
  127. BOOL CMiniVersion::GetFileType(DWORD& rdwType)
  128. {
  129. rdwType = m_dwFileType;
  130. return TRUE;
  131. }
  132. ///////////////////////////////////////////////////////////////////////////////
  133. // GetFileSubtype
  134. BOOL CMiniVersion::GetFileSubtype(DWORD& rdwType)
  135. {
  136. rdwType = m_dwFileSubtype;
  137. return TRUE;
  138. }
  139. ///////////////////////////////////////////////////////////////////////////////
  140. // GetCompanyName
  141. BOOL CMiniVersion::GetCompanyName(LPTSTR lpszCompanyName, int nSize)
  142. {
  143. if (!lpszCompanyName)
  144. return FALSE;
  145. ZeroMemory(lpszCompanyName, nSize);
  146. lstrcpyn(lpszCompanyName, m_szCompanyName, nSize-1);
  147. return TRUE;
  148. }
  149. ///////////////////////////////////////////////////////////////////////////////
  150. // GetFileDescription
  151. BOOL CMiniVersion::GetFileDescription(LPTSTR lpszFileDescription, int nSize)
  152. {
  153. if (!lpszFileDescription)
  154. return FALSE;
  155. ZeroMemory(lpszFileDescription, nSize);
  156. lstrcpyn(lpszFileDescription, m_szFileDescription, nSize-1);
  157. return TRUE;
  158. }
  159. ///////////////////////////////////////////////////////////////////////////////
  160. // GetProductName
  161. BOOL CMiniVersion::GetProductName(LPTSTR lpszProductName, int nSize)
  162. {
  163. if (!lpszProductName)
  164. return FALSE;
  165. ZeroMemory(lpszProductName, nSize);
  166. lstrcpyn(lpszProductName, m_szProductName, nSize-1);
  167. return TRUE;
  168. }
  169. ///////////////////////////////////////////////////////////////////////////////
  170. ///////////////////////////////////////////////////////////////////////////////
  171. //
  172. // protected methods
  173. //
  174. ///////////////////////////////////////////////////////////////////////////////
  175. ///////////////////////////////////////////////////////////////////////////////
  176. ///////////////////////////////////////////////////////////////////////////////
  177. // GetFixedInfo
  178. BOOL CMiniVersion::GetFixedInfo(VS_FIXEDFILEINFO& rFixedInfo)
  179. {
  180. BOOL rc;
  181. UINT nLength;
  182. VS_FIXEDFILEINFO *pFixedInfo = NULL;
  183. if (!m_pData)
  184. return FALSE;
  185. if (m_pData)
  186. rc = ::VerQueryValue(m_pData, _T("\\"), (void **) &pFixedInfo, &nLength);
  187. else
  188. rc = FALSE;
  189. if (rc)
  190. memcpy (&rFixedInfo, pFixedInfo, sizeof (VS_FIXEDFILEINFO));
  191. return rc;
  192. }
  193. ///////////////////////////////////////////////////////////////////////////////
  194. // GetStringInfo
  195. BOOL CMiniVersion::GetStringInfo(LPCTSTR lpszKey, LPTSTR lpszReturnValue, unsigned int cchBuffer)
  196. {
  197. BOOL rc;
  198. DWORD *pdwTranslation;
  199. UINT nLength;
  200. LPTSTR lpszValue;
  201. if (m_pData == NULL)
  202. return FALSE;
  203. if (!lpszReturnValue)
  204. return FALSE;
  205. if (!lpszKey)
  206. return FALSE;
  207. *lpszReturnValue = 0;
  208. rc = ::VerQueryValue(m_pData, _T("\\VarFileInfo\\Translation"),
  209. (void**) &pdwTranslation, &nLength);
  210. if (!rc)
  211. return FALSE;
  212. TCHAR szKey[2000] = {0};
  213. StringCchPrintf(szKey, 2000, _T("\\StringFileInfo\\%04x%04x\\%s"), LOWORD (*pdwTranslation), HIWORD (*pdwTranslation), lpszKey);
  214. rc = ::VerQueryValue(m_pData, szKey, (void**) &lpszValue, &nLength);
  215. if (!rc)
  216. return FALSE;
  217. StringCchCopy(lpszReturnValue,cchBuffer, lpszValue);
  218. return TRUE;
  219. }