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.

208 lines
5.2 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  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. #pragma hdrstop
  21. #include "umpnpi.h"
  22. #include "umpnpdat.h"
  23. //
  24. // global data
  25. //
  26. HANDLE ghPnPHeap; // Private heap for PNP Manager
  27. HANDLE ghInst; // Module handle
  28. HKEY ghEnumKey = NULL; // Key to HKLM\System\CCC\Enum
  29. HKEY ghServicesKey = NULL; // Key to HKLM\System\CCC\Services
  30. HKEY ghClassKey = NULL; // key to HKLM\System\CCC\Class
  31. HKEY ghPerHwIdKey = NULL; // key to HKLM\Software\Microsoft\Windows NT\CurrentVersion\PerHwIdStorage
  32. CRITICAL_SECTION PnpSynchronousCall;
  33. BOOL
  34. DllMainCRTStartup(
  35. PVOID hModule,
  36. ULONG Reason,
  37. PCONTEXT pContext
  38. )
  39. /*++
  40. Routine Description:
  41. This is the standard DLL entrypoint routine, called whenever a process
  42. or thread attaches or detaches.
  43. Arguments:
  44. hModule - PVOID parameter that specifies the handle of the DLL
  45. Reason - ULONG parameter that specifies the reason this entrypoint
  46. was called (either PROCESS_ATTACH, PROCESS_DETACH,
  47. THREAD_ATTACH, or THREAD_DETACH).
  48. pContext - Reserved, not used.
  49. Return value:
  50. Returns true if initialization compeleted successfully, false is not.
  51. --*/
  52. {
  53. UNREFERENCED_PARAMETER(pContext);
  54. ghInst = (HANDLE)hModule;
  55. switch (Reason) {
  56. case DLL_PROCESS_ATTACH:
  57. ghPnPHeap = HeapCreate(0, 65536, 0);
  58. if (ghPnPHeap == NULL) {
  59. KdPrintEx((DPFLTR_PNPMGR_ID,
  60. DBGF_ERRORS,
  61. "UMPNPMGR: Failed to create heap, error = %d\n",
  62. GetLastError()));
  63. ghPnPHeap = GetProcessHeap();
  64. }
  65. try {
  66. InitializeCriticalSection(&PnpSynchronousCall);
  67. } except(EXCEPTION_EXECUTE_HANDLER) {
  68. //
  69. // InitializeCriticalSection may raise STATUS_NO_MEMORY exception
  70. //
  71. return FALSE;
  72. }
  73. if (ghEnumKey == NULL) {
  74. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, pszRegPathEnum, 0,
  75. KEY_ALL_ACCESS, &ghEnumKey)
  76. != ERROR_SUCCESS) {
  77. //
  78. // The Enum key must exist by the time the service controller
  79. // loads this DLL into it's process. It is created by the
  80. // kernel-mode plug and play manager, with special
  81. // (non-inherited) ACLs applied. It is not valid to attempt and
  82. // create it here if it does not exist.
  83. //
  84. ghEnumKey = NULL;
  85. }
  86. }
  87. if (ghServicesKey == NULL) {
  88. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, pszRegPathServices, 0,
  89. KEY_ALL_ACCESS, &ghServicesKey)
  90. != ERROR_SUCCESS) {
  91. ghServicesKey = NULL;
  92. }
  93. }
  94. if (ghClassKey == NULL) {
  95. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, pszRegPathClass, 0,
  96. KEY_ALL_ACCESS, &ghClassKey)
  97. != ERROR_SUCCESS) {
  98. ghClassKey = NULL;
  99. }
  100. }
  101. if(ghPerHwIdKey == NULL) {
  102. if(ERROR_SUCCESS != RegCreateKeyEx(HKEY_LOCAL_MACHINE,
  103. pszRegPathPerHwIdStorage,
  104. 0,
  105. NULL,
  106. REG_OPTION_NON_VOLATILE,
  107. KEY_ALL_ACCESS,
  108. NULL,
  109. &ghPerHwIdKey,
  110. NULL)) {
  111. ghPerHwIdKey = NULL;
  112. }
  113. }
  114. //
  115. // Initialize notification lists.
  116. //
  117. if (!InitNotification()) {
  118. return FALSE;
  119. }
  120. break;
  121. case DLL_PROCESS_DETACH:
  122. if (ghEnumKey != NULL) {
  123. RegCloseKey(ghEnumKey);
  124. ghEnumKey = NULL;
  125. }
  126. if (ghServicesKey != NULL) {
  127. RegCloseKey(ghServicesKey);
  128. ghServicesKey = NULL;
  129. }
  130. if (ghClassKey != NULL) {
  131. RegCloseKey(ghClassKey);
  132. ghClassKey = NULL;
  133. }
  134. if (ghPerHwIdKey != NULL) {
  135. RegCloseKey(ghPerHwIdKey);
  136. ghPerHwIdKey = NULL;
  137. }
  138. try {
  139. DeleteCriticalSection(&PnpSynchronousCall);
  140. } except(EXCEPTION_EXECUTE_HANDLER) {
  141. NOTHING;
  142. }
  143. break;
  144. case DLL_THREAD_ATTACH:
  145. case DLL_THREAD_DETACH:
  146. break;
  147. }
  148. return TRUE;
  149. } // DllMainCRTStartup