FileInfo.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. bool __fastcall IsWin7()
  96. {
  97. return
  98. (Win32MajorVersion > 6) ||
  99. ((Win32MajorVersion == 6) && (Win32MinorVersion >= 1));
  100. }
  101. //---------------------------------------------------------------------------
  102. unsigned int GetFileVersionInfoSizeFix(const char * FileName, unsigned long * Handle)
  103. {
  104. unsigned int Len;
  105. if (IsWin7())
  106. {
  107. *Handle = 0;
  108. Len = VERSION_GetFileVersionInfo_PE(FileName, 0, NULL);
  109. if (Len != 0)
  110. {
  111. Len = (Len * 2) + 4;
  112. }
  113. }
  114. else
  115. {
  116. Len = GetFileVersionInfoSize((char *)FileName, Handle);
  117. }
  118. return Len;
  119. }
  120. //---------------------------------------------------------------------------
  121. bool GetFileVersionInfoFix(const char * FileName, unsigned long Handle,
  122. unsigned int DataSize, void * Data)
  123. {
  124. bool Result;
  125. if (IsWin7())
  126. {
  127. VS_VERSION_INFO_STRUCT32 * VersionInfo = (VS_VERSION_INFO_STRUCT32*)Data;
  128. unsigned int Len = VERSION_GetFileVersionInfo_PE(FileName, DataSize, Data);
  129. Result = (Len != 0);
  130. if (Result)
  131. {
  132. static const char Signature[] = "FE2X";
  133. unsigned int BufSize = VersionInfo->wLength + strlen(Signature);
  134. unsigned int ConvBuf;
  135. if (DataSize >= BufSize)
  136. {
  137. ConvBuf = DataSize - VersionInfo->wLength;
  138. memcpy(((char*)(Data)) + VersionInfo->wLength, Signature, ConvBuf > 4 ? 4 : ConvBuf );
  139. }
  140. }
  141. }
  142. else
  143. {
  144. Result = GetFileVersionInfo((char *)FileName, Handle, DataSize, Data);
  145. }
  146. return Result;
  147. }
  148. //---------------------------------------------------------------------------
  149. // Return pointer to file version info block
  150. void * __fastcall CreateFileInfo(AnsiString FileName)
  151. {
  152. unsigned long Handle;
  153. unsigned int Size;
  154. void * Result = NULL;
  155. // Get file version info block size
  156. Size = GetFileVersionInfoSizeFix(FileName.c_str(), &Handle);
  157. // If size is valid
  158. if (Size > 0)
  159. {
  160. Result = new char[Size];
  161. // Get file version info block
  162. if (!GetFileVersionInfoFix(FileName.c_str(), Handle, Size, Result))
  163. {
  164. delete[] Result;
  165. Result = NULL;
  166. }
  167. }
  168. else
  169. {
  170. }
  171. return Result;
  172. }
  173. //---------------------------------------------------------------------------
  174. // Free file version info block memory
  175. void __fastcall FreeFileInfo(void * FileInfo)
  176. {
  177. delete[] FileInfo;
  178. }
  179. //---------------------------------------------------------------------------
  180. typedef TTranslation TTranslations[65536];
  181. typedef TTranslation *PTranslations;
  182. //---------------------------------------------------------------------------
  183. // Return pointer to fixed file version info
  184. PVSFixedFileInfo __fastcall GetFixedFileInfo(void * FileInfo)
  185. {
  186. UINT Len;
  187. PVSFixedFileInfo Result;
  188. if (!VerQueryValue(FileInfo, "\\", (void**)&Result, &Len))
  189. throw Exception("Fixed file info not available");
  190. return Result;
  191. };
  192. //---------------------------------------------------------------------------
  193. // Return number of available file version info translations
  194. unsigned __fastcall GetTranslationCount(void * FileInfo)
  195. {
  196. PTranslations P;
  197. UINT Len;
  198. if (!VerQueryValue(FileInfo, "\\VarFileInfo\\Translation", (void**)&P, &Len))
  199. throw Exception("File info translations not available");
  200. return Len / 4;
  201. }
  202. //---------------------------------------------------------------------------
  203. // Return i-th translation in the file version info translation list
  204. TTranslation __fastcall GetTranslation(void * FileInfo, unsigned i)
  205. {
  206. PTranslations P;
  207. UINT Len;
  208. if (!VerQueryValue(FileInfo, "\\VarFileInfo\\Translation", (void**)&P, &Len))
  209. throw Exception("File info translations not available");
  210. if (i * sizeof(TTranslation) >= Len)
  211. throw Exception("Specified translation not available");
  212. return P[i];
  213. };
  214. //---------------------------------------------------------------------------
  215. // Return the name of the specified language
  216. AnsiString __fastcall GetLanguage(Word Language)
  217. {
  218. UINT Len;
  219. Char P[256];
  220. Len = VerLanguageName(Language, P, sizeof(P));
  221. if (Len > sizeof(P))
  222. throw Exception("Language not available");
  223. return AnsiString(P, Len);
  224. };
  225. //---------------------------------------------------------------------------
  226. // Return the value of the specified file version info string using the
  227. // specified translation
  228. AnsiString __fastcall GetFileInfoString(void * FileInfo,
  229. TTranslation Translation, AnsiString StringName)
  230. {
  231. PChar P;
  232. UINT Len;
  233. if (!VerQueryValue(FileInfo, (AnsiString("\\StringFileInfo\\") +
  234. IntToHex(Translation.Language, 4) +
  235. IntToHex(Translation.CharSet, 4) +
  236. "\\" + StringName).c_str(), (void**)&P, &Len))
  237. {
  238. throw Exception("Specified file info string not available");
  239. }
  240. // c_str() makes sure that returned string has only necessary bytes allocated
  241. AnsiString Result = AnsiString(P, Len).c_str();
  242. return Result;
  243. };
  244. //---------------------------------------------------------------------------
  245. int __fastcall CalculateCompoundVersion(int MajorVer,
  246. int MinorVer, int Release, int Build)
  247. {
  248. int CompoundVer = Build + 1000 * (Release + 100 * (MinorVer +
  249. 100 * MajorVer));
  250. return CompoundVer;
  251. }