FileInfo.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include <Exceptions.h>
  6. #include <Windows.hpp>
  7. #include "FileInfo.h"
  8. #include "FileBuffer.h"
  9. //---------------------------------------------------------------------------
  10. #pragma package(smart_init)
  11. //---------------------------------------------------------------------------
  12. #define DWORD_ALIGN( base, ptr ) \
  13. ( (LPBYTE)(base) + ((((LPBYTE)(ptr) - (LPBYTE)(base)) + 3) & ~3) )
  14. struct VS_VERSION_INFO_STRUCT32
  15. {
  16. WORD wLength;
  17. WORD wValueLength;
  18. WORD wType;
  19. WCHAR szKey[1];
  20. };
  21. //---------------------------------------------------------------------------
  22. unsigned int VERSION_GetFileVersionInfo_PE(const char * FileName, unsigned int DataSize, void * Data)
  23. {
  24. unsigned int Len;
  25. bool NeedFree = false;
  26. HMODULE Module = GetModuleHandle(FileName);
  27. if (Module == NULL)
  28. {
  29. Module = LoadLibraryEx(FileName, 0, LOAD_LIBRARY_AS_DATAFILE);
  30. NeedFree = true;
  31. }
  32. if (Module == NULL)
  33. {
  34. }
  35. else
  36. {
  37. try
  38. {
  39. HANDLE Rsrc = FindResource(Module, MAKEINTRESOURCE(VS_VERSION_INFO),
  40. MAKEINTRESOURCE(VS_FILE_INFO));
  41. if (Rsrc == NULL)
  42. {
  43. }
  44. else
  45. {
  46. Len = SizeofResource(Module, Rsrc);
  47. HANDLE Mem = LoadResource(Module, Rsrc);
  48. if (Mem == NULL)
  49. {
  50. }
  51. else
  52. {
  53. try
  54. {
  55. VS_VERSION_INFO_STRUCT32 * VersionInfo = (VS_VERSION_INFO_STRUCT32 *)LockResource(Mem);
  56. const VS_FIXEDFILEINFO * FixedInfo =
  57. (VS_FIXEDFILEINFO *)DWORD_ALIGN(VersionInfo, VersionInfo->szKey + wcslen(VersionInfo->szKey) + 1);
  58. if (FixedInfo->dwSignature != VS_FFI_SIGNATURE)
  59. {
  60. Len = 0;
  61. }
  62. else
  63. {
  64. if (Data != NULL)
  65. {
  66. if (DataSize < Len)
  67. {
  68. Len = DataSize;
  69. }
  70. if (Len > 0)
  71. {
  72. memcpy(Data, VersionInfo, Len);
  73. }
  74. }
  75. }
  76. }
  77. __finally
  78. {
  79. FreeResource(Mem);
  80. }
  81. }
  82. }
  83. }
  84. __finally
  85. {
  86. if (NeedFree)
  87. {
  88. FreeLibrary(Module);
  89. }
  90. }
  91. }
  92. return Len;
  93. }
  94. //---------------------------------------------------------------------------
  95. unsigned int GetFileVersionInfoSizeFix(const char * FileName, unsigned long * Handle)
  96. {
  97. unsigned int Len;
  98. if (IsWin7())
  99. {
  100. *Handle = 0;
  101. Len = VERSION_GetFileVersionInfo_PE(FileName, 0, NULL);
  102. if (Len != 0)
  103. {
  104. Len = (Len * 2) + 4;
  105. }
  106. }
  107. else
  108. {
  109. Len = GetFileVersionInfoSize((char *)FileName, Handle);
  110. }
  111. return Len;
  112. }
  113. //---------------------------------------------------------------------------
  114. bool GetFileVersionInfoFix(const char * FileName, unsigned long Handle,
  115. unsigned int DataSize, void * Data)
  116. {
  117. bool Result;
  118. if (IsWin7())
  119. {
  120. VS_VERSION_INFO_STRUCT32 * VersionInfo = (VS_VERSION_INFO_STRUCT32*)Data;
  121. unsigned int Len = VERSION_GetFileVersionInfo_PE(FileName, DataSize, Data);
  122. Result = (Len != 0);
  123. if (Result)
  124. {
  125. static const char Signature[] = "FE2X";
  126. unsigned int BufSize = VersionInfo->wLength + strlen(Signature);
  127. unsigned int ConvBuf;
  128. if (DataSize >= BufSize)
  129. {
  130. ConvBuf = DataSize - VersionInfo->wLength;
  131. memcpy(((char*)(Data)) + VersionInfo->wLength, Signature, ConvBuf > 4 ? 4 : ConvBuf );
  132. }
  133. }
  134. }
  135. else
  136. {
  137. Result = GetFileVersionInfo((char *)FileName, Handle, DataSize, Data);
  138. }
  139. return Result;
  140. }
  141. //---------------------------------------------------------------------------
  142. // Return pointer to file version info block
  143. void * __fastcall CreateFileInfo(AnsiString FileName)
  144. {
  145. unsigned long Handle;
  146. unsigned int Size;
  147. void * Result = NULL;
  148. // Get file version info block size
  149. Size = GetFileVersionInfoSizeFix(FileName.c_str(), &Handle);
  150. // If size is valid
  151. if (Size > 0)
  152. {
  153. Result = new char[Size];
  154. // Get file version info block
  155. if (!GetFileVersionInfoFix(FileName.c_str(), Handle, Size, Result))
  156. {
  157. delete[] Result;
  158. Result = NULL;
  159. }
  160. }
  161. else
  162. {
  163. }
  164. return Result;
  165. }
  166. //---------------------------------------------------------------------------
  167. // Free file version info block memory
  168. void __fastcall FreeFileInfo(void * FileInfo)
  169. {
  170. delete[] FileInfo;
  171. }
  172. //---------------------------------------------------------------------------
  173. typedef TTranslation TTranslations[65536];
  174. typedef TTranslation *PTranslations;
  175. //---------------------------------------------------------------------------
  176. // Return pointer to fixed file version info
  177. PVSFixedFileInfo __fastcall GetFixedFileInfo(void * FileInfo)
  178. {
  179. UINT Len;
  180. PVSFixedFileInfo Result;
  181. if (!VerQueryValue(FileInfo, "\\", (void**)&Result, &Len))
  182. throw Exception("Fixed file info not available");
  183. return Result;
  184. };
  185. //---------------------------------------------------------------------------
  186. // Return number of available file version info translations
  187. unsigned __fastcall GetTranslationCount(void * FileInfo)
  188. {
  189. PTranslations P;
  190. UINT Len;
  191. if (!VerQueryValue(FileInfo, "\\VarFileInfo\\Translation", (void**)&P, &Len))
  192. throw Exception("File info translations not available");
  193. return Len / 4;
  194. }
  195. //---------------------------------------------------------------------------
  196. // Return i-th translation in the file version info translation list
  197. TTranslation __fastcall GetTranslation(void * FileInfo, unsigned i)
  198. {
  199. PTranslations P;
  200. UINT Len;
  201. if (!VerQueryValue(FileInfo, "\\VarFileInfo\\Translation", (void**)&P, &Len))
  202. throw Exception("File info translations not available");
  203. if (i * sizeof(TTranslation) >= Len)
  204. throw Exception("Specified translation not available");
  205. return P[i];
  206. };
  207. //---------------------------------------------------------------------------
  208. // Return the name of the specified language
  209. AnsiString __fastcall GetLanguage(Word Language)
  210. {
  211. UINT Len;
  212. Char P[256];
  213. Len = VerLanguageName(Language, P, sizeof(P));
  214. if (Len > sizeof(P))
  215. throw Exception("Language not available");
  216. return AnsiString(P, Len);
  217. };
  218. //---------------------------------------------------------------------------
  219. // Return the value of the specified file version info string using the
  220. // specified translation
  221. AnsiString __fastcall GetFileInfoString(void * FileInfo,
  222. TTranslation Translation, AnsiString StringName)
  223. {
  224. PChar P;
  225. UINT Len;
  226. if (!VerQueryValue(FileInfo, (AnsiString("\\StringFileInfo\\") +
  227. IntToHex(Translation.Language, 4) +
  228. IntToHex(Translation.CharSet, 4) +
  229. "\\" + StringName).c_str(), (void**)&P, &Len))
  230. {
  231. throw Exception("Specified file info string not available");
  232. }
  233. // c_str() makes sure that returned string has only necessary bytes allocated
  234. AnsiString Result = AnsiString(P, Len).c_str();
  235. return Result;
  236. };
  237. //---------------------------------------------------------------------------
  238. int __fastcall CalculateCompoundVersion(int MajorVer,
  239. int MinorVer, int Release, int Build)
  240. {
  241. int CompoundVer = Build + 10000 * (Release + 100 * (MinorVer +
  242. 100 * MajorVer));
  243. return CompoundVer;
  244. }