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.

228 lines
5.1 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. EmulateCreateFileMapping.cpp
  5. Abstract:
  6. Win9x defaults to SEC_COMMIT when the SEC_NOCACHE flag is set.
  7. NT fails the call.
  8. File mapping names, can't contain backslashes.
  9. Notes:
  10. This is a general purpose shim.
  11. History:
  12. 02/17/2000 linstev Created
  13. 05/26/2001 robkenny Replace all \ to _ in map names.
  14. --*/
  15. #include "precomp.h"
  16. IMPLEMENT_SHIM_BEGIN(EmulateCreateFileMapping)
  17. #include "ShimHookMacro.h"
  18. APIHOOK_ENUM_BEGIN
  19. APIHOOK_ENUM_ENTRY(CreateFileMappingA)
  20. APIHOOK_ENUM_ENTRY(CreateFileMappingW)
  21. APIHOOK_ENUM_ENTRY(OpenFileMappingA)
  22. APIHOOK_ENUM_ENTRY(OpenFileMappingW)
  23. APIHOOK_ENUM_END
  24. /*++
  25. Correct the flag and name
  26. --*/
  27. HANDLE
  28. APIHOOK(CreateFileMappingW)(
  29. HANDLE hFile,
  30. LPSECURITY_ATTRIBUTES lpAttributes,
  31. DWORD flProtect,
  32. DWORD dwMaximumSizeHigh,
  33. DWORD dwMaximumSizeLow,
  34. LPCWSTR lpName
  35. )
  36. {
  37. HANDLE hRet = ORIGINAL_API(CreateFileMappingW)(hFile, lpAttributes,
  38. flProtect, dwMaximumSizeHigh, dwMaximumSizeLow, lpName);
  39. if (!hRet) {
  40. //
  41. // The call failed, so try correcting the parameters
  42. //
  43. DWORD flNewProtect = flProtect;
  44. if ((flProtect & SEC_NOCACHE) &&
  45. (!((flProtect & SEC_COMMIT) || (flProtect & SEC_RESERVE)))) {
  46. // Add the SEC_COMMIT flag
  47. flNewProtect |= SEC_COMMIT;
  48. }
  49. CSTRING_TRY {
  50. // Replace backslashes
  51. CString csName(lpName);
  52. int nCount = csName.Replace(L'\\', '_');
  53. LPCWSTR lpCorrectName = csName;
  54. if (nCount || (flProtect != flNewProtect)) {
  55. // Something happened, so log it
  56. if (nCount) {
  57. LOGN(eDbgLevelError,
  58. "[CreateFileMapping] Corrected event name from (%S) to (%S)", lpName, lpCorrectName);
  59. }
  60. if (flProtect != flNewProtect) {
  61. LOGN(eDbgLevelError, "[CreateFileMapping] Adding SEC_COMMIT flag");
  62. }
  63. // Call again with fixed parameters
  64. hRet = ORIGINAL_API(CreateFileMappingW)(hFile, lpAttributes, flNewProtect,
  65. dwMaximumSizeHigh, dwMaximumSizeLow, lpCorrectName);
  66. }
  67. }
  68. CSTRING_CATCH {
  69. // Do nothing
  70. }
  71. }
  72. return hRet;
  73. }
  74. /*++
  75. Pass through to CreateFileMappingW.
  76. --*/
  77. HANDLE
  78. APIHOOK(CreateFileMappingA)(
  79. HANDLE hFile,
  80. LPSECURITY_ATTRIBUTES lpAttributes,
  81. DWORD flProtect,
  82. DWORD dwMaximumSizeHigh,
  83. DWORD dwMaximumSizeLow,
  84. LPCSTR lpName
  85. )
  86. {
  87. HANDLE hRet;
  88. CSTRING_TRY {
  89. // Convert to unicode
  90. CString csName(lpName);
  91. LPCWSTR lpwName = csName;
  92. hRet = APIHOOK(CreateFileMappingW)(hFile, lpAttributes, flProtect,
  93. dwMaximumSizeHigh, dwMaximumSizeLow, lpwName);
  94. }
  95. CSTRING_CATCH {
  96. // Fall back as gracefully as possible
  97. hRet = ORIGINAL_API(CreateFileMappingA)(hFile, lpAttributes, flProtect,
  98. dwMaximumSizeHigh, dwMaximumSizeLow, lpName);
  99. }
  100. return hRet;
  101. }
  102. /*++
  103. Correct the name
  104. --*/
  105. HANDLE
  106. APIHOOK(OpenFileMappingW)(
  107. DWORD dwDesiredAccess,
  108. BOOL bInheritHandle,
  109. LPCWSTR lpName
  110. )
  111. {
  112. HANDLE hRet = ORIGINAL_API(OpenFileMappingW)(dwDesiredAccess, bInheritHandle,
  113. lpName);
  114. if (!hRet) {
  115. //
  116. // The call failed, so try correcting the parameters
  117. //
  118. CSTRING_TRY {
  119. // Replace backslashes
  120. CString csName(lpName);
  121. int nCount = csName.Replace(L'\\', '_');
  122. LPCWSTR lpCorrectName = csName;
  123. if (nCount) {
  124. // Something happened, so log it
  125. LOGN(eDbgLevelError,
  126. "OpenFileMappingW corrected event name from (%S) to (%S)", lpName, lpCorrectName);
  127. // Call again with fixed parameters
  128. hRet = ORIGINAL_API(OpenFileMappingW)(dwDesiredAccess, bInheritHandle,
  129. lpCorrectName);
  130. }
  131. }
  132. CSTRING_CATCH
  133. {
  134. // Do nothing
  135. }
  136. }
  137. return hRet;
  138. }
  139. /*++
  140. Pass through to OpenFileMappingW.
  141. --*/
  142. HANDLE
  143. APIHOOK(OpenFileMappingA)(
  144. DWORD dwDesiredAccess,
  145. BOOL bInheritHandle,
  146. LPCSTR lpName
  147. )
  148. {
  149. HANDLE hRet;
  150. CSTRING_TRY {
  151. // Convert to unicode
  152. CString csName(lpName);
  153. LPCWSTR lpwName = csName;
  154. hRet = APIHOOK(OpenFileMappingW)(dwDesiredAccess, bInheritHandle,
  155. lpwName);
  156. }
  157. CSTRING_CATCH {
  158. // Fall back as gracefully as possible
  159. hRet = ORIGINAL_API(OpenFileMappingA)(dwDesiredAccess, bInheritHandle,
  160. lpName);
  161. }
  162. return hRet;
  163. }
  164. /*++
  165. Register hooked functions
  166. --*/
  167. HOOK_BEGIN
  168. APIHOOK_ENTRY(KERNEL32.DLL, CreateFileMappingA)
  169. APIHOOK_ENTRY(KERNEL32.DLL, CreateFileMappingW)
  170. APIHOOK_ENTRY(KERNEL32.DLL, OpenFileMappingA)
  171. APIHOOK_ENTRY(KERNEL32.DLL, OpenFileMappingW)
  172. HOOK_END
  173. IMPLEMENT_SHIM_END