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.

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