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.

146 lines
3.4 KiB

  1. // FileVersion.cpp: implementation of the CFileVersion class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "FileVersion.h"
  6. #ifdef _DEBUG
  7. #undef THIS_FILE
  8. static char THIS_FILE[]=__FILE__;
  9. #define new DEBUG_NEW
  10. #endif
  11. //////////////////////////////////////////////////////////////////////
  12. // Construction/Destruction
  13. //////////////////////////////////////////////////////////////////////
  14. CFileVersion::CFileVersion()
  15. {
  16. m_lpVersionData = NULL;
  17. m_dwLangCharset = 0;
  18. }
  19. CFileVersion::~CFileVersion()
  20. {
  21. Close();
  22. }
  23. void CFileVersion::Close()
  24. {
  25. delete[] m_lpVersionData;
  26. m_lpVersionData = NULL;
  27. m_dwLangCharset = 0;
  28. }
  29. BOOL CFileVersion::Open(LPCTSTR lpszModuleName)
  30. {
  31. ASSERT(_tcslen(lpszModuleName) > 0);
  32. ASSERT(m_lpVersionData == NULL);
  33. // Get the version information size for allocate the buffer
  34. DWORD dwHandle;
  35. DWORD dwDataSize = ::GetFileVersionInfoSize((LPTSTR)lpszModuleName, &dwHandle);
  36. if ( dwDataSize == 0 )
  37. return FALSE;
  38. // Allocate buffer and retrieve version information
  39. m_lpVersionData = new BYTE[dwDataSize];
  40. if (!::GetFileVersionInfo((LPTSTR)lpszModuleName, dwHandle, dwDataSize,
  41. (void**)m_lpVersionData) )
  42. {
  43. Close();
  44. return FALSE;
  45. }
  46. // Retrieve the first language and character-set identifier
  47. UINT nQuerySize;
  48. DWORD* pTransTable;
  49. if (!::VerQueryValue(m_lpVersionData, _T("\\VarFileInfo\\Translation"),
  50. (void **)&pTransTable, &nQuerySize) )
  51. {
  52. Close();
  53. return FALSE;
  54. }
  55. // Swap the words to have lang-charset in the correct format
  56. m_dwLangCharset = MAKELONG(HIWORD(pTransTable[0]), LOWORD(pTransTable[0]));
  57. return TRUE;
  58. }
  59. CString CFileVersion::QueryValue(LPCTSTR lpszValueName,
  60. DWORD dwLangCharset /* = 0*/)
  61. {
  62. // Must call Open() first
  63. // ASSERT(m_lpVersionData != NULL);
  64. if ( m_lpVersionData == NULL )
  65. return (CString)_T("");
  66. // If no lang-charset specified use default
  67. if ( dwLangCharset == 0 )
  68. dwLangCharset = m_dwLangCharset;
  69. // Query version information value
  70. UINT nQuerySize;
  71. LPVOID lpData;
  72. CString strValue, strBlockName;
  73. strBlockName.Format(_T("\\StringFileInfo\\%08lx\\%s"),
  74. dwLangCharset, lpszValueName);
  75. if ( ::VerQueryValue((void **)m_lpVersionData, strBlockName.GetBuffer(0),
  76. &lpData, &nQuerySize) )
  77. strValue = (LPCTSTR)lpData;
  78. strBlockName.ReleaseBuffer();
  79. return strValue;
  80. }
  81. BOOL CFileVersion::GetFixedInfo(VS_FIXEDFILEINFO& vsffi)
  82. {
  83. // Must call Open() first
  84. ASSERT(m_lpVersionData != NULL);
  85. if ( m_lpVersionData == NULL )
  86. return FALSE;
  87. UINT nQuerySize;
  88. VS_FIXEDFILEINFO* pVsffi;
  89. if ( ::VerQueryValue((void **)m_lpVersionData, _T("\\"),
  90. (void**)&pVsffi, &nQuerySize) )
  91. {
  92. vsffi = *pVsffi;
  93. return TRUE;
  94. }
  95. return FALSE;
  96. }
  97. CString CFileVersion::GetFixedFileVersion()
  98. {
  99. CString strVersion;
  100. VS_FIXEDFILEINFO vsffi;
  101. if ( GetFixedInfo(vsffi) )
  102. {
  103. strVersion.Format(_T("%u,%u,%u,%u"),HIWORD(vsffi.dwFileVersionMS),
  104. LOWORD(vsffi.dwFileVersionMS),
  105. HIWORD(vsffi.dwFileVersionLS),
  106. LOWORD(vsffi.dwFileVersionLS));
  107. }
  108. return strVersion;
  109. }
  110. CString CFileVersion::GetFixedProductVersion()
  111. {
  112. CString strVersion;
  113. VS_FIXEDFILEINFO vsffi;
  114. if ( GetFixedInfo(vsffi) )
  115. {
  116. strVersion.Format(_T("%u,%u,%u,%u"), HIWORD(vsffi.dwProductVersionMS),
  117. LOWORD(vsffi.dwProductVersionMS),
  118. HIWORD(vsffi.dwProductVersionLS),
  119. LOWORD(vsffi.dwProductVersionLS));
  120. }
  121. return strVersion;
  122. }