FileInfo.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Windows.hpp>
  5. #include "FileInfo.h"
  6. //---------------------------------------------------------------------------
  7. #pragma package(smart_init)
  8. //---------------------------------------------------------------------------
  9. // Return pointer to file version info block
  10. void * __fastcall CreateFileInfo(AnsiString FileName)
  11. {
  12. DWORD Handle, Size;
  13. void * Result = NULL;
  14. // Get file version info block size
  15. Size = GetFileVersionInfoSize(FileName.c_str(), &Handle);
  16. // If size is valid
  17. if (Size)
  18. {
  19. Result = new char[Size];
  20. // Get file version info block
  21. if (!GetFileVersionInfo(FileName.c_str(), Handle, Size, Result))
  22. {
  23. delete[] Result;
  24. Result = NULL;
  25. }
  26. }
  27. return Result;
  28. }
  29. //---------------------------------------------------------------------------
  30. // Free file version info block memory
  31. void __fastcall FreeFileInfo(void * FileInfo)
  32. {
  33. delete[] FileInfo;
  34. }
  35. //---------------------------------------------------------------------------
  36. typedef TTranslation TTranslations[65536];
  37. typedef TTranslation *PTranslations;
  38. //---------------------------------------------------------------------------
  39. // Return pointer to fixed file version info
  40. PVSFixedFileInfo __fastcall GetFixedFileInfo(void * FileInfo)
  41. {
  42. UINT Len;
  43. PVSFixedFileInfo Result;
  44. if (!VerQueryValue(FileInfo, "\\", (void**)&Result, &Len))
  45. throw Exception("Fixed file info not available");
  46. return Result;
  47. };
  48. //---------------------------------------------------------------------------
  49. // Return number of available file version info translations
  50. unsigned __fastcall GetTranslationCount(void * FileInfo)
  51. {
  52. PTranslations P;
  53. UINT Len;
  54. if (!VerQueryValue(FileInfo, "\\VarFileInfo\\Translation", (void**)&P, &Len))
  55. throw Exception("File info translations not available");
  56. return Len / 4;
  57. }
  58. //---------------------------------------------------------------------------
  59. // Return i-th translation in the file version info translation list
  60. TTranslation __fastcall GetTranslation(void * FileInfo, unsigned i)
  61. {
  62. PTranslations P;
  63. UINT Len;
  64. if (!VerQueryValue(FileInfo, "\\VarFileInfo\\Translation", (void**)&P, &Len))
  65. throw Exception("File info translations not available");
  66. if (i * sizeof(TTranslation) >= Len)
  67. throw Exception("Specified translation not available");
  68. return P[i];
  69. };
  70. //---------------------------------------------------------------------------
  71. // Return the name of the specified language
  72. AnsiString __fastcall GetLanguage(Word Language)
  73. {
  74. UINT Len;
  75. Char P[256];
  76. Len = VerLanguageName(Language, P, sizeof(P));
  77. if (Len > sizeof(P))
  78. throw Exception("Language not available");
  79. return AnsiString(P, Len);
  80. };
  81. //---------------------------------------------------------------------------
  82. // Return the value of the specified file version info string using the
  83. // specified translation
  84. AnsiString __fastcall GetFileInfoString(void * FileInfo,
  85. TTranslation Translation, AnsiString StringName)
  86. {
  87. PChar P;
  88. UINT Len;
  89. if (!VerQueryValue(FileInfo, (AnsiString("\\StringFileInfo\\") +
  90. IntToHex(Translation.Language, 4) +
  91. IntToHex(Translation.CharSet, 4) +
  92. "\\" + StringName).c_str(), (void**)&P, &Len))
  93. {
  94. throw Exception("Specified file info string not available");
  95. }
  96. // c_str() makes sure that returned string has only necessary bytes allocated
  97. return AnsiString(P, Len).c_str();
  98. };