FileInfo.cpp 8.4 KB

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