Source code of Windows XP (NT5)
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.

323 lines
8.2 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. HoyleGames.cpp
  5. Abstract:
  6. All Hoyle apps have one common problem and that is a hard
  7. coded "C:\" in its data section of the image.The apps crash
  8. because of this if installed and run from any other drive
  9. other than C:\.
  10. This shim goes through the image of the app searching
  11. for the hardcoded string and replaces them if found. This
  12. shim replaces all the existing app specific shims for
  13. Hoyle Games.
  14. This is an app specific shim.
  15. History:
  16. 04/17/2001 Prashkud Created
  17. --*/
  18. #include "precomp.h"
  19. IMPLEMENT_SHIM_BEGIN(HoyleGames)
  20. #include "ShimHookMacro.h"
  21. APIHOOK_ENUM_BEGIN
  22. APIHOOK_ENUM_ENTRY(GetPrivateProfileStringA)
  23. APIHOOK_ENUM_END
  24. // Max Virtual address replacements in all sections
  25. #define MAX_VA 50
  26. // Global array to hold the replacement VA
  27. DWORD g_ReplaceVA[MAX_VA];
  28. // Replacement count
  29. int g_ReplaceCnt;
  30. /*++
  31. Parse the Section and fill in the location index into the
  32. SECTION structure. This function also fills in the number
  33. of occurences of the hard-coded "C:\" string in this section.
  34. --*/
  35. BOOL
  36. GetReplacementLocations(
  37. DWORD dwSecPtr,
  38. DWORD dwSize
  39. )
  40. {
  41. BYTE *pbFilePtr = (BYTE*)dwSecPtr;
  42. BOOL bRet = FALSE;
  43. for (DWORD i = 0; i < dwSize; i++)
  44. {
  45. if ((BYTE)*(pbFilePtr + i) == 'c')
  46. {
  47. if((BYTE)*(pbFilePtr + i + 1) == ':' &&
  48. (BYTE)*(pbFilePtr + i + 2) == '\\')
  49. {
  50. g_ReplaceVA[g_ReplaceCnt++] = dwSecPtr + i;
  51. bRet = TRUE;
  52. }
  53. }
  54. }
  55. return bRet;
  56. }
  57. /*++
  58. This function loops through each section looking for a Initialized Data
  59. section. Once it gets the Initialized Data section, it calls the helper
  60. function GetReplacementLocations() to get the offset from the base of the
  61. section. It then calculates the Virtual Address at which the replacement
  62. should occur.
  63. --*/
  64. BOOL
  65. GetInitializedDataSection()
  66. {
  67. PIMAGE_NT_HEADERS NtHeader;
  68. PIMAGE_FILE_HEADER FileHeader;
  69. PIMAGE_OPTIONAL_HEADER OptionalHeader;
  70. PIMAGE_SECTION_HEADER NtSection;
  71. DWORD dwSectionVA = 0, dwSize = 0;
  72. BOOL bRet = FALSE;
  73. // Get the module base address
  74. PUCHAR Base = (PUCHAR)GetModuleHandle(NULL);
  75. if ((ULONG_PTR)Base & 0x00000001)
  76. {
  77. Base = (PUCHAR)((ULONG_PTR)Base & ~0x1);
  78. }
  79. NtHeader = RtlpImageNtHeader(Base);
  80. if (NtHeader)
  81. {
  82. FileHeader = &NtHeader->FileHeader;
  83. OptionalHeader = &NtHeader->OptionalHeader;
  84. }
  85. else
  86. {
  87. // Handle case where Image passed in doesn't have a dos stub (ROM images for instance);
  88. FileHeader = (PIMAGE_FILE_HEADER)Base;
  89. OptionalHeader = (PIMAGE_OPTIONAL_HEADER) ((ULONG_PTR)Base + IMAGE_SIZEOF_FILE_HEADER);
  90. }
  91. NtSection = (PIMAGE_SECTION_HEADER)((ULONG_PTR)OptionalHeader +
  92. FileHeader->SizeOfOptionalHeader);
  93. for (DWORD i=0; i<FileHeader->NumberOfSections; i++)
  94. {
  95. // Check whether the section is a Initialized Data Section
  96. if (NtSection->Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)
  97. {
  98. // Size of the Section to search
  99. dwSize = NtSection->SizeOfRawData;
  100. // Get the Section's Virtual address
  101. dwSectionVA = (DWORD)(Base + NtSection->VirtualAddress);
  102. __try
  103. {
  104. if(GetReplacementLocations(dwSectionVA, dwSize))
  105. {
  106. bRet = TRUE;
  107. }
  108. DPFN( eDbgLevelError, "Replacing was successful");
  109. }
  110. __except(EXCEPTION_EXECUTE_HANDLER)
  111. {
  112. DPFN( eDbgLevelError, "Replacing crashed");
  113. goto Exit;
  114. }
  115. }
  116. ++NtSection;
  117. }
  118. return bRet;
  119. Exit:
  120. return FALSE;
  121. }
  122. /*++
  123. Very specific hack to return a good FaceMaker path, so the app doesn't fail
  124. when it is installed on the wrong drive.
  125. --*/
  126. DWORD
  127. APIHOOK(GetPrivateProfileStringA)(
  128. LPCSTR lpAppName,
  129. LPCSTR lpKeyName,
  130. LPCSTR lpDefault,
  131. LPSTR lpReturnedString,
  132. DWORD nSize,
  133. LPCSTR lpFileName
  134. )
  135. {
  136. CSTRING_TRY
  137. {
  138. CString csApp = lpAppName;
  139. CString csKey = lpKeyName;
  140. CString csFile = lpFileName;
  141. if ((csApp.Compare(L"Settings") == 0) &&
  142. (csKey.Compare(L"FaceMakerPath") == 0) &&
  143. (csFile.Find(L"CARDGAME.INI") > -1)) {
  144. DWORD dwRet = ORIGINAL_API(GetPrivateProfileStringA)(lpAppName, lpKeyName,
  145. lpDefault, lpReturnedString, nSize, lpFileName);
  146. if (!dwRet) {
  147. // Substitute the right path
  148. CString csPath = L"%ProgramFiles%\\WON\\FaceMaker";
  149. csPath.ExpandEnvironmentStringsW();
  150. if (lpReturnedString && ((int)nSize > csPath.GetLength())) {
  151. LOGN(eDbgLevelError, "[GetPrivateProfileStringA] Forced correct FaceMaker path");
  152. strncpy(lpReturnedString, csPath.GetAnsi(), nSize);
  153. dwRet = csPath.GetLength();
  154. }
  155. }
  156. return dwRet;
  157. }
  158. }
  159. CSTRING_CATCH
  160. {
  161. // fall through
  162. }
  163. return ORIGINAL_API(GetPrivateProfileStringA)(lpAppName, lpKeyName,
  164. lpDefault, lpReturnedString, nSize, lpFileName);
  165. }
  166. /*++
  167. This function hooks GetVersion (called early on by Hoyle Board Games)
  168. and replaces the hard coded 'c's with the correct install drive letter
  169. that it looks up in the registry.
  170. It uses g_HoyleWordGames_bPatched to patch only once.
  171. --*/
  172. BOOL
  173. NOTIFY_FUNCTION(
  174. DWORD fdwReason
  175. )
  176. {
  177. if (fdwReason == SHIM_STATIC_DLLS_INITIALIZED)
  178. {
  179. CHAR szInstallDir[MAX_PATH];
  180. CHAR szProgFilesDir[MAX_PATH]; // Added by Noah Young on 1/26/01
  181. DWORD cb = MAX_PATH;
  182. HKEY hKey = 0;
  183. DWORD dwOldProtect = 0;
  184. // Fix problem where Program Files dir isn't on same drive as BOARD3.EXE
  185. if( ERROR_SUCCESS != RegOpenKeyExA(HKEY_LOCAL_MACHINE,
  186. "SOFTWARE\\Microsoft\\Windows\\CurrentVersion",
  187. 0,
  188. KEY_QUERY_VALUE,
  189. &hKey) )
  190. {
  191. goto exit;
  192. }
  193. if( ERROR_SUCCESS != RegQueryValueExA(hKey,
  194. "ProgramFilesDir",
  195. NULL,
  196. NULL, // REG_SZ
  197. (LPBYTE)szProgFilesDir,
  198. &cb) )
  199. {
  200. goto exit;
  201. }
  202. // Scan the image's initialized data section....
  203. char szModule[MAX_PATH];
  204. if(!GetModuleFileNameA(NULL, szModule, MAX_PATH))
  205. {
  206. DPFN( eDbgLevelError, "GetModuleFileA returned error");
  207. goto exit;
  208. }
  209. // Get the Virtual adresses that need to be replaced
  210. if(!GetInitializedDataSection())
  211. {
  212. DPFN( eDbgLevelError, "No patching done!");
  213. goto exit;
  214. }
  215. long PATCH_LENGTH = g_ReplaceVA[ g_ReplaceCnt - 1] - g_ReplaceVA[0] + 1;
  216. // Make the memory page writable
  217. if( VirtualProtect( (PVOID) g_ReplaceVA[0],
  218. PATCH_LENGTH,
  219. PAGE_READWRITE,
  220. &dwOldProtect ) )
  221. {
  222. for (int i=0; i< g_ReplaceCnt; i++)
  223. {
  224. // Make sure it's what we expect
  225. if( 'c' == *((CHAR*) g_ReplaceVA[i]) )
  226. {
  227. if (i==0)
  228. {
  229. *((CHAR*) g_ReplaceVA[i]) = szProgFilesDir[0];
  230. }
  231. else
  232. {
  233. *((CHAR*) g_ReplaceVA[i]) = szModule[0];
  234. }
  235. }
  236. }
  237. }
  238. }
  239. exit:
  240. return TRUE;
  241. }
  242. /*++
  243. Register hooked functions
  244. --*/
  245. HOOK_BEGIN
  246. CALL_NOTIFY_FUNCTION
  247. APIHOOK_ENTRY(KERNEL32.DLL, GetPrivateProfileStringA)
  248. HOOK_END
  249. IMPLEMENT_SHIM_END