FileInfo.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. HMODULE Module = GetModuleHandle(FileName);
  26. if (Module == NULL)
  27. {
  28. Module = LoadLibraryEx(FileName, 0, LOAD_LIBRARY_AS_DATAFILE);
  29. }
  30. if (Module == NULL)
  31. {
  32. }
  33. else
  34. {
  35. try
  36. {
  37. HANDLE Rsrc = FindResource(Module, MAKEINTRESOURCE(VS_VERSION_INFO),
  38. MAKEINTRESOURCE(VS_FILE_INFO));
  39. if (Rsrc == NULL)
  40. {
  41. }
  42. else
  43. {
  44. Len = SizeofResource(Module, Rsrc);
  45. HANDLE Mem = LoadResource(Module, Rsrc);
  46. if (Mem == NULL)
  47. {
  48. }
  49. else
  50. {
  51. try
  52. {
  53. VS_VERSION_INFO_STRUCT32 * VersionInfo = (VS_VERSION_INFO_STRUCT32 *)LockResource(Mem);
  54. const VS_FIXEDFILEINFO * FixedInfo =
  55. (VS_FIXEDFILEINFO *)DWORD_ALIGN(VersionInfo, VersionInfo->szKey + wcslen(VersionInfo->szKey) + 1);
  56. if (FixedInfo->dwSignature != VS_FFI_SIGNATURE)
  57. {
  58. Len = 0;
  59. }
  60. else
  61. {
  62. if (Data != NULL)
  63. {
  64. if (DataSize < Len)
  65. {
  66. Len = DataSize;
  67. }
  68. if (Len > 0)
  69. {
  70. memcpy(Data, VersionInfo, Len);
  71. }
  72. }
  73. }
  74. }
  75. __finally
  76. {
  77. FreeResource(Mem);
  78. }
  79. }
  80. }
  81. }
  82. __finally
  83. {
  84. FreeLibrary(Module);
  85. }
  86. }
  87. return Len;
  88. }
  89. //---------------------------------------------------------------------------
  90. bool __fastcall IsWin7()
  91. {
  92. return
  93. (Win32MajorVersion > 6) ||
  94. ((Win32MajorVersion == 6) && (Win32MinorVersion >= 1));
  95. }
  96. //---------------------------------------------------------------------------
  97. unsigned int GetFileVersionInfoSizeFix(const char * 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((char *)FileName, Handle);
  112. }
  113. return Len;
  114. }
  115. //---------------------------------------------------------------------------
  116. bool GetFileVersionInfoFix(const char * 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. memcpy(((char*)(Data)) + VersionInfo->wLength, Signature, ConvBuf > 4 ? 4 : ConvBuf );
  134. }
  135. }
  136. }
  137. else
  138. {
  139. Result = GetFileVersionInfo((char *)FileName, Handle, DataSize, Data);
  140. }
  141. return Result;
  142. }
  143. //---------------------------------------------------------------------------
  144. // Return pointer to file version info block
  145. void * __fastcall CreateFileInfo(AnsiString 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, "\\", (void**)&Result, &Len))
  184. throw Exception("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, "\\VarFileInfo\\Translation", (void**)&P, &Len))
  194. throw Exception("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, "\\VarFileInfo\\Translation", (void**)&P, &Len))
  204. throw Exception("File info translations not available");
  205. if (i * sizeof(TTranslation) >= Len)
  206. throw Exception("Specified translation not available");
  207. return P[i];
  208. };
  209. //---------------------------------------------------------------------------
  210. // Return the name of the specified language
  211. AnsiString __fastcall GetLanguage(Word Language)
  212. {
  213. UINT Len;
  214. Char P[256];
  215. Len = VerLanguageName(Language, P, sizeof(P));
  216. if (Len > sizeof(P))
  217. throw Exception("Language not available");
  218. return AnsiString(P, Len);
  219. };
  220. //---------------------------------------------------------------------------
  221. // Return the value of the specified file version info string using the
  222. // specified translation
  223. AnsiString __fastcall GetFileInfoString(void * FileInfo,
  224. TTranslation Translation, AnsiString StringName)
  225. {
  226. PChar P;
  227. UINT Len;
  228. if (!VerQueryValue(FileInfo, (AnsiString("\\StringFileInfo\\") +
  229. IntToHex(Translation.Language, 4) +
  230. IntToHex(Translation.CharSet, 4) +
  231. "\\" + StringName).c_str(), (void**)&P, &Len))
  232. {
  233. throw Exception("Specified file info string not available");
  234. }
  235. // c_str() makes sure that returned string has only necessary bytes allocated
  236. AnsiString Result = AnsiString(P, Len).c_str();
  237. return Result;
  238. };
  239. //---------------------------------------------------------------------------
  240. int __fastcall CalculateCompoundVersion(int MajorVer,
  241. int MinorVer, int Release, int Build)
  242. {
  243. int CompoundVer = Build + 1000 * (Release + 100 * (MinorVer +
  244. 100 * MajorVer));
  245. return CompoundVer;
  246. }