FileInfo.cpp 4.6 KB

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