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.

265 lines
5.7 KiB

  1. #include "headers.hxx"
  2. DeclareTag(tagFactory, "MTScript", "Monitor Class Factories");
  3. HRESULT CreateConnectedMachine(CMTScript *pMT, IUnknown **ppUnk);
  4. HRESULT CreateScriptedProcess(CMTScript *pMT, IUnknown **ppUnk);
  5. REGCLASSDATA g_regData[] =
  6. {
  7. {
  8. &CLSID_RemoteMTScript,
  9. CreateConnectedMachine,
  10. CLSCTX_SERVER,
  11. 0
  12. },
  13. {
  14. &CLSID_LocalScriptedProcess,
  15. CreateScriptedProcess,
  16. CLSCTX_LOCAL_SERVER,
  17. 0
  18. }
  19. };
  20. // ***************************************************************
  21. //
  22. //
  23. HRESULT
  24. RegisterClassObjects(CMTScript *pMT)
  25. {
  26. HRESULT hr = S_OK;
  27. REGCLASSDATA *prcd;
  28. int i;
  29. CStdFactory *pFact;
  30. TraceTag((tagFactory, "Registering Class Factories..."));
  31. for (i = 0, prcd = g_regData;
  32. i < ARRAY_SIZE(g_regData);
  33. i++, prcd++)
  34. {
  35. pFact = new CStdFactory(pMT, prcd->pfnCreate);
  36. if (!pFact)
  37. {
  38. hr = E_OUTOFMEMORY;
  39. }
  40. else
  41. {
  42. hr = CoRegisterClassObject(*prcd->pclsid,
  43. pFact,
  44. prcd->ctxCreate,
  45. REGCLS_MULTIPLEUSE,
  46. &prcd->dwCookie);
  47. pFact->Release();
  48. }
  49. if (hr)
  50. {
  51. // BUGBUG -- how do we handle errors here?
  52. WCHAR achBuf[100];
  53. wsprintf(achBuf, L"CoRegisterClassObject failed with (%x)", hr);
  54. MessageBox(NULL, achBuf, L"MTScript", MB_OK | MB_SETFOREGROUND);
  55. break;
  56. }
  57. }
  58. return hr;
  59. }
  60. void
  61. UnregisterClassObjects()
  62. {
  63. REGCLASSDATA *prcd;
  64. int i;
  65. TraceTag((tagFactory, "Unregistering Class Factories..."));
  66. for (i = 0, prcd = g_regData;
  67. i < ARRAY_SIZE(g_regData);
  68. i++, prcd++)
  69. {
  70. if (prcd->dwCookie != 0)
  71. {
  72. CoRevokeClassObject(prcd->dwCookie);
  73. }
  74. }
  75. }
  76. // ***************************************************************
  77. CStdFactory::CStdFactory(CMTScript *pMT, FNCREATE *pfnCreate)
  78. {
  79. _ulRefs = 1;
  80. _pMT = pMT;
  81. _pfnCreate = pfnCreate;
  82. }
  83. STDMETHODIMP
  84. CStdFactory::QueryInterface(REFIID iid, void ** ppvObject)
  85. {
  86. if (iid == IID_IClassFactory || iid == IID_IUnknown)
  87. {
  88. *ppvObject = (IClassFactory*)this;
  89. }
  90. else
  91. {
  92. *ppvObject = NULL;
  93. return E_NOINTERFACE;
  94. }
  95. ((IUnknown *)*ppvObject)->AddRef();
  96. return S_OK;
  97. }
  98. STDMETHODIMP
  99. CStdFactory::CreateInstance(IUnknown * pUnkOuter,
  100. REFIID riid,
  101. void ** ppvObject)
  102. {
  103. IUnknown *pUnk;
  104. HRESULT hr = E_FAIL;
  105. *ppvObject = NULL;
  106. TraceTag((tagFactory, "%p: CreateInstance called...", this));
  107. if (pUnkOuter != NULL)
  108. {
  109. hr = CLASS_E_NOAGGREGATION;
  110. }
  111. hr = (*_pfnCreate)(_pMT, &pUnk);
  112. if (!hr)
  113. {
  114. hr = pUnk->QueryInterface(riid, ppvObject);
  115. }
  116. pUnk->Release();
  117. if (hr)
  118. {
  119. // BUGBUG -- Fix this
  120. WCHAR achBuf[100];
  121. wsprintf(achBuf, L"CreateInstance failed with (%x)", hr);
  122. MessageBox(NULL, achBuf, L"MTScript", MB_OK | MB_SETFOREGROUND);
  123. }
  124. return hr;
  125. }
  126. STDMETHODIMP
  127. CStdFactory::LockServer(BOOL fLock)
  128. {
  129. // BUGBUG -- Increment a count on the CMTScript object and don't go
  130. // away if it's not zero.
  131. return S_OK;
  132. }
  133. //+---------------------------------------------------------------------------
  134. //
  135. // Function: CreateConnectedMachine
  136. //
  137. // Synopsis: Creates the object which implements IConnectedMachine
  138. //
  139. // Arguments: [pMT] -- Pointer to CMTScript object
  140. // [ppUnk] -- Created object is returned here
  141. //
  142. // Notes: This function creates only one object and returns it for
  143. // all CreateInstance calls. There is no per-connection state
  144. // so it is unnecessary to create a separate object for each
  145. // connection.
  146. //
  147. //----------------------------------------------------------------------------
  148. HRESULT
  149. CreateConnectedMachine(CMTScript *pMT, IUnknown **ppUnk)
  150. {
  151. HRESULT hr = S_OK;
  152. CMachine *pMach;
  153. TraceTag((tagFactory, "Creating a CMachine object"));
  154. pMT->PostToThread(pMT, MD_MACHINECONNECT);
  155. LOCK_LOCALS(pMT);
  156. if (!pMT->_pMachine)
  157. {
  158. TraceTag((tagFactory, "Creating a CMachine object"));
  159. pMach = new CMachine(pMT, pMT->_pTIMachine);
  160. if (!pMach)
  161. {
  162. return E_OUTOFMEMORY;
  163. }
  164. hr = pMach->StartThread(NULL);
  165. if (FAILED(hr))
  166. {
  167. return hr;
  168. }
  169. pMT->_pMachine = pMach;
  170. }
  171. else
  172. {
  173. TraceTag((tagFactory, "Connecting to the existing CMachine object"));
  174. pMach = pMT->_pMachine;
  175. }
  176. // We are in the free-threaded apartment so we don't have to marshal this.
  177. return pMach->QueryInterface(IID_IUnknown, (LPVOID*)ppUnk);
  178. }
  179. //+---------------------------------------------------------------------------
  180. //
  181. // Function: CreateScriptedProcess
  182. //
  183. // Synopsis: Creates the object which implement IScriptedProcess
  184. //
  185. // Arguments: [pMT] -- Pointer to CMTScript object
  186. // [ppUnk] -- Created object is returned here
  187. //
  188. // Notes: Creates a new object for each connection.
  189. //
  190. //----------------------------------------------------------------------------
  191. HRESULT
  192. CreateScriptedProcess(CMTScript *pMT, IUnknown **ppUnk)
  193. {
  194. HRESULT hr = S_OK;
  195. CProcessComm *pPC;
  196. TraceTag((tagFactory, "Creating a CProcessComm object"));
  197. LOCK_LOCALS(pMT);
  198. pPC = new CProcessComm(pMT);
  199. if (!pPC)
  200. {
  201. return E_OUTOFMEMORY;
  202. }
  203. hr = pPC->QueryInterface(IID_IUnknown, (LPVOID*)ppUnk);
  204. pPC->Release();
  205. return hr;
  206. }