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.

371 lines
9.8 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1993.
  5. //
  6. // File: defcf.cpp
  7. //
  8. // Contents: The class factory implementations for the default handler
  9. // and default link
  10. //
  11. // Classes: CDefClassFactory
  12. //
  13. // Functions: DllGetClassObject
  14. //
  15. // History: dd-mmm-yy Author Comment
  16. // 01-Feb-95 t-ScottH added Dump method to CDefClassFactory
  17. // and DumpCDefClassFactory API
  18. // 24-Jan-94 alexgo first pass converting to Cairo style
  19. // memory allocation
  20. // 11-Jan-94 alexgo made custom memory stream unmarshalling
  21. // for 16bit only.
  22. // 11-Jan-94 alexgo added VDATEHEAP macro to every function
  23. // and method
  24. // 22-Nov-93 alexgo removed overload GUID ==
  25. // 09-Nov-93 alexgo 32bit port
  26. // 04-Mar-92 SriniK author
  27. //
  28. //--------------------------------------------------------------------------
  29. #include <le2int.h>
  30. #pragma SEG(deflink)
  31. #include <create.h>
  32. #include "defcf.h"
  33. #ifdef _DEBUG
  34. #include <dbgdump.h>
  35. #endif // _DEBUG
  36. NAME_SEG(DefLink)
  37. ASSERTDATA
  38. #ifdef _MAC
  39. // These global class decl's are necessary for CFront because both
  40. // defhndlr.h and deflink.h have nested class's of the same name.
  41. // These decl's allow this.
  42. class CDataObjectImpl {
  43. VDATEHEAP();
  44. };
  45. class COleObjectImpl {};
  46. class CManagerImpl {};
  47. class CAdvSinkImpl {};
  48. class CPersistStgImpl {};
  49. #endif
  50. #include "olerem.h"
  51. #include "defhndlr.h"
  52. #include "deflink.h"
  53. //+-------------------------------------------------------------------------
  54. //
  55. // Function: Ole32DllGetClassObject
  56. //
  57. // Synopsis: Returns a pointer to the class factory
  58. //
  59. // Effects:
  60. //
  61. // Arguments: [clsid] -- the class id desired
  62. // [iid] -- the requested interface
  63. // [ppv] -- where to put the pointer to the new object
  64. //
  65. // Requires:
  66. //
  67. // Returns: HRESULT
  68. //
  69. // Signals:
  70. //
  71. // Modifies:
  72. //
  73. // Algorithm:
  74. //
  75. // History: dd-mmm-yy Author Comment
  76. // 09-Nov-93 alexgo 32bit port
  77. // 22-Nov-93 alexgo removed overloaded GUID ==
  78. //
  79. // Notes:
  80. //
  81. //--------------------------------------------------------------------------
  82. #pragma SEG(DllGetClassObject)
  83. #ifdef WIN32
  84. HRESULT Ole232DllGetClassObject(REFCLSID clsid, REFIID iid, void FAR* FAR* ppv)
  85. #else
  86. STDAPI DllGetClassObject(REFCLSID clsid, REFIID iid, void FAR* FAR* ppv)
  87. #endif // WIN32
  88. {
  89. VDATEHEAP();
  90. VDATEIID( iid );
  91. VDATEPTROUT(ppv, LPVOID);
  92. *ppv = NULL;
  93. if ( !IsEqualIID(iid,IID_IUnknown) && !IsEqualIID(iid,
  94. IID_IClassFactory))
  95. {
  96. return ResultFromScode(E_NOINTERFACE);
  97. }
  98. if ((*ppv = new CDefClassFactory (clsid)) == NULL)
  99. {
  100. return ResultFromScode(E_OUTOFMEMORY);
  101. }
  102. return NOERROR;
  103. }
  104. /*
  105. * IMPLEMENTATION of CDefClassFactory
  106. *
  107. */
  108. //+-------------------------------------------------------------------------
  109. //
  110. // Member: CDefClassFactory::CDefClassFactory
  111. //
  112. // Synopsis: constructor for the class factory
  113. //
  114. // Effects:
  115. //
  116. // Arguments: [clsidClass] -- the class id for the the factory
  117. //
  118. // Requires:
  119. //
  120. // Returns: void
  121. //
  122. // Signals:
  123. //
  124. // Modifies:
  125. //
  126. // Derivation:
  127. //
  128. // Algorithm:
  129. //
  130. // History: dd-mmm-yy Author Comment
  131. // 09-Nov-93 alexgo 32bit port
  132. //
  133. // Notes:
  134. //
  135. //--------------------------------------------------------------------------
  136. #pragma SEG(CDefClassFactory_ctor)
  137. CDefClassFactory::CDefClassFactory (REFCLSID clsidClass)
  138. : CStdClassFactory(1), m_clsid(clsidClass)
  139. {
  140. VDATEHEAP();
  141. GET_A5();
  142. }
  143. //+-------------------------------------------------------------------------
  144. //
  145. // Member: CDefClassFactory::CreateInstance
  146. //
  147. // Synopsis: Creates an instance of the class that the class factory
  148. // was created for (by Ole32DllGetClassObject)
  149. //
  150. // Effects:
  151. //
  152. // Arguments: [pUnkOuter] -- the controlling unknown (for aggregation)
  153. // [iid] -- the requested interface ID
  154. // [ppv] -- where to put the pointer to the new object
  155. //
  156. // Requires:
  157. //
  158. // Returns: HRESULT. E_INVALIDARG is returned if an non-null pUnkOuter
  159. // is passed when asked to create a moniker.
  160. //
  161. // Derivation: IClassFactory
  162. //
  163. // Algorithm: Tests the classid against a number of predefined ones, doing
  164. // appropriate tests and actions for each (see comments below).
  165. //
  166. // History: dd-mmm-yy Author Comment
  167. // 11-Jan-94 alexgo ifdef'ed out code to deal with
  168. // custom marshalling of memory streams
  169. // and lockbytes (it's 16bit only)
  170. // 12-Nov-93 alexgo removed IID check's for monikers
  171. // (see notes below)
  172. // 12-Nov-93 alexgo removed a goto and more redundant code
  173. // changed overloaded == to IsEqualCLSID
  174. // 11-Nov-93 alexgo 32bit port
  175. //
  176. //--------------------------------------------------------------------------
  177. #pragma SEG(CDefClassFactory_CreateInstance)
  178. STDMETHODIMP CDefClassFactory::CreateInstance
  179. (IUnknown FAR* pUnkOuter, REFIID iid, void FAR* FAR* ppv)
  180. {
  181. VDATEHEAP();
  182. M_PROLOG(this);
  183. VDATEIID( iid );
  184. VDATEPTROUT( ppv, LPVOID );
  185. *ppv = NULL;
  186. if ( pUnkOuter )
  187. {
  188. VDATEIFACE( pUnkOuter );
  189. }
  190. HRESULT hresult = E_OUTOFMEMORY;
  191. IUnknown *pUnk;
  192. if (IsEqualCLSID(m_clsid, CLSID_StdOleLink))
  193. {
  194. pUnk = CDefLink::Create(pUnkOuter);
  195. }
  196. else
  197. {
  198. pUnk = CDefObject::Create(pUnkOuter, m_clsid,
  199. EMBDHLP_INPROC_HANDLER | EMBDHLP_CREATENOW, NULL);
  200. }
  201. if ( pUnk != NULL )
  202. {
  203. //if we get this far, then everything is OK; we have successfully
  204. //created default handler or default link. now QueryInterface and return
  205. hresult = pUnk->QueryInterface(iid, ppv);
  206. //The QI will add a ref, plus the ref from Create, so
  207. //we need to release one.
  208. pUnk->Release();
  209. }
  210. return hresult;
  211. }
  212. //+-------------------------------------------------------------------------
  213. //
  214. // Member: CDefClassFactory::Dump, public (_DEBUG only)
  215. //
  216. // Synopsis: return a string containing the contents of the data members
  217. //
  218. // Effects:
  219. //
  220. // Arguments: [ppszDump] - an out pointer to a null terminated character array
  221. // [ulFlag] - flag determining prefix of all newlines of the
  222. // out character array (default is 0 - no prefix)
  223. // [nIndentLevel] - will add a indent prefix after the other prefix
  224. // for ALL newlines (including those with no prefix)
  225. //
  226. // Requires:
  227. //
  228. // Returns: HRESULT
  229. //
  230. // Signals:
  231. //
  232. // Modifies: [ppszDump] - argument
  233. //
  234. // Derivation:
  235. //
  236. // Algorithm: use dbgstream to create a string containing information on the
  237. // content of data structures
  238. //
  239. // History: dd-mmm-yy Author Comment
  240. // 01-Feb-95 t-ScottH author
  241. //
  242. // Notes:
  243. //
  244. //--------------------------------------------------------------------------
  245. #ifdef _DEBUG
  246. HRESULT CDefClassFactory::Dump(char **ppszDump, ULONG ulFlag, int nIndentLevel)
  247. {
  248. int i;
  249. char *pszPrefix;
  250. char *pszCLSID;
  251. dbgstream dstrPrefix;
  252. dbgstream dstrDump;
  253. // determine prefix of newlines
  254. if ( ulFlag & DEB_VERBOSE )
  255. {
  256. dstrPrefix << this << " _VB ";
  257. }
  258. // determine indentation prefix for all newlines
  259. for (i = 0; i < nIndentLevel; i++)
  260. {
  261. dstrPrefix << DUMPTAB;
  262. }
  263. pszPrefix = dstrPrefix.str();
  264. // put data members in stream
  265. pszCLSID = DumpCLSID(m_clsid);
  266. dstrDump << pszPrefix << "CLSID = " << pszCLSID << endl;
  267. CoTaskMemFree(pszCLSID);
  268. // cleanup and provide pointer to character array
  269. *ppszDump = dstrDump.str();
  270. if (*ppszDump == NULL)
  271. {
  272. *ppszDump = UtDupStringA(szDumpErrorMessage);
  273. }
  274. CoTaskMemFree(pszPrefix);
  275. return NOERROR;
  276. }
  277. #endif // _DEBUG
  278. //+-------------------------------------------------------------------------
  279. //
  280. // Function: DumpCDefClassFactory, public (_DEBUG only)
  281. //
  282. // Synopsis: calls the CDefClassFactory::Dump method, takes care of errors and
  283. // returns the zero terminated string
  284. //
  285. // Effects:
  286. //
  287. // Arguments: [pCDF] - pointer to CDefClassFactory
  288. // [ulFlag] - flag determining prefix of all newlines of the
  289. // out character array (default is 0 - no prefix)
  290. // [nIndentLevel] - will add a indent prefix after the other prefix
  291. // for ALL newlines (including those with no prefix)
  292. //
  293. // Requires:
  294. //
  295. // Returns: character array of structure dump or error (null terminated)
  296. //
  297. // Signals:
  298. //
  299. // Modifies:
  300. //
  301. // Algorithm:
  302. //
  303. // History: dd-mmm-yy Author Comment
  304. // 01-Feb-95 t-ScottH author
  305. //
  306. // Notes:
  307. //
  308. //--------------------------------------------------------------------------
  309. #ifdef _DEBUG
  310. char *DumpCDefClassFactory(CDefClassFactory *pCDF, ULONG ulFlag, int nIndentLevel)
  311. {
  312. HRESULT hresult;
  313. char *pszDump;
  314. if (pCDF == NULL)
  315. {
  316. return UtDupStringA(szDumpBadPtr);
  317. }
  318. hresult = pCDF->Dump(&pszDump, ulFlag, nIndentLevel);
  319. if (hresult != NOERROR)
  320. {
  321. CoTaskMemFree(pszDump);
  322. return DumpHRESULT(hresult);
  323. }
  324. return pszDump;
  325. }
  326. #endif // _DEBUG