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.

142 lines
2.8 KiB

  1. /*++
  2. Copyright (c) 2000-2001 Microsoft Corporation
  3. Module Name:
  4. Utils.cpp
  5. Abstract:
  6. Common functions for all modules
  7. Notes:
  8. None
  9. History:
  10. 11/03/2001 clupu Created
  11. --*/
  12. #include "ShimHook.h"
  13. namespace ShimLib
  14. {
  15. void
  16. DumpUnloadOrder(
  17. PPEB Peb
  18. )
  19. {
  20. PLIST_ENTRY LdrNext;
  21. if (GetDebugLevel() > eDbgLevelInfo) {
  22. return;
  23. }
  24. //
  25. // Dump the order the shims will unload
  26. //
  27. LdrNext = Peb->Ldr->InInitializationOrderModuleList.Blink;
  28. DPF("ShimLib", eDbgLevelInfo, "\n[SeiDumpUnloadOrder] Unload order:\n");
  29. while (LdrNext != &Peb->Ldr->InInitializationOrderModuleList) {
  30. PLDR_DATA_TABLE_ENTRY LdrEntry;
  31. LdrEntry = CONTAINING_RECORD(LdrNext, LDR_DATA_TABLE_ENTRY, InInitializationOrderLinks);
  32. LdrNext = LdrNext->Blink;
  33. //
  34. // Dump the entry to be called
  35. //
  36. DPF("ShimLib",
  37. eDbgLevelInfo,
  38. "[SeiDumpUnloadOrder] \"%40S\" 0x%x\n",
  39. LdrEntry->BaseDllName.Buffer,
  40. LdrEntry->DllBase);
  41. }
  42. }
  43. /*++
  44. Function Description:
  45. Call this function if you want to push the specified DLL to the
  46. end of the list of modules to be unloaded.
  47. NOTE: Make sure that the module that will be pushed at the end
  48. will not call any APIs that reside in other modules during
  49. its DLL_PROCESS_DETACH callout.
  50. Arguments:
  51. IN hMod - handle to the module to push.
  52. Specify NULL to push the calling DLL.
  53. Return Value:
  54. TRUE if successful, FALSE otherwise.
  55. History:
  56. 11/01/2001 clupu Created
  57. --*/
  58. BOOL
  59. MakeShimUnloadLast(
  60. HMODULE hMod
  61. )
  62. {
  63. PPEB Peb = NtCurrentPeb();
  64. PLIST_ENTRY LdrHead;
  65. PLIST_ENTRY LdrNext;
  66. BOOL bRet = FALSE;
  67. if (hMod == NULL) {
  68. hMod = g_hinstDll;
  69. }
  70. //
  71. // Dump the unload order if SHIM_DEBUG_LEVEL is at least eDbgLevelInfo
  72. //
  73. DumpUnloadOrder(Peb);
  74. LdrHead = &Peb->Ldr->InInitializationOrderModuleList;
  75. LdrNext = LdrHead->Flink;
  76. while (LdrNext != LdrHead) {
  77. PLIST_ENTRY LdrCrt;
  78. PLDR_DATA_TABLE_ENTRY LdrEntry;
  79. LdrEntry = CONTAINING_RECORD(LdrNext, LDR_DATA_TABLE_ENTRY, InInitializationOrderLinks);
  80. LdrCrt = LdrNext;
  81. LdrNext = LdrEntry->InInitializationOrderLinks.Flink;
  82. if (LdrEntry->DllBase == hMod) {
  83. //
  84. // This is the module we're looking for. Get him out of the list
  85. // and insert it at the beginning of the list.
  86. //
  87. RemoveEntryList(LdrCrt);
  88. InsertHeadList(LdrHead, LdrCrt);
  89. bRet = TRUE;
  90. break;
  91. }
  92. }
  93. DumpUnloadOrder(Peb);
  94. return bRet;
  95. }
  96. }; // end of namespace ShimLib