FileInfo.cpp 8.4 KB

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