FileInfo.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include <Exceptions.h>
  6. #include <Windows.hpp>
  7. #include <Math.hpp>
  8. #include "FileInfo.h"
  9. #include "FileBuffer.h"
  10. //---------------------------------------------------------------------------
  11. #pragma package(smart_init)
  12. //---------------------------------------------------------------------------
  13. // Return pointer to file version info block
  14. void * __fastcall CreateFileInfo(UnicodeString FileName)
  15. {
  16. unsigned long Handle;
  17. unsigned int Size;
  18. char * Result = NULL;
  19. // Get file version info block size
  20. Size = GetFileVersionInfoSize(FileName.c_str(), &Handle);
  21. // If size is valid
  22. if (Size > 0)
  23. {
  24. Result = new char[Size];
  25. // Get file version info block
  26. if (!GetFileVersionInfo(FileName.c_str(), Handle, Size, Result))
  27. {
  28. delete[] Result;
  29. Result = NULL;
  30. }
  31. }
  32. return Result;
  33. }
  34. //---------------------------------------------------------------------------
  35. // Free file version info block memory
  36. void __fastcall FreeFileInfo(void * FileInfo)
  37. {
  38. delete[] static_cast<char *>(FileInfo);
  39. }
  40. //---------------------------------------------------------------------------
  41. typedef TTranslation TTranslations[65536];
  42. typedef TTranslation *PTranslations;
  43. //---------------------------------------------------------------------------
  44. // Return pointer to fixed file version info
  45. PVSFixedFileInfo __fastcall GetFixedFileInfo(void * FileInfo)
  46. {
  47. UINT Len;
  48. PVSFixedFileInfo Result;
  49. if (!VerQueryValue(FileInfo, L"\\", (void**)&Result, &Len))
  50. {
  51. throw Exception(L"Fixed file info not available");
  52. }
  53. return Result;
  54. }
  55. //---------------------------------------------------------------------------
  56. // Return number of available file version info translations
  57. unsigned __fastcall GetTranslationCount(void * FileInfo)
  58. {
  59. PTranslations P;
  60. UINT Len;
  61. if (!VerQueryValue(FileInfo, L"\\VarFileInfo\\Translation", (void**)&P, &Len))
  62. {
  63. throw Exception(L"File info translations not available");
  64. }
  65. return Len / 4;
  66. }
  67. //---------------------------------------------------------------------------
  68. // Return i-th translation in the file version info translation list
  69. TTranslation __fastcall GetTranslation(void * FileInfo, unsigned i)
  70. {
  71. PTranslations P;
  72. UINT Len;
  73. if (!VerQueryValue(FileInfo, L"\\VarFileInfo\\Translation", (void**)&P, &Len))
  74. {
  75. throw Exception(L"File info translations not available");
  76. }
  77. if (i * sizeof(TTranslation) >= Len)
  78. {
  79. throw Exception(L"Specified translation not available");
  80. }
  81. return P[i];
  82. }
  83. //---------------------------------------------------------------------------
  84. // Return the value of the specified file version info string using the
  85. // specified translation
  86. UnicodeString __fastcall GetFileInfoString(void * FileInfo,
  87. TTranslation Translation, UnicodeString StringName, bool AllowEmpty)
  88. {
  89. UnicodeString Result;
  90. wchar_t * P;
  91. UINT Len;
  92. UnicodeString SubBlock =
  93. UnicodeString(L"\\StringFileInfo\\") + IntToHex(Translation.Language, 4) + IntToHex(Translation.CharSet, 4) + L"\\" + StringName;
  94. if (!VerQueryValue(FileInfo, SubBlock.c_str(), (void**)&P, &Len))
  95. {
  96. if (!AllowEmpty)
  97. {
  98. throw Exception("Specified file info string not available");
  99. }
  100. }
  101. else
  102. {
  103. Result = UnicodeString(P, Len);
  104. PackStr(Result);
  105. }
  106. return Result;
  107. }
  108. //---------------------------------------------------------------------------
  109. int __fastcall CalculateCompoundVersion(int MajorVer, int MinorVer, int Release)
  110. {
  111. int CompoundVer = 10000 * (Release + 100 * (MinorVer + 100 * MajorVer));
  112. return CompoundVer;
  113. }
  114. //---------------------------------------------------------------------------
  115. int ZeroBuildNumber(int CompoundVersion)
  116. {
  117. return (CompoundVersion / 10000 * 10000);
  118. }
  119. //---------------------------------------------------------------------------
  120. int __fastcall StrToCompoundVersion(UnicodeString S)
  121. {
  122. int MajorVer = Min(StrToInt(CutToChar(S, L'.', false)), 99);
  123. int MinorVer = Min(StrToInt(CutToChar(S, L'.', false)), 99);
  124. int Release = S.IsEmpty() ? 0 : Min(StrToInt(CutToChar(S, L'.', false)), 99);
  125. return CalculateCompoundVersion(MajorVer, MinorVer, Release);
  126. }
  127. //---------------------------------------------------------------------------
  128. int __fastcall CompareVersion(UnicodeString V1, UnicodeString V2)
  129. {
  130. int Result = 0;
  131. while ((Result == 0) && (!V1.IsEmpty() || !V2.IsEmpty()))
  132. {
  133. int C1 = StrToIntDef(CutToChar(V1, L'.', false), 0);
  134. int C2 = StrToIntDef(CutToChar(V2, L'.', false), 0);
  135. Result = CompareValue(C1, C2);
  136. }
  137. return Result;
  138. }