FileInfo.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 wchar_t * 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. Len = 0;
  35. }
  36. else
  37. {
  38. try
  39. {
  40. HRSRC Rsrc = FindResource(Module, MAKEINTRESOURCE(VS_VERSION_INFO),
  41. MAKEINTRESOURCE(VS_FILE_INFO));
  42. if (Rsrc == NULL)
  43. {
  44. }
  45. else
  46. {
  47. Len = SizeofResource(Module, Rsrc);
  48. HANDLE Mem = LoadResource(Module, Rsrc);
  49. if (Mem == NULL)
  50. {
  51. }
  52. else
  53. {
  54. try
  55. {
  56. VS_VERSION_INFO_STRUCT32 * VersionInfo = (VS_VERSION_INFO_STRUCT32 *)LockResource(Mem);
  57. const VS_FIXEDFILEINFO * FixedInfo =
  58. (VS_FIXEDFILEINFO *)DWORD_ALIGN(VersionInfo, VersionInfo->szKey + wcslen(VersionInfo->szKey) + 1);
  59. if (FixedInfo->dwSignature != VS_FFI_SIGNATURE)
  60. {
  61. Len = 0;
  62. }
  63. else
  64. {
  65. if (Data != NULL)
  66. {
  67. if (DataSize < Len)
  68. {
  69. Len = DataSize;
  70. }
  71. if (Len > 0)
  72. {
  73. memmove(Data, VersionInfo, Len);
  74. }
  75. }
  76. }
  77. }
  78. __finally
  79. {
  80. FreeResource(Mem);
  81. }
  82. }
  83. }
  84. }
  85. __finally
  86. {
  87. if (NeedFree)
  88. {
  89. FreeLibrary(Module);
  90. }
  91. }
  92. }
  93. return Len;
  94. }
  95. //---------------------------------------------------------------------------
  96. unsigned int GetFileVersionInfoSizeFix(const wchar_t * FileName, unsigned long * Handle)
  97. {
  98. unsigned int Len;
  99. if (IsWin7())
  100. {
  101. *Handle = 0;
  102. Len = VERSION_GetFileVersionInfo_PE(FileName, 0, NULL);
  103. if (Len != 0)
  104. {
  105. Len = (Len * 2) + 4;
  106. }
  107. }
  108. else
  109. {
  110. Len = GetFileVersionInfoSize(FileName, Handle);
  111. }
  112. return Len;
  113. }
  114. //---------------------------------------------------------------------------
  115. bool GetFileVersionInfoFix(const wchar_t * FileName, unsigned long Handle,
  116. unsigned int DataSize, void * Data)
  117. {
  118. bool Result;
  119. if (IsWin7())
  120. {
  121. VS_VERSION_INFO_STRUCT32 * VersionInfo = (VS_VERSION_INFO_STRUCT32*)Data;
  122. unsigned int Len = VERSION_GetFileVersionInfo_PE(FileName, DataSize, Data);
  123. Result = (Len != 0);
  124. if (Result)
  125. {
  126. static const char Signature[] = "FE2X";
  127. unsigned int BufSize = VersionInfo->wLength + strlen(Signature);
  128. unsigned int ConvBuf;
  129. if (DataSize >= BufSize)
  130. {
  131. ConvBuf = DataSize - VersionInfo->wLength;
  132. memmove(((char*)(Data)) + VersionInfo->wLength, Signature, ConvBuf > 4 ? 4 : ConvBuf );
  133. }
  134. }
  135. }
  136. else
  137. {
  138. Result = GetFileVersionInfo(FileName, Handle, DataSize, Data);
  139. }
  140. return Result;
  141. }
  142. //---------------------------------------------------------------------------
  143. // Return pointer to file version info block
  144. void * __fastcall CreateFileInfo(UnicodeString FileName)
  145. {
  146. unsigned long Handle;
  147. unsigned int Size;
  148. void * Result = NULL;
  149. // Get file version info block size
  150. Size = GetFileVersionInfoSizeFix(FileName.c_str(), &Handle);
  151. // If size is valid
  152. if (Size > 0)
  153. {
  154. Result = new char[Size];
  155. // Get file version info block
  156. if (!GetFileVersionInfoFix(FileName.c_str(), Handle, Size, Result))
  157. {
  158. delete[] Result;
  159. Result = NULL;
  160. }
  161. }
  162. else
  163. {
  164. }
  165. return Result;
  166. }
  167. //---------------------------------------------------------------------------
  168. // Free file version info block memory
  169. void __fastcall FreeFileInfo(void * FileInfo)
  170. {
  171. delete[] FileInfo;
  172. }
  173. //---------------------------------------------------------------------------
  174. typedef TTranslation TTranslations[65536];
  175. typedef TTranslation *PTranslations;
  176. //---------------------------------------------------------------------------
  177. // Return pointer to fixed file version info
  178. PVSFixedFileInfo __fastcall GetFixedFileInfo(void * FileInfo)
  179. {
  180. UINT Len;
  181. PVSFixedFileInfo Result;
  182. if (!VerQueryValue(FileInfo, L"\\", (void**)&Result, &Len))
  183. throw Exception(L"Fixed file info not available");
  184. return Result;
  185. }
  186. //---------------------------------------------------------------------------
  187. // Return number of available file version info translations
  188. unsigned __fastcall GetTranslationCount(void * FileInfo)
  189. {
  190. PTranslations P;
  191. UINT Len;
  192. if (!VerQueryValue(FileInfo, L"\\VarFileInfo\\Translation", (void**)&P, &Len))
  193. throw Exception(L"File info translations not available");
  194. return Len / 4;
  195. }
  196. //---------------------------------------------------------------------------
  197. // Return i-th translation in the file version info translation list
  198. TTranslation __fastcall GetTranslation(void * FileInfo, unsigned i)
  199. {
  200. PTranslations P;
  201. UINT Len;
  202. if (!VerQueryValue(FileInfo, L"\\VarFileInfo\\Translation", (void**)&P, &Len))
  203. throw Exception(L"File info translations not available");
  204. if (i * sizeof(TTranslation) >= Len)
  205. throw Exception(L"Specified translation not available");
  206. return P[i];
  207. }
  208. //---------------------------------------------------------------------------
  209. // Return the name of the specified language
  210. UnicodeString __fastcall GetLanguage(Word Language)
  211. {
  212. UINT Len;
  213. wchar_t P[256];
  214. Len = VerLanguageName(Language, P, LENOF(P));
  215. if (Len > LENOF(P))
  216. throw Exception(L"Language not available");
  217. return UnicodeString(P, Len);
  218. }
  219. //---------------------------------------------------------------------------
  220. // Return the value of the specified file version info string using the
  221. // specified translation
  222. UnicodeString __fastcall GetFileInfoString(void * FileInfo,
  223. TTranslation Translation, UnicodeString StringName, bool AllowEmpty)
  224. {
  225. UnicodeString Result;
  226. wchar_t * P;
  227. UINT Len;
  228. if (!VerQueryValue(FileInfo, (UnicodeString(L"\\StringFileInfo\\") +
  229. IntToHex(Translation.Language, 4) +
  230. IntToHex(Translation.CharSet, 4) +
  231. L"\\" + StringName).c_str(), (void**)&P, &Len))
  232. {
  233. if (!AllowEmpty)
  234. {
  235. throw Exception("Specified file info string not available");
  236. }
  237. }
  238. else
  239. {
  240. Result = UnicodeString(P, Len);
  241. PackStr(Result);
  242. }
  243. return Result;
  244. }
  245. //---------------------------------------------------------------------------
  246. int __fastcall CalculateCompoundVersion(int MajorVer,
  247. int MinorVer, int Release, int Build)
  248. {
  249. int CompoundVer = Build + 10000 * (Release + 100 * (MinorVer +
  250. 100 * MajorVer));
  251. return CompoundVer;
  252. }
  253. //---------------------------------------------------------------------------
  254. int __fastcall StrToCompoundVersion(UnicodeString S)
  255. {
  256. int MajorVer = StrToInt(CutToChar(S, L'.', false));
  257. int MinorVer = StrToInt(CutToChar(S, L'.', false));
  258. int Release = S.IsEmpty() ? 0 : StrToInt(CutToChar(S, L'.', false));
  259. int Build = S.IsEmpty() ? 0 : StrToInt(CutToChar(S, L'.', false));
  260. return CalculateCompoundVersion(MajorVer, MinorVer, Release, Build);
  261. }