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.

210 lines
4.8 KiB

  1. //***************************************************************************
  2. //
  3. // MAINDLL.CPP
  4. //
  5. // Module: WMI Framework Instance provider
  6. //
  7. // Purpose: Contains DLL entry points. Also has code that controls
  8. // when the DLL can be unloaded by tracking the number of
  9. // objects and locks as well as routines that support
  10. // self registration.
  11. //
  12. // Copyright (c) 2000-2001 Microsoft Corporation, All Rights Reserved
  13. //
  14. //***************************************************************************
  15. #include "precomp.h"
  16. #include <dllunreg.h>
  17. #include <DllCommon.h>
  18. #include <brodcast.h>
  19. #include <initguid.h>
  20. HMODULE ghModule;
  21. // {6E78DAD9-E187-4d6e-BA63-760256D6F405}
  22. DEFINE_GUID( CLSID_WMISESSION,
  23. 0x6e78dad9, 0xe187, 0x4d6e, 0xba, 0x63, 0x76, 0x2, 0x56, 0xd6, 0xf4, 0x5);
  24. #define PROVIDER_NAME L"WMIPSESS"
  25. //Count number of objects and number of locks.
  26. long g_cLock = 0;
  27. //***************************************************************************
  28. //
  29. // DllGetClassObject
  30. //
  31. // Purpose: Called by Ole when some client wants a class factory. Return
  32. // one only if it is the sort of class this DLL supports.
  33. //
  34. //***************************************************************************
  35. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, PPVOID ppv)
  36. {
  37. HRESULT hr = S_OK;
  38. try
  39. {
  40. if ( CLSID_WMISESSION == rclsid )
  41. {
  42. hr = CommonGetClassObject(riid, ppv, PROVIDER_NAME, g_cLock);
  43. }
  44. else
  45. {
  46. hr = E_FAIL;
  47. }
  48. }
  49. catch ( ... )
  50. {
  51. hr = E_OUTOFMEMORY;
  52. }
  53. return hr;
  54. }
  55. //***************************************************************************
  56. //
  57. // DllCanUnloadNow
  58. //
  59. // Purpose: Called periodically by Ole in order to determine if the
  60. // DLL can be freed.
  61. //
  62. // Return: S_OK if there are no objects in use and the class factory
  63. // isn't locked.
  64. //
  65. //***************************************************************************
  66. STDAPI DllCanUnloadNow()
  67. {
  68. SCODE sc = S_FALSE;
  69. try
  70. {
  71. sc = CommonCanUnloadNow(PROVIDER_NAME, g_cLock);
  72. }
  73. catch ( ... )
  74. {
  75. // sc should already be set correctly
  76. }
  77. return sc;
  78. }
  79. //***************************************************************************
  80. //
  81. // DllRegisterServer
  82. //
  83. // Purpose: Called during setup or by regsvr32.
  84. //
  85. // Return: NOERROR if registration successful, error otherwise.
  86. //***************************************************************************
  87. STDAPI DllRegisterServer(void)
  88. {
  89. HRESULT t_status = S_OK;
  90. try
  91. {
  92. t_status = RegisterServer( _T("Microsoft Session And Connection Provider"), CLSID_WMISESSION ) ;
  93. }
  94. catch ( ... )
  95. {
  96. t_status = E_OUTOFMEMORY;
  97. }
  98. return t_status;
  99. }
  100. //***************************************************************************
  101. //
  102. // DllUnregisterServer
  103. //
  104. // Purpose: Called when it is time to remove the registry entries.
  105. //
  106. // Return: NOERROR if registration successful, error otherwise.
  107. //***************************************************************************
  108. STDAPI DllUnregisterServer(void)
  109. {
  110. HRESULT t_status = S_OK;
  111. try
  112. {
  113. t_status = UnregisterServer( CLSID_WMISESSION ) ;
  114. }
  115. catch ( ... )
  116. {
  117. t_status = E_OUTOFMEMORY;
  118. }
  119. return t_status;
  120. }
  121. //***************************************************************************
  122. //
  123. // DllMain
  124. //
  125. // Purpose: Called by the operating system when processes and threads are
  126. // initialized and terminated, or upon calls to the LoadLibrary
  127. // and FreeLibrary functions
  128. //
  129. // Return: TRUE if load was successful, else FALSE
  130. //***************************************************************************
  131. BOOL APIENTRY DllMain( HINSTANCE hInstDLL, // handle to DLL module
  132. DWORD fdwReason, // reason for calling function
  133. LPVOID lpReserved ) // reserved
  134. {
  135. BOOL bRet = TRUE;
  136. try
  137. {
  138. LogMessage2( L"%s -> DllMain", PROVIDER_NAME);
  139. // Perform actions based on the reason for calling.
  140. switch( fdwReason )
  141. {
  142. case DLL_PROCESS_ATTACH:
  143. {
  144. bRet = CommonProcessAttach(PROVIDER_NAME, g_cLock, hInstDLL);
  145. }
  146. break;
  147. case DLL_THREAD_ATTACH:
  148. {
  149. // Do thread-specific initialization.
  150. }
  151. break;
  152. case DLL_THREAD_DETACH:
  153. {
  154. // Do thread-specific cleanup.
  155. }
  156. break;
  157. case DLL_PROCESS_DETACH:
  158. {
  159. // Perform any necessary cleanup.
  160. LogMessage( L"DLL_PROCESS_DETACH" );
  161. }
  162. break;
  163. }
  164. }
  165. catch ( ... )
  166. {
  167. bRet = FALSE;
  168. }
  169. return bRet; // Status of DLL_PROCESS_ATTACH.
  170. }