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.

223 lines
5.9 KiB

  1. /* File: D:\wacker\ext\defclsf.c (Created: 02-Mar-1994)
  2. *
  3. * Copyright 1994 by Hilgraeve Inc. -- Monroe, MI
  4. * All rights reserved
  5. *
  6. * $Revision: 2 $
  7. * $Date: 2/05/99 3:20p $
  8. */
  9. //
  10. // This file contains the implementation of SHCreateDefClassObject
  11. //
  12. #define _INC_OLE // WIN32, get ole2 from windows.h
  13. #define CONST_VTABLE
  14. #include <windows.h>
  15. #pragma hdrstop
  16. #include <windowsx.h>
  17. #include <shellapi.h>
  18. #include <shlobj.h>
  19. #include "pageext.hh"
  20. // Helper macro for C programmers
  21. #define _IOffset(class, itf) ((UINT_PTR)&(((class *)0)->itf))
  22. #define IToClass(class, itf, pitf) ((class *)(((LPSTR)pitf)-_IOffset(class, itf)))
  23. #define IToClassN(class, itf, pitf) IToClass(class, itf, pitf)
  24. #if 0
  25. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  26. * FUNCTION:
  27. * IsEqualGUID
  28. *
  29. * DESCRIPTION:
  30. * By using this function, we keep from linking into OLE32.DLL.
  31. *
  32. */
  33. STDAPI_(BOOL) IsEqualGUID(REFGUID guid1, REFGUID guid2)
  34. {
  35. return !memcmp(guid1, guid2, sizeof(GUID));
  36. }
  37. #endif
  38. //=========================================================================
  39. // CDefClassFactory class
  40. //=========================================================================
  41. STDMETHODIMP CDefClassFactory_QueryInterface(IClassFactory FAR * pcf, REFIID riid, LPVOID FAR* ppvObj);
  42. ULONG STDMETHODCALLTYPE CDefClassFactory_AddRef(IClassFactory FAR * pcf);
  43. ULONG STDMETHODCALLTYPE CDefClassFactory_Release(IClassFactory FAR * pcf);
  44. STDMETHODIMP CDefClassFactory_CreateInstance(IClassFactory FAR * pcf, LPUNKNOWN pUnkOuter,
  45. REFIID riid,
  46. LPVOID FAR* ppvObject);
  47. STDMETHODIMP CDefClassFactory_LockServer(IClassFactory FAR * pcf, BOOL fLock);
  48. //
  49. // CDefClassFactory: Class definition
  50. //
  51. //#pragma data_seg(DATASEG_READONLY)
  52. static IClassFactoryVtbl c_vtblAppUIClassFactory =
  53. {
  54. CDefClassFactory_QueryInterface,
  55. CDefClassFactory_AddRef,
  56. CDefClassFactory_Release,
  57. CDefClassFactory_CreateInstance,
  58. CDefClassFactory_LockServer
  59. };
  60. typedef struct
  61. {
  62. IClassFactory cf;
  63. UINT cRef; // Reference count
  64. LPFNCREATEINSTANCE lpfnCI; // CreateInstance callback entry
  65. UINT FAR * pcRefDll; // Reference count of the DLL
  66. const IID FAR * riidInst; // Optional interface for instance
  67. } CDefClassFactory;
  68. //
  69. // CDefClassFactory::QueryInterface
  70. //
  71. STDMETHODIMP CDefClassFactory_QueryInterface(IClassFactory FAR * pcf, REFIID riid, LPVOID FAR* ppvObj)
  72. {
  73. register CDefClassFactory * this=IToClass(CDefClassFactory, cf, pcf);
  74. if (IsEqualIID(riid, &IID_IClassFactory)
  75. || IsEqualIID(riid, &IID_IUnknown))
  76. {
  77. (LPCLASSFACTORY)*ppvObj = &this->cf;
  78. this->cRef++;
  79. return NOERROR;
  80. }
  81. return ResultFromScode(E_NOINTERFACE);
  82. }
  83. //
  84. // CDefClassFactory::AddRef
  85. //
  86. ULONG STDMETHODCALLTYPE CDefClassFactory_AddRef(IClassFactory FAR * pcf)
  87. {
  88. register CDefClassFactory * this=IToClass(CDefClassFactory, cf, pcf);
  89. return (++this->cRef);
  90. }
  91. //
  92. // CDefClassFactory::Release
  93. //
  94. ULONG STDMETHODCALLTYPE CDefClassFactory_Release(IClassFactory FAR * pcf)
  95. {
  96. register CDefClassFactory * this=IToClass(CDefClassFactory, cf, pcf);
  97. if (--this->cRef > 0)
  98. return this->cRef;
  99. if (this->pcRefDll)
  100. {
  101. (*this->pcRefDll)--;
  102. }
  103. LocalFree((HLOCAL)this);
  104. return 0;
  105. }
  106. //
  107. // CDefClassFactory::CDefClassFactory
  108. //
  109. STDMETHODIMP CDefClassFactory_CreateInstance(IClassFactory FAR * pcf, LPUNKNOWN pUnkOuter,
  110. REFIID riid,
  111. LPVOID FAR* ppvObject)
  112. {
  113. register CDefClassFactory * this=IToClass(CDefClassFactory, cf, pcf);
  114. //
  115. // We don't support aggregation at all.
  116. //
  117. if (pUnkOuter)
  118. return ResultFromScode(CLASS_E_NOAGGREGATION);
  119. //
  120. // if this->riidInst is specified, they should match.
  121. //
  122. if (this->riidInst==NULL || IsEqualIID(riid, this->riidInst)
  123. || IsEqualIID(riid, &IID_IUnknown))
  124. {
  125. return this->lpfnCI(pUnkOuter, riid, ppvObject);
  126. }
  127. return ResultFromScode(E_NOINTERFACE);
  128. }
  129. //
  130. // CDefClassFactory::LockServer
  131. //
  132. STDMETHODIMP CDefClassFactory_LockServer(IClassFactory FAR * pcf, BOOL fLock)
  133. {
  134. // REVIEW: Is this appropriate?
  135. return ResultFromScode(E_NOTIMPL);
  136. }
  137. //
  138. // CDefClassFactory constructor
  139. //
  140. CDefClassFactory * NEAR PASCAL CDefClassFactory_Create(
  141. LPFNCREATEINSTANCE lpfnCI, UINT FAR * pcRefDll, REFIID riidInst)
  142. {
  143. register CDefClassFactory * pacf;
  144. pacf = (CDefClassFactory *)LocalAlloc(LPTR, sizeof(CDefClassFactory));
  145. if (pacf)
  146. {
  147. pacf->cf.lpVtbl = &c_vtblAppUIClassFactory;
  148. pacf->cRef++; // pacf->cRef=0; (generates smaller code)
  149. pacf->pcRefDll = pcRefDll;
  150. pacf->lpfnCI = lpfnCI;
  151. pacf->riidInst = riidInst;
  152. if (pcRefDll)
  153. (*pcRefDll)++;
  154. }
  155. return pacf;
  156. }
  157. //
  158. // creates a simple default implementation of IClassFactory
  159. //
  160. // Parameters:
  161. // riid -- Specifies the interface to the class object
  162. // ppv -- Specifies the pointer to LPVOID where the class object pointer
  163. // will be returned.
  164. // lpfnCI -- Specifies the callback entry for instanciation.
  165. // pcRefDll -- Specifies the address to the DLL reference count (optional)
  166. // riidInst -- Specifies the interface to the instance (optional).
  167. //
  168. // Notes:
  169. // The riidInst will be specified only if the instance of the class
  170. // support only one interface.
  171. //
  172. STDAPI SHCreateDefClassObject(REFIID riid, LPVOID FAR* ppv,
  173. LPFNCREATEINSTANCE lpfnCI, UINT FAR * pcRefDll,
  174. REFIID riidInst)
  175. {
  176. // The default class factory supports only IClassFactory interface
  177. if (IsEqualIID(riid, &IID_IClassFactory))
  178. {
  179. CDefClassFactory *pacf =
  180. CDefClassFactory_Create(lpfnCI, pcRefDll, riidInst);
  181. if (pacf)
  182. {
  183. (IClassFactory FAR *)*ppv = &pacf->cf;
  184. return NOERROR;
  185. }
  186. return ResultFromScode(E_OUTOFMEMORY);
  187. }
  188. return ResultFromScode(E_NOINTERFACE);
  189. }