Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
5.4 KiB

  1. ////////////////////////////////////////////////////////////////
  2. // 1998 Microsoft Systems Journal
  3. // If this code works, it was written by Paul DiLascia.
  4. // If not, I don't know who wrote it.
  5. //
  6. // CModuleVersion provides an easy way to get version info
  7. // for a module.(DLL or EXE).
  8. //
  9. // This code appeard in April 1998 edition of Microsoft Systems
  10. // Journal.
  11. //
  12. // 27-July-1998 -- Adapted by James A. McLaughiln (Schlumberger
  13. // Technology Corp.) for Smart Cards. Merged with the concepts from
  14. // CFileVersion class contributed by Manuel Laflamme on a posting to
  15. // www.codeguru.com. If these mods don't work, then you can blame me.
  16. #include "StdAfx.h"
  17. #include "slbModVer.h"
  18. CModuleVersion::CModuleVersion()
  19. : m_pVersionInfo(NULL)
  20. {
  21. }
  22. //////////////////
  23. // Destroy: delete version info
  24. //
  25. CModuleVersion::~CModuleVersion()
  26. {
  27. delete [] m_pVersionInfo;
  28. }
  29. BOOL CModuleVersion::GetFileVersionInfo(LPCTSTR modulename)
  30. {
  31. // get module handle
  32. HMODULE hModule = ::GetModuleHandle(modulename);
  33. if (hModule==NULL && modulename!=NULL)
  34. return FALSE;
  35. return GetFileVersionInfo(hModule);
  36. }
  37. //////////////////
  38. // Get file version info for a given module
  39. // Allocates storage for all info, fills "this" with
  40. // VS_FIXEDFILEINFO, and sets codepage.
  41. //
  42. BOOL CModuleVersion::GetFileVersionInfo(HMODULE hModule)
  43. {
  44. m_translation.charset = 1252; // default = ANSI code page
  45. memset((VS_FIXEDFILEINFO*)this, 0, sizeof(VS_FIXEDFILEINFO));
  46. // get module file name
  47. TCHAR filename[_MAX_PATH];
  48. DWORD len = GetModuleFileName(hModule, filename,
  49. sizeof(filename)/sizeof(filename[0]));
  50. if (len <= 0)
  51. return FALSE;
  52. // read file version info
  53. DWORD dwDummyHandle; // will always be set to zero
  54. len = GetFileVersionInfoSize(filename, &dwDummyHandle);
  55. if (len <= 0)
  56. return FALSE;
  57. m_pVersionInfo = new BYTE[len]; // allocate version info
  58. if (!::GetFileVersionInfo(filename, 0, len, m_pVersionInfo))
  59. return FALSE;
  60. // copy fixed info to myself, which am derived from VS_FIXEDFILEINFO
  61. if (!GetFixedInfo(*(VS_FIXEDFILEINFO*)this))
  62. return FALSE;
  63. // Get translation info
  64. LPVOID lpvi;
  65. UINT iLen;
  66. if (VerQueryValue(m_pVersionInfo,
  67. TEXT("\\VarFileInfo\\Translation"), &lpvi, &iLen) && iLen >= 4) {
  68. m_translation = *(TRANSLATION*)lpvi;
  69. TRACE(TEXT("code page = %d\n"), m_translation.charset);
  70. }
  71. return dwSignature == VS_FFI_SIGNATURE;
  72. }
  73. //////////////////
  74. // Get string file info.
  75. // Key name is something like "CompanyName".
  76. // returns the value as a CString.
  77. //
  78. CString CModuleVersion::GetValue(LPCTSTR lpKeyName)
  79. {
  80. CString sVal;
  81. if (m_pVersionInfo) {
  82. // To get a string value must pass query in the form
  83. //
  84. // "\StringFileInfo\<langID><codepage>\keyname"
  85. //
  86. // where <lang-codepage> is the languageID concatenated with the
  87. // code page, in hex. Wow.
  88. //
  89. CString query;
  90. query.Format(_T("\\StringFileInfo\\%04x%04x\\%s"),
  91. m_translation.langID,
  92. m_translation.charset,
  93. lpKeyName);
  94. LPCTSTR pVal;
  95. UINT iLenVal;
  96. if (VerQueryValue(m_pVersionInfo, (LPTSTR)(LPCTSTR)query,
  97. (LPVOID*)&pVal, &iLenVal)) {
  98. sVal = pVal;
  99. }
  100. }
  101. return sVal;
  102. }
  103. // typedef for DllGetVersion proc
  104. typedef HRESULT (CALLBACK* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
  105. /////////////////
  106. // Get DLL Version by calling DLL's DllGetVersion proc
  107. //
  108. BOOL CModuleVersion::DllGetVersion(LPCTSTR modulename, DLLVERSIONINFO& dvi)
  109. {
  110. HINSTANCE hinst = LoadLibrary(modulename);
  111. if (!hinst)
  112. return FALSE;
  113. // Must use GetProcAddress because the DLL might not implement
  114. // DllGetVersion. Depending upon the DLL, the lack of implementation of the
  115. // function may be a version marker in itself.
  116. //
  117. DLLGETVERSIONPROC pDllGetVersion =
  118. (DLLGETVERSIONPROC)GetProcAddress(hinst, reinterpret_cast<const char *>(_T("DllGetVersion")));
  119. if (!pDllGetVersion)
  120. return FALSE;
  121. memset(&dvi, 0, sizeof(dvi)); // clear
  122. dvi.cbSize = sizeof(dvi); // set size for Windows
  123. return SUCCEEDED((*pDllGetVersion)(&dvi));
  124. }
  125. BOOL CModuleVersion::GetFixedInfo(VS_FIXEDFILEINFO& vsffi)
  126. {
  127. // Must furst call GetFileVersionInfo or constructor with arg
  128. ASSERT(m_pVersionInfo != NULL);
  129. if ( m_pVersionInfo == NULL )
  130. return FALSE;
  131. UINT nQuerySize;
  132. VS_FIXEDFILEINFO* pVsffi;
  133. if ( ::VerQueryValue((void **)m_pVersionInfo, _T("\\"),
  134. (void**)&pVsffi, &nQuerySize) )
  135. {
  136. vsffi = *pVsffi;
  137. return TRUE;
  138. }
  139. return FALSE;
  140. }
  141. CString CModuleVersion::GetFixedFileVersion()
  142. {
  143. CString strVersion;
  144. VS_FIXEDFILEINFO vsffi;
  145. if (GetFixedInfo(vsffi))
  146. {
  147. strVersion.Format (_T("%u,%u,%u,%u"),HIWORD(dwFileVersionMS),
  148. LOWORD(dwFileVersionMS),
  149. HIWORD(dwFileVersionLS),
  150. LOWORD(dwFileVersionLS));
  151. }
  152. return strVersion;
  153. }
  154. CString CModuleVersion::GetFixedProductVersion()
  155. {
  156. CString strVersion;
  157. VS_FIXEDFILEINFO vsffi;
  158. if (GetFixedInfo(vsffi))
  159. {
  160. strVersion.Format (_T("%u,%u,%u,%u"), HIWORD(dwProductVersionMS),
  161. LOWORD(dwProductVersionMS),
  162. HIWORD(dwProductVersionLS),
  163. LOWORD(dwProductVersionLS));
  164. }
  165. return strVersion;
  166. }