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.

207 lines
5.0 KiB

  1. /*++
  2. Copyright (c) 1995-2000 Microsoft Corporation
  3. Module Name:
  4. main.c
  5. Abstract:
  6. This module contains the startup and termination code for the
  7. User-mode Plug-and-Play service.
  8. Author:
  9. Paula Tomlinson (paulat) 6-20-1995
  10. Environment:
  11. User mode only.
  12. Revision History:
  13. 3-Mar-1995 paulat
  14. Creation and initial implementation.
  15. --*/
  16. //
  17. // includes
  18. //
  19. #include "precomp.h"
  20. #include "umpnpi.h"
  21. #include "umpnpdat.h"
  22. //
  23. // global data
  24. //
  25. HANDLE ghPnPHeap; // Private heap for PNP Manager
  26. HANDLE ghInst; // Module handle
  27. HKEY ghEnumKey = NULL; // Key to HKLM\System\CCC\Enum
  28. HKEY ghServicesKey = NULL; // Key to HKLM\System\CCC\Services
  29. HKEY ghClassKey = NULL; // key to HKLM\System\CCC\Class
  30. HKEY ghPerHwIdKey = NULL; // key to HKLM\Software\Microsoft\Windows NT\CurrentVersion\PerHwIdStorage
  31. LUID gLuidLoadDriverPrivilege; // LUID of LoadDriver privilege
  32. LUID gLuidUndockPrivilege; // LUID of Undock privilege
  33. CRITICAL_SECTION PnpSynchronousCall;
  34. BOOL
  35. DllMainCRTStartup(
  36. PVOID hModule,
  37. ULONG Reason,
  38. PCONTEXT pContext
  39. )
  40. /*++
  41. Routine Description:
  42. This is the standard DLL entrypoint routine, called whenever a process
  43. or thread attaches or detaches.
  44. Arguments:
  45. hModule - PVOID parameter that specifies the handle of the DLL
  46. Reason - ULONG parameter that specifies the reason this entrypoint
  47. was called (either PROCESS_ATTACH, PROCESS_DETACH,
  48. THREAD_ATTACH, or THREAD_DETACH).
  49. pContext - Reserved, not used.
  50. Return value:
  51. Returns true if initialization compeleted successfully, false is not.
  52. --*/
  53. {
  54. UNREFERENCED_PARAMETER(pContext);
  55. ghInst = (HANDLE)hModule;
  56. switch (Reason) {
  57. case DLL_PROCESS_ATTACH:
  58. ghPnPHeap = HeapCreate(0, 65536, 0);
  59. if (ghPnPHeap == NULL) {
  60. KdPrintEx((DPFLTR_PNPMGR_ID,
  61. DBGF_ERRORS,
  62. "UMPNPMGR: Failed to create heap, error = %d\n",
  63. GetLastError()));
  64. ghPnPHeap = GetProcessHeap();
  65. }
  66. try {
  67. InitializeCriticalSection(&PnpSynchronousCall);
  68. } except(EXCEPTION_EXECUTE_HANDLER) {
  69. //
  70. // InitializeCriticalSection may raise STATUS_NO_MEMORY exception
  71. //
  72. return FALSE;
  73. }
  74. if (ghEnumKey == NULL) {
  75. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, pszRegPathEnum, 0,
  76. KEY_ALL_ACCESS, &ghEnumKey)
  77. != ERROR_SUCCESS) {
  78. if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, pszRegPathEnum,
  79. 0, NULL, REG_OPTION_NON_VOLATILE,
  80. KEY_ALL_ACCESS, NULL, &ghEnumKey,
  81. NULL) != ERROR_SUCCESS) {
  82. ghEnumKey = NULL;
  83. }
  84. }
  85. }
  86. if (ghServicesKey == NULL) {
  87. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, pszRegPathServices, 0,
  88. KEY_ALL_ACCESS, &ghServicesKey)
  89. != ERROR_SUCCESS) {
  90. ghServicesKey = NULL;
  91. }
  92. }
  93. if (ghClassKey == NULL) {
  94. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, pszRegPathClass, 0,
  95. KEY_ALL_ACCESS, &ghClassKey)
  96. != ERROR_SUCCESS) {
  97. ghClassKey = NULL;
  98. }
  99. }
  100. if(ghPerHwIdKey == NULL) {
  101. if(ERROR_SUCCESS != RegCreateKeyEx(HKEY_LOCAL_MACHINE,
  102. pszRegPathPerHwIdStorage,
  103. 0,
  104. NULL,
  105. REG_OPTION_NON_VOLATILE,
  106. KEY_ALL_ACCESS,
  107. NULL,
  108. &ghPerHwIdKey,
  109. NULL)) {
  110. ghPerHwIdKey = NULL;
  111. }
  112. }
  113. //
  114. // Initialize notification lists.
  115. //
  116. if (!InitNotification()) {
  117. return FALSE;
  118. }
  119. break;
  120. case DLL_PROCESS_DETACH:
  121. if (ghEnumKey != NULL) {
  122. RegCloseKey(ghEnumKey);
  123. ghEnumKey = NULL;
  124. }
  125. if (ghServicesKey != NULL) {
  126. RegCloseKey(ghServicesKey);
  127. ghServicesKey = NULL;
  128. }
  129. if (ghClassKey != NULL) {
  130. RegCloseKey(ghClassKey);
  131. ghClassKey = NULL;
  132. }
  133. if (ghPerHwIdKey != NULL) {
  134. RegCloseKey(ghPerHwIdKey);
  135. ghPerHwIdKey = NULL;
  136. }
  137. try {
  138. DeleteCriticalSection(&PnpSynchronousCall);
  139. } except(EXCEPTION_EXECUTE_HANDLER) {
  140. NOTHING;
  141. }
  142. break;
  143. case DLL_THREAD_ATTACH:
  144. case DLL_THREAD_DETACH:
  145. break;
  146. }
  147. return TRUE;
  148. } // DllMainCRTStartup