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.

342 lines
7.5 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1995
  5. //
  6. // File: factory.cxx
  7. //
  8. // Contents: Contains the class factory implementation and other DLL
  9. // functions for the mtlocal DLL.
  10. //
  11. //----------------------------------------------------------------------------
  12. #include "headers.hxx"
  13. #include "mtscript.h" // MIDL generated file
  14. #include "localobj.h"
  15. #include <advpub.h> // for RegInstall
  16. HINSTANCE g_hInstDll;
  17. HINSTANCE g_hinstAdvPack = NULL;
  18. REGINSTALL g_pfnRegInstall = NULL;
  19. // Globals used by our utilities.
  20. DWORD g_dwFALSE = 0;
  21. EXTERN_C HANDLE g_hProcessHeap = NULL;
  22. // Global class factory
  23. CMTLocalFactory g_LocalFactory;
  24. // ***************************************************************
  25. CMTLocalFactory::CMTLocalFactory()
  26. {
  27. _ulRefs = 0;
  28. }
  29. STDMETHODIMP
  30. CMTLocalFactory::QueryInterface(REFIID iid, void ** ppvObject)
  31. {
  32. if (iid == IID_IClassFactory || iid == IID_IUnknown)
  33. {
  34. *ppvObject = (IClassFactory*)this;
  35. }
  36. else
  37. {
  38. *ppvObject = NULL;
  39. return E_NOINTERFACE;
  40. }
  41. ((IUnknown *)*ppvObject)->AddRef();
  42. return S_OK;
  43. }
  44. //+---------------------------------------------------------------------------
  45. //
  46. // Member: CMTLocalFactory::CreateInstance, public
  47. //
  48. // Synopsis: Creates the CLocalMTProxy object
  49. //
  50. //----------------------------------------------------------------------------
  51. STDMETHODIMP
  52. CMTLocalFactory::CreateInstance(IUnknown * pUnkOuter,
  53. REFIID riid,
  54. void ** ppvObject)
  55. {
  56. HRESULT hr = E_FAIL;
  57. CLocalMTProxy *pMTP;
  58. *ppvObject = NULL;
  59. if (pUnkOuter != NULL)
  60. {
  61. hr = CLASS_E_NOAGGREGATION;
  62. }
  63. pMTP = new CLocalMTProxy();
  64. if (!pMTP)
  65. {
  66. return E_OUTOFMEMORY;
  67. }
  68. hr = pMTP->QueryInterface(riid, ppvObject);
  69. pMTP->Release();
  70. #if DBG == 1
  71. if (hr)
  72. TraceTag((tagError, "CreateInstance failed with %x", hr));
  73. #endif
  74. return hr;
  75. }
  76. //+---------------------------------------------------------------------------
  77. //
  78. // Member: CMTLocalFactory::LockServer, public
  79. //
  80. // Synopsis: Keeps the DLL from being unloaded
  81. //
  82. //----------------------------------------------------------------------------
  83. STDMETHODIMP
  84. CMTLocalFactory::LockServer(BOOL fLock)
  85. {
  86. // Because we implement our class factory as a global object, we don't
  87. // need to worry about keeping it in memory if LockServer is called.
  88. if (fLock)
  89. {
  90. InterlockedIncrement(&g_lObjectCount);
  91. }
  92. else
  93. {
  94. InterlockedDecrement(&g_lObjectCount);
  95. }
  96. return S_OK;
  97. }
  98. //+---------------------------------------------------------------------------
  99. //
  100. // Function: DllMain
  101. //
  102. // Synopsis: Main DLL entrypoint.
  103. //
  104. // Returns: Doesn't do much except unload advpack.dll if we loaded it.
  105. //
  106. //----------------------------------------------------------------------------
  107. BOOL
  108. WINAPI
  109. DllMain(HANDLE hDll, DWORD dwReason, LPVOID lpReserved)
  110. {
  111. BOOL fOk = TRUE;
  112. switch (dwReason)
  113. {
  114. case DLL_PROCESS_ATTACH:
  115. g_hInstDll = (HINSTANCE)hDll;
  116. Assert(g_hInstDll != NULL);
  117. DisableThreadLibraryCalls(g_hInstDll);
  118. // Set the variable used by our memory allocator.
  119. g_hProcessHeap = GetProcessHeap();
  120. break;
  121. case DLL_PROCESS_DETACH:
  122. if (g_hinstAdvPack)
  123. {
  124. FreeLibrary(g_hinstAdvPack);
  125. }
  126. break;
  127. case DLL_THREAD_DETACH:
  128. break;
  129. }
  130. return fOk;
  131. }
  132. //+---------------------------------------------------------------------------
  133. //
  134. // Function: LoadAdvPack
  135. //
  136. // Synopsis: Loads AdvPack.dll for DLL registration.
  137. //
  138. //----------------------------------------------------------------------------
  139. HRESULT
  140. LoadAdvPack()
  141. {
  142. HRESULT hr = S_OK;
  143. g_hinstAdvPack = LoadLibrary(_T("ADVPACK.DLL"));
  144. if (!g_hinstAdvPack)
  145. goto Error;
  146. g_pfnRegInstall = (REGINSTALL)GetProcAddress(g_hinstAdvPack, achREGINSTALL);
  147. if (!g_pfnRegInstall)
  148. goto Error;
  149. Cleanup:
  150. return hr;
  151. Error:
  152. hr = HRESULT_FROM_WIN32(GetLastError());
  153. if (g_hinstAdvPack)
  154. {
  155. FreeLibrary(g_hinstAdvPack);
  156. }
  157. goto Cleanup;
  158. }
  159. //+---------------------------------------------------------------------------
  160. //
  161. // Function: DllRegisterServer
  162. //
  163. // Synopsis: Register the various important information needed by our
  164. // class.
  165. //
  166. // Notes: Uses AdvPack.dll and an INF file to do the registration
  167. //
  168. //----------------------------------------------------------------------------
  169. STDAPI
  170. DllRegisterServer()
  171. {
  172. HRESULT hr;
  173. STRTABLE stReg = { 0, NULL };
  174. ITypeLib *pTypeLibDLL = NULL;
  175. TCHAR achDll[MAX_PATH];
  176. Assert(g_hInstDll != NULL);
  177. // Make sure the type library is registered
  178. GetModuleFileName(g_hInstDll, achDll, MAX_PATH);
  179. hr = THR(LoadTypeLib(achDll, &pTypeLibDLL));
  180. if (hr)
  181. goto Cleanup;
  182. // This may fail if the user is not an administrator on this machine.
  183. // It's not a big deal unless they try to run mtscript.exe, but the UI
  184. // will still work.
  185. (void) RegisterTypeLib(pTypeLibDLL, achDll, NULL);
  186. if (!g_hinstAdvPack)
  187. {
  188. hr = LoadAdvPack();
  189. if (hr)
  190. goto Cleanup;
  191. }
  192. hr = g_pfnRegInstall(g_hInstDll, "Register", &stReg);
  193. Cleanup:
  194. if (pTypeLibDLL)
  195. {
  196. pTypeLibDLL->Release();
  197. }
  198. RegFlushKey(HKEY_CLASSES_ROOT);
  199. return hr;
  200. }
  201. //+------------------------------------------------------------------------
  202. //
  203. // Function: DllUnregisterServer
  204. //
  205. // Synopsis: Undo the actions of DllRegisterServer.
  206. //
  207. //-------------------------------------------------------------------------
  208. STDAPI
  209. DllUnregisterServer()
  210. {
  211. HRESULT hr;
  212. STRTABLE stReg = { 0, NULL };
  213. Assert(g_hInstDll != NULL);
  214. if (!g_hinstAdvPack)
  215. {
  216. hr = LoadAdvPack();
  217. if (hr)
  218. goto Cleanup;
  219. }
  220. hr = g_pfnRegInstall(g_hInstDll, "Unregister", &stReg);
  221. // Unregister the type library
  222. if (!hr)
  223. {
  224. (void) UnRegisterTypeLib(LIBID_MTScriptEngine, 1, 0, 0, SYS_WIN32);
  225. }
  226. Cleanup:
  227. RegFlushKey(HKEY_CLASSES_ROOT);
  228. return hr;
  229. }
  230. //+---------------------------------------------------------------------------
  231. //
  232. // Function: DllGetClassObject
  233. //
  234. // Synopsis: Returns the class factory for a particular object
  235. //
  236. //----------------------------------------------------------------------------
  237. STDAPI
  238. DllGetClassObject(REFCLSID clsid, REFIID iid, LPVOID *ppv)
  239. {
  240. HRESULT hr;
  241. if (clsid == CLSID_RemoteMTScriptProxy)
  242. {
  243. hr = g_LocalFactory.QueryInterface(iid, ppv);
  244. }
  245. else
  246. {
  247. hr = CLASS_E_CLASSNOTAVAILABLE;
  248. }
  249. return hr;
  250. }
  251. //+---------------------------------------------------------------------------
  252. //
  253. // Function: DllCanUnloadNow
  254. //
  255. // Synopsis: Indicates if we can be unloaded.
  256. //
  257. // Notes: Returns OK if we currently have no objects running.
  258. //
  259. //----------------------------------------------------------------------------
  260. STDAPI
  261. DllCanUnloadNow()
  262. {
  263. if (g_lObjectCount == 0)
  264. {
  265. return S_OK;
  266. }
  267. return S_FALSE;
  268. }