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.

249 lines
5.8 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: D L L M A I N . C P P
  7. //
  8. // Contents: DLL entry points for upnphost.dll
  9. //
  10. // Notes:
  11. //
  12. // Author: mbend 8 Aug 2000
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "pch.h"
  16. #pragma hdrstop
  17. #include "uhbase.h"
  18. #include "uhres.h"
  19. #include "hostp.h"
  20. #include "hostp_i.c"
  21. #include "uhutil.h"
  22. #define INITGUID
  23. #include "uhclsid.h"
  24. //+---------------------------------------------------------------------------
  25. // Note: Proxy/Stub Information
  26. // To merge the proxy/stub code into the object DLL, add the file
  27. // dlldatax.c to the project. Make sure precompiled headers
  28. // are turned off for this file, and add _MERGE_PROXYSTUB to the
  29. // defines for the project.
  30. //
  31. // If you are not running WinNT4.0 or Win95 with DCOM, then you
  32. // need to remove the following define from dlldatax.c
  33. // #define _WIN32_WINNT 0x0400
  34. //
  35. // Further, if you are running MIDL without /Oicf switch, you also
  36. // need to remove the following define from dlldatax.c.
  37. // #define USE_STUBLESS_PROXY
  38. //
  39. // Modify the custom build rule for foo.idl by adding the following
  40. // files to the Outputs.
  41. // foo_p.c
  42. // dlldata.c
  43. // To build a separate proxy/stub DLL,
  44. // run nmake -f foops.mk in the project directory.
  45. // Proxy/Stub registration entry points
  46. //
  47. #include "dlldatax.h"
  48. #ifdef _MERGE_PROXYSTUB
  49. extern "C" HINSTANCE hProxyDll;
  50. #endif
  51. //+---------------------------------------------------------------------------
  52. // DLL Entry Point
  53. //
  54. EXTERN_C
  55. BOOL
  56. WINAPI
  57. DllMain (
  58. HINSTANCE hinst,
  59. DWORD dwReason,
  60. LPVOID pvReserved)
  61. {
  62. #ifdef _MERGE_PROXYSTUB
  63. if (!PrxDllMain(hinst, dwReason, pvReserved))
  64. {
  65. return FALSE;
  66. }
  67. #endif
  68. if (dwReason == DLL_PROCESS_ATTACH)
  69. {
  70. DisableThreadLibraryCalls (hinst);
  71. InitializeDebugging();
  72. _Module.DllProcessAttach (hinst);
  73. }
  74. else if (dwReason == DLL_PROCESS_DETACH)
  75. {
  76. DbgCheckPrematureDllUnload ("upnphost.dll", _Module.GetLockCount());
  77. _Module.DllProcessDetach ();
  78. UnInitializeDebugging();
  79. }
  80. return TRUE;
  81. }
  82. //+---------------------------------------------------------------------------
  83. // Used to determine whether the DLL can be unloaded by OLE
  84. //
  85. STDAPI
  86. DllCanUnloadNow ()
  87. {
  88. #ifdef _MERGE_PROXYSTUB
  89. return PrxDllCanUnloadNow();
  90. #endif
  91. }
  92. //+---------------------------------------------------------------------------
  93. // Returns a class factory to create an object of the requested type
  94. //
  95. STDAPI
  96. DllGetClassObject (
  97. REFCLSID rclsid,
  98. REFIID riid,
  99. LPVOID* ppv)
  100. {
  101. #ifdef _MERGE_PROXYSTUB
  102. if (PrxDllGetClassObject(rclsid, riid, ppv) == S_OK)
  103. {
  104. return S_OK;
  105. }
  106. #endif
  107. return _Module.GetClassObject(rclsid, riid, ppv);
  108. }
  109. //+---------------------------------------------------------------------------
  110. // ServiceMain - Called by the generic service process when starting
  111. // this service.
  112. //
  113. // type of LPSERVICE_MAIN_FUNCTIONW
  114. //
  115. EXTERN_C
  116. VOID
  117. WINAPI
  118. ServiceMain (
  119. DWORD argc,
  120. PWSTR argv[])
  121. {
  122. _Module.ServiceMain (argc, argv);
  123. }
  124. //+---------------------------------------------------------------------------
  125. //
  126. // Function: HrChangeServiceToRestart
  127. //
  128. // Purpose: Change the SCM registration to restart the service automatically if
  129. // dies prematurely.
  130. //
  131. // Arguments:
  132. // (none)
  133. //
  134. // Author: mbend 11 Jan 2001
  135. //
  136. // Notes:
  137. //
  138. HRESULT HrChangeServiceToRestart()
  139. {
  140. HRESULT hr = S_OK;
  141. SC_HANDLE scm = OpenSCManager(NULL, SERVICES_ACTIVE_DATABASE, SC_MANAGER_CONNECT);
  142. if(!scm)
  143. {
  144. hr = HrFromLastWin32Error();
  145. }
  146. if(SUCCEEDED(hr))
  147. {
  148. SC_HANDLE scUpnphost = OpenService(scm, L"upnphost", SERVICE_CHANGE_CONFIG | SERVICE_START);
  149. if(!scUpnphost)
  150. {
  151. hr = HrFromLastWin32Error();
  152. }
  153. if(SUCCEEDED(hr))
  154. {
  155. SC_ACTION scAction = {SC_ACTION_RESTART, 0};
  156. SERVICE_FAILURE_ACTIONS sfa = {INFINITE, NULL, NULL, 1, &scAction};
  157. if(!ChangeServiceConfig2(scUpnphost, SERVICE_CONFIG_FAILURE_ACTIONS, &sfa))
  158. {
  159. hr = HrFromLastWin32Error();
  160. }
  161. CloseServiceHandle(scUpnphost);
  162. }
  163. CloseServiceHandle(scm);
  164. }
  165. TraceHr(ttidRegistrar, FAL, hr, FALSE, "HrChangeServiceToRestart");
  166. return hr;
  167. }
  168. //+---------------------------------------------------------------------------
  169. // DllRegisterServer - Adds entries to the system registry
  170. //
  171. STDAPI
  172. DllRegisterServer ()
  173. {
  174. BOOL fCoUninitialize = TRUE;
  175. HRESULT hr = CoInitializeEx (NULL,
  176. COINIT_DISABLE_OLE1DDE | COINIT_APARTMENTTHREADED);
  177. if (FAILED(hr))
  178. {
  179. fCoUninitialize = FALSE;
  180. if (RPC_E_CHANGED_MODE == hr)
  181. {
  182. hr = S_OK;
  183. }
  184. }
  185. if (SUCCEEDED(hr))
  186. {
  187. _Module.UpdateRegistryFromResource (IDR_UPNPHOST, TRUE);
  188. hr = NcAtlModuleRegisterServer (&_Module);
  189. #ifdef _MERGE_PROXYSTUB
  190. if(SUCCEEDED(hr))
  191. {
  192. hr = PrxDllRegisterServer ();
  193. }
  194. #endif
  195. if (fCoUninitialize)
  196. {
  197. CoUninitialize ();
  198. }
  199. }
  200. if(SUCCEEDED(hr))
  201. {
  202. hr = HrChangeServiceToRestart();
  203. }
  204. TraceHr (ttidError, FAL, hr, FALSE, "netman!DllRegisterServer");
  205. return hr;
  206. }
  207. //+---------------------------------------------------------------------------
  208. // DllUnregisterServer - Removes entries from the system registry
  209. //
  210. STDAPI
  211. DllUnregisterServer ()
  212. {
  213. _Module.UpdateRegistryFromResource (IDR_UPNPHOST, FALSE);
  214. _Module.UnregisterServer ();
  215. #ifdef _MERGE_PROXYSTUB
  216. PrxDllUnregisterServer ();
  217. #endif
  218. return S_OK;
  219. }