Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

358 lines
12 KiB

  1. /*++
  2. Copyright (c) 2000-2002 Microsoft Corporation
  3. Module Name:
  4. FileVersionInfoLie.cpp
  5. Abstract:
  6. This shim replaces the info returned from calls to GetFileVersionInfoSize and
  7. GetFileVersionInfo with information stored in resource files. The default
  8. is to replace file info with stored info obtained from DirectX ver 7a.
  9. This can be overridden with command line input. For example:
  10. COMMAND_LINE("D3drgbxf.dll,IDR_D3DRGBXFINFO;dsound.vxd,IDR_DSOUNDVXDINFO")
  11. this would intercept the info calls for D3drgbxf.dll and dsounc.vxd and replace
  12. their info with the info stored in the resources named. Note: All spaces within
  13. the command line are considered part of the filename or resource name, only the
  14. commas and semicolons are delimeters.
  15. Notes:
  16. This is a general purpose shim.
  17. History:
  18. 01/03/2000 a-jamd Created
  19. 03/28/2000 a-jamd Added resource for ddraw16.dll
  20. 04/04/2000 a-michni Added resource for D3drgbxf.dll
  21. 04/07/2000 linstev Added resource for dsound.vxd
  22. 04/10/2000 markder Removed GetModuleHandle("_DDRAW6V") calls -- use g_hinstDll
  23. 04/18/2000 a-michni Modified DDraw6Versionlie to be command line input driven and
  24. renamed to FileVersionInfoLie
  25. 04/26/2000 a-batjar GetFileVersionInfo should return truncated result if passed
  26. in buffer size is smaller than infosize
  27. 07/19/2000 andyseti Added resource for shdocvw.bin
  28. 08/11/2000 a-brienw changed g_nNumDirectX6 to 7 and added entry for dsound.dll
  29. made it the same as dsound.vxd
  30. 08/15/2000 a-vales Added resource for dsound.dll
  31. 11/08/2000 a-brienw changed dsound.dll entry to return dsound.vxd version info.
  32. a-vales changed it from my previous entry which caused MAX2
  33. to no longer work. I changed it back and checked it with
  34. MAX2 and his app Golden Nugget and it is fine with both.
  35. 12/06/2000 mnikkel Added resources for all directx7a dlls and also for any
  36. dlls that existed in previous versions of directx but
  37. were deleted. NOTE: the resources for these files are
  38. in win98 format so that apps which directly read the version
  39. info will receive them in the way they are expecting.
  40. 02/18/2002 mnikkel Added check to make sure a null pointer was not passed into GetFileVersionInfoSizeA.
  41. Added check to make sure a null pointer was not passed into GetFileVersionInfoA.
  42. --*/
  43. #include "precomp.h"
  44. IMPLEMENT_SHIM_BEGIN(FileVersionInfoLie)
  45. #include "ShimHookMacro.h"
  46. APIHOOK_ENUM_BEGIN
  47. APIHOOK_ENUM_ENTRY(GetFileVersionInfoA)
  48. APIHOOK_ENUM_ENTRY(GetFileVersionInfoSizeA)
  49. APIHOOK_ENUM_END
  50. // Keep a list of files to version lie.
  51. struct LIELIST
  52. {
  53. struct LIELIST * next;
  54. CString szFileName;
  55. CString szResource;
  56. };
  57. LIELIST *g_pLieList = NULL;
  58. // DirectX 7a default files go here.
  59. const INT g_nNumDirectX7a = 67;
  60. WCHAR *g_szDirectX7aFiles[g_nNumDirectX7a] =
  61. { L"dplay.dll", L"d3dim.dll", L"d3dim700.dll",
  62. L"d3dpmesh.dll", L"d3dramp.dll", L"d3drampf.dll",
  63. L"d3dref.dll", L"d3drg16f.dll", L"d3drg24f.dll",
  64. L"d3drg24x.dll", L"d3dhalf.dll", L"d3drg32f.dll",
  65. L"d3drg32x.dll", L"d3drg55x.dll", L"d3drg56x.dll",
  66. L"d3drg8f.dll", L"d3drg8x.dll", L"d3drgbf.dll",
  67. L"d3drgbxf.dll", L"d3drm.dll", L"d3drm16f.dll",
  68. L"d3drm24f.dll", L"d3drm32f.dll", L"d3drm8f.dll",
  69. L"d3dxof.dll", L"ddhelp.exe", L"ddraw.dll",
  70. L"ddraw16.dll", L"ddrawex.dll", L"devnode1.dll",
  71. L"devnode2.dll", L"dinput.dll", L"dmband.dll",
  72. L"dmcompos.dll", L"dmime.dll", L"dmloader.dll",
  73. L"dmstyle.dll", L"dmsynth.dll", L"dmusic.dll",
  74. L"dmusic16.dll", L"dmusic32.dll", L"dplayx.dll",
  75. L"dpmodemx.dll", L"dpserial.dll", L"dpwsock.dll",
  76. L"dpwsockx.dll", L"dsetup.dll", L"dsetup16.dll",
  77. L"dsetup32.dll", L"dsetup6e.dll", L"dsetup6j.dll",
  78. L"dsetupe.dll", L"dsetupj.dll", L"dsound.dll",
  79. L"dsound3d.dll", L"dx7vb.dll", L"dxmigr.dll",
  80. L"gcdef.dll", L"gchand.dll", L"msvcrt.dll",
  81. L"pid.dll", L"vjoyd.vxd", L"dinput.vxd",
  82. L"dsound.vxd", L"joyhid.vxd", L"mtrr.vxd",
  83. L"ddraw.vxd"
  84. };
  85. // NOTE: These are 16 bit resources!!! This is necessary in case
  86. // they index into the data themselves. If they do a verqueryvalue
  87. // the data is converted before its returned by verqueryvalue.
  88. WCHAR * g_szDirectX7aResource[g_nNumDirectX7a] =
  89. { L"IDR_dplay", L"IDR_d3dim", L"IDR_d3dim700",
  90. L"IDR_d3dpmesh", L"IDR_d3dramp", L"IDR_d3drampf",
  91. L"IDR_d3dref", L"IDR_d3drg16f", L"IDR_d3drg24f",
  92. L"IDR_d3drg24x", L"IDR_d3dhalf", L"IDR_d3drg32f",
  93. L"IDR_d3drg32x", L"IDR_d3drg55x", L"IDR_d3drg56x",
  94. L"IDR_d3drg8f", L"IDR_d3drg8x", L"IDR_d3drgbf",
  95. L"IDR_d3drgbxf", L"IDR_d3drm", L"IDR_d3drm16f",
  96. L"IDR_d3drm24f", L"IDR_d3drm32f", L"IDR_d3drm8f",
  97. L"IDR_d3dxof", L"IDR_ddhelp", L"IDR_ddraw",
  98. L"IDR_ddraw16", L"IDR_ddrawex", L"IDR_devnode1",
  99. L"IDR_devnode2", L"IDR_dinput", L"IDR_dmband",
  100. L"IDR_dmcompos", L"IDR_dmime", L"IDR_dmloader",
  101. L"IDR_dmstyle", L"IDR_dmsynth", L"IDR_dmusic",
  102. L"IDR_dmusic16", L"IDR_dmusic32", L"IDR_dplayx",
  103. L"IDR_dpmodemx", L"IDR_dpserial", L"IDR_dpwsock",
  104. L"IDR_dpwsockx", L"IDR_dsetup", L"IDR_dsetup16",
  105. L"IDR_dsetup32", L"IDR_dsetup6e", L"IDR_dsetup6j",
  106. L"IDR_dsetupe", L"IDR_dsetupj", L"IDR_dsound",
  107. L"IDR_dsound3d", L"IDR_dx7vb", L"IDR_dxmigr",
  108. L"IDR_gcdef", L"IDR_gchand", L"IDR_msvcrt",
  109. L"IDR_pid", L"IDR_vjoydvxd", L"IDR_dinputvxd",
  110. L"IDR_dsoundvxd", L"IDR_joyhidvxd", L"IDR_mtrrvxd",
  111. L"IDR_ddrawvxd"
  112. };
  113. /*++
  114. return the size from the resource.
  115. --*/
  116. DWORD
  117. APIHOOK(GetFileVersionInfoSizeA)(
  118. LPSTR lpstrFilename,
  119. LPDWORD lpdwHandle
  120. )
  121. {
  122. DWORD dwRet = 0;
  123. CSTRING_TRY
  124. {
  125. HRSRC hrsrcManifest = NULL;
  126. LIELIST *pLiePtr = g_pLieList;
  127. DPFN( eDbgLevelSpew, "[GetFileVersionInfoSizeA] size requested for %s\n", lpstrFilename );
  128. CString csFileName(lpstrFilename);
  129. CString csFilePart;
  130. csFileName.GetLastPathComponent(csFilePart);
  131. // Search through the list of files with their matching IDR's
  132. while( pLiePtr )
  133. {
  134. if (csFilePart.CompareNoCase(pLiePtr->szFileName) == 0)
  135. {
  136. hrsrcManifest = FindResourceW( g_hinstDll, pLiePtr->szResource, L"FILES");
  137. break;
  138. }
  139. pLiePtr = pLiePtr->next;
  140. }
  141. // If a match was found, get the resource size
  142. if( hrsrcManifest )
  143. {
  144. dwRet = SizeofResource(g_hinstDll, hrsrcManifest);
  145. if (lpdwHandle)
  146. {
  147. *lpdwHandle = 0;
  148. }
  149. }
  150. }
  151. CSTRING_CATCH
  152. {
  153. // Do nothing
  154. }
  155. if (dwRet == 0)
  156. {
  157. dwRet = ORIGINAL_API(GetFileVersionInfoSizeA)(lpstrFilename, lpdwHandle);
  158. }
  159. return dwRet;
  160. }
  161. /*++
  162. Return the version for the modules that shipped with Win98SE.
  163. --*/
  164. BOOL
  165. APIHOOK(GetFileVersionInfoA)(
  166. LPSTR lpstrFilename,
  167. DWORD dwHandle,
  168. DWORD dwLen,
  169. LPVOID lpData
  170. )
  171. {
  172. BOOL bRet = FALSE;
  173. CSTRING_TRY
  174. {
  175. HRSRC hrsrcManifest = NULL;
  176. LIELIST *pLiePtr = g_pLieList;
  177. DPFN( eDbgLevelSpew, "[GetFileVersionInfoA] info requested for %s\n", lpstrFilename );
  178. CString csFileName(lpstrFilename);
  179. CString csFilePart;
  180. csFileName.GetLastPathComponent(csFilePart);
  181. // Search through the list of files with their matching IDR's
  182. while( pLiePtr )
  183. {
  184. if (csFilePart.CompareNoCase(pLiePtr->szFileName) == 0)
  185. {
  186. hrsrcManifest = FindResourceW( g_hinstDll, pLiePtr->szResource, L"FILES");
  187. break;
  188. }
  189. pLiePtr = pLiePtr->next;
  190. }
  191. // If a match was found, get the resource size
  192. if( hrsrcManifest )
  193. {
  194. LOGN( eDbgLevelError, "[GetFileVersionInfoA] Getting legacy version for %s.", lpstrFilename);
  195. DWORD dwManifestSize = SizeofResource(g_hinstDll, hrsrcManifest);
  196. HGLOBAL hManifestMem = LoadResource (g_hinstDll, hrsrcManifest);
  197. PVOID lpManifestMem = LockResource (hManifestMem);
  198. if (lpManifestMem)
  199. {
  200. memcpy(lpData, lpManifestMem, dwLen >= dwManifestSize ? dwManifestSize:dwLen );
  201. bRet = TRUE;
  202. }
  203. }
  204. }
  205. CSTRING_CATCH
  206. {
  207. // Do nothing
  208. }
  209. if (!bRet)
  210. {
  211. bRet = ORIGINAL_API(GetFileVersionInfoA)(
  212. lpstrFilename,
  213. dwHandle,
  214. dwLen,
  215. lpData);
  216. }
  217. return bRet;
  218. }
  219. /*++
  220. Parse the command line inputs.
  221. --*/
  222. BOOL ParseCommandLine(const char * commandLine)
  223. {
  224. CSTRING_TRY
  225. {
  226. CString csCmdLine(commandLine);
  227. // if there are no command line inputs then default to
  228. // the DirectX 7a files needed.
  229. if (csCmdLine.IsEmpty())
  230. {
  231. DPFN( eDbgLevelSpew, "Defaulting to DirectX7a\n" );
  232. for(int i = 0; i < g_nNumDirectX7a; i++)
  233. {
  234. LIELIST * pLiePtr = new LIELIST;
  235. if (pLiePtr == NULL)
  236. {
  237. DPFN( eDbgLevelSpew, "Out of Memory when trying to allocate list." );
  238. return FALSE;
  239. }
  240. pLiePtr->szFileName = g_szDirectX7aFiles[i];
  241. pLiePtr->szResource = g_szDirectX7aResource[i];
  242. pLiePtr->next = g_pLieList;
  243. g_pLieList = pLiePtr;
  244. }
  245. }
  246. else
  247. {
  248. CStringToken csTokenList(csCmdLine, L";");
  249. CString csEntryTok;
  250. while (csTokenList.GetToken(csEntryTok))
  251. {
  252. CStringToken csEntry(csEntryTok, L",");
  253. CString csLeft;
  254. CString csRight;
  255. csEntry.GetToken(csLeft);
  256. csEntry.GetToken(csRight);
  257. if (!csLeft.IsEmpty() && !csRight.IsEmpty())
  258. {
  259. LIELIST * pLiePtr = new LIELIST;
  260. pLiePtr->szFileName = csLeft;
  261. pLiePtr->szResource = csRight;
  262. pLiePtr->next = g_pLieList;
  263. g_pLieList = pLiePtr;
  264. }
  265. }
  266. }
  267. }
  268. CSTRING_CATCH
  269. {
  270. return FALSE;
  271. }
  272. return TRUE;
  273. }
  274. /*++
  275. Register hooked functions
  276. --*/
  277. BOOL
  278. NOTIFY_FUNCTION(
  279. DWORD fdwReason
  280. )
  281. {
  282. if (fdwReason == DLL_PROCESS_ATTACH)
  283. {
  284. return ParseCommandLine(COMMAND_LINE);
  285. }
  286. return TRUE;
  287. }
  288. HOOK_BEGIN
  289. CALL_NOTIFY_FUNCTION
  290. APIHOOK_ENTRY(VERSION.DLL, GetFileVersionInfoA)
  291. APIHOOK_ENTRY(VERSION.DLL, GetFileVersionInfoSizeA)
  292. HOOK_END
  293. IMPLEMENT_SHIM_END