FileInfo.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include <Windows.hpp>
  6. #include "FileInfo.h"
  7. //---------------------------------------------------------------------------
  8. #pragma package(smart_init)
  9. //---------------------------------------------------------------------------
  10. // Return pointer to file version info block
  11. void * __fastcall CreateFileInfo(AnsiString FileName)
  12. {
  13. DWORD Handle, Size;
  14. void * Result = NULL;
  15. // Get file version info block size
  16. Size = GetFileVersionInfoSize(FileName.c_str(), &Handle);
  17. // If size is valid
  18. if (Size)
  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[] 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, "\\", (void**)&Result, &Len))
  46. throw Exception("Fixed file info not available");
  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, "\\VarFileInfo\\Translation", (void**)&P, &Len))
  56. throw Exception("File info translations not available");
  57. return Len / 4;
  58. }
  59. //---------------------------------------------------------------------------
  60. // Return i-th translation in the file version info translation list
  61. TTranslation __fastcall GetTranslation(void * FileInfo, unsigned i)
  62. {
  63. PTranslations P;
  64. UINT Len;
  65. if (!VerQueryValue(FileInfo, "\\VarFileInfo\\Translation", (void**)&P, &Len))
  66. throw Exception("File info translations not available");
  67. if (i * sizeof(TTranslation) >= Len)
  68. throw Exception("Specified translation not available");
  69. return P[i];
  70. };
  71. //---------------------------------------------------------------------------
  72. // Return the name of the specified language
  73. AnsiString __fastcall GetLanguage(Word Language)
  74. {
  75. UINT Len;
  76. Char P[256];
  77. Len = VerLanguageName(Language, P, sizeof(P));
  78. if (Len > sizeof(P))
  79. throw Exception("Language not available");
  80. return AnsiString(P, Len);
  81. };
  82. //---------------------------------------------------------------------------
  83. // Return the value of the specified file version info string using the
  84. // specified translation
  85. AnsiString __fastcall GetFileInfoString(void * FileInfo,
  86. TTranslation Translation, AnsiString StringName)
  87. {
  88. PChar P;
  89. UINT Len;
  90. if (!VerQueryValue(FileInfo, (AnsiString("\\StringFileInfo\\") +
  91. IntToHex(Translation.Language, 4) +
  92. IntToHex(Translation.CharSet, 4) +
  93. "\\" + StringName).c_str(), (void**)&P, &Len))
  94. {
  95. throw Exception("Specified file info string not available");
  96. }
  97. // c_str() makes sure that returned string has only necessary bytes allocated
  98. return AnsiString(P, Len).c_str();
  99. };