FileInfo.cpp 4.5 KB

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