DEFCLSF.C
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:6k
源码类别:

Windows编程

开发平台:

Visual C++

  1. /******************************************************************************
  2. *       This is a part of the Microsoft Source Code Samples. 
  3. *       Copyright (C) 1995-1997 Microsoft Corporation.
  4. *       All rights reserved. 
  5. *       This source code is only intended as a supplement to 
  6. *       Microsoft Development Tools and/or WinHelp documentation.
  7. *       See these sources for detailed information regarding the 
  8. *       Microsoft samples programs.
  9. ******************************************************************************/
  10. //
  11. // This file contains the implementation of SHCreateDefClassObject
  12. //
  13. #include "priv.h"
  14. //=========================================================================
  15. // CDefClassFactory class
  16. //=========================================================================
  17. STDMETHODIMP CDefClassFactory_QueryInterface(IClassFactory FAR * pcf, REFIID riid, LPVOID FAR* ppvObj);
  18. ULONG STDMETHODCALLTYPE CDefClassFactory_AddRef(IClassFactory FAR * pcf);
  19. ULONG STDMETHODCALLTYPE CDefClassFactory_Release(IClassFactory FAR * pcf);
  20. STDMETHODIMP CDefClassFactory_CreateInstance(IClassFactory FAR * pcf, LPUNKNOWN pUnkOuter,
  21.                               REFIID riid,
  22.                               LPVOID FAR* ppvObject);
  23. STDMETHODIMP CDefClassFactory_LockServer(IClassFactory FAR * pcf, BOOL fLock);
  24. //
  25. // CDefClassFactory: Class definition
  26. //
  27. #pragma data_seg(".text")
  28. IClassFactoryVtbl c_vtblAppUIClassFactory = {
  29.             CDefClassFactory_QueryInterface,
  30.             CDefClassFactory_AddRef,
  31.             CDefClassFactory_Release,
  32.             CDefClassFactory_CreateInstance,
  33.     CDefClassFactory_LockServer
  34. };
  35. #pragma data_seg()
  36. typedef struct
  37. {
  38.     IClassFactory      cf;
  39.     UINT               cRef; // Reference count
  40.     LPFNCREATEINSTANCE lpfnCI; // CreateInstance callback entry
  41.     UINT FAR *         pcRefDll; // Reference count of the DLL
  42.     const IID FAR *    riidInst; // Optional interface for instance
  43. } CDefClassFactory;
  44. //
  45. // CDefClassFactory::QueryInterface
  46. //
  47. STDMETHODIMP CDefClassFactory_QueryInterface(IClassFactory FAR * pcf, REFIID riid, LPVOID FAR* ppvObj)
  48. {
  49.     register CDefClassFactory * this=IToClassN(CDefClassFactory, cf, pcf);
  50.     if (IsEqualIID(riid, &IID_IClassFactory)
  51.      || IsEqualIID(riid, &IID_IUnknown))
  52.     {
  53. (LPCLASSFACTORY)*ppvObj = &this->cf;
  54. this->cRef++;
  55.         return NOERROR;
  56.     }
  57.     return ResultFromScode(E_NOINTERFACE);
  58. }
  59. //
  60. // CDefClassFactory::AddRef
  61. //
  62. ULONG STDMETHODCALLTYPE CDefClassFactory_AddRef(IClassFactory FAR * pcf)
  63. {
  64.     register CDefClassFactory * this=IToClassN(CDefClassFactory, cf, pcf);
  65.     return (++this->cRef);
  66. }
  67. //
  68. // CDefClassFactory::Release
  69. //
  70. ULONG STDMETHODCALLTYPE CDefClassFactory_Release(IClassFactory FAR * pcf)
  71. {
  72.     register CDefClassFactory * this=IToClassN(CDefClassFactory, cf, pcf);
  73.     if (--this->cRef > 0)
  74. return this->cRef;
  75.     if (this->pcRefDll) {
  76. (*this->pcRefDll)--;
  77.     }
  78.     LocalFree((HLOCAL)this);
  79.     return 0;
  80. }
  81. //
  82. // CDefClassFactory::CDefClassFactory
  83. //
  84. STDMETHODIMP CDefClassFactory_CreateInstance(IClassFactory FAR * pcf, LPUNKNOWN pUnkOuter,
  85.                               REFIID riid,
  86.                               LPVOID FAR* ppvObject)
  87. {
  88.     register CDefClassFactory * this=IToClassN(CDefClassFactory, cf, pcf);
  89.     //
  90.     // We don't support aggregation at all.
  91.     //
  92.     if (pUnkOuter)
  93.     {
  94. return ResultFromScode(CLASS_E_NOAGGREGATION);
  95.     }
  96.     //
  97.     // if this->riidInst is specified, they should match.
  98.     //
  99.     if (this->riidInst==NULL
  100.      || IsEqualIID(riid, this->riidInst)
  101.      || IsEqualIID(riid, &IID_IUnknown))
  102.     {
  103. return this->lpfnCI(pUnkOuter, riid, ppvObject);
  104.     }
  105.     return ResultFromScode(E_NOINTERFACE);
  106. }
  107. //
  108. // CDefClassFactory::LockServer
  109. //
  110. STDMETHODIMP CDefClassFactory_LockServer(IClassFactory FAR * pcf, BOOL fLock)
  111. {
  112.     // REVIEW: Is this appropriate?
  113.     return ResultFromScode(E_NOTIMPL);
  114. }
  115. //
  116. // CDefClassFactory constructor
  117. //
  118. CDefClassFactory * NEAR PASCAL CDefClassFactory_Create(
  119.     LPFNCREATEINSTANCE lpfnCI, UINT FAR * pcRefDll, REFIID riidInst)
  120. {
  121.     register CDefClassFactory * pacf;
  122.     pacf = (CDefClassFactory *)LocalAlloc(LPTR, sizeof(CDefClassFactory));
  123.     if (pacf)
  124.     {
  125. pacf->cf.lpVtbl = &c_vtblAppUIClassFactory;
  126. pacf->cRef++;  // pacf->cRef=0; (generates smaller code)
  127. pacf->pcRefDll = pcRefDll;
  128. pacf->lpfnCI = lpfnCI;
  129. pacf->riidInst = riidInst;
  130. if (pcRefDll) {
  131.     (*pcRefDll)++;
  132. }
  133.     }
  134.     return pacf;
  135. }
  136. // BUGBUG: remove this for build 86
  137. STDAPI Shell_CreateDefClassObject(REFIID riid, LPVOID FAR* ppv,
  138.  LPFNCREATEINSTANCE lpfnCI, UINT FAR * pcRefDll,
  139.  REFIID riidInst)
  140. {
  141.     MessageBeep(0);
  142.     // DebugMsg(DM_TRACE, "Shell_CreateDefClassObject: obsolete, use SHCreateDefClassObject");
  143.     return SHCreateDefClassObject(riid, ppv, lpfnCI, pcRefDll, riidInst);
  144. }
  145. //
  146. // creates a simple default implementation of IClassFactory
  147. //
  148. // Parameters:
  149. //  riid     -- Specifies the interface to the class object
  150. //  ppv      -- Specifies the pointer to LPVOID where the class object pointer
  151. //               will be returned.
  152. //  lpfnCI   -- Specifies the callback entry for instanciation.
  153. //  pcRefDll -- Specifies the address to the DLL reference count (optional)
  154. //  riidInst -- Specifies the interface to the instance (optional).
  155. //
  156. // Notes:
  157. //   The riidInst will be specified only if the instance of the class
  158. //  support only one interface.
  159. //
  160. STDAPI SHCreateDefClassObject(REFIID riid, LPVOID FAR* ppv,
  161.  LPFNCREATEINSTANCE lpfnCI, UINT FAR * pcRefDll,
  162.  REFIID riidInst)
  163. {
  164.     // The default class factory supports only IClassFactory interface
  165.     if (IsEqualIID(riid, &IID_IClassFactory))
  166.     {
  167. CDefClassFactory * pacf = CDefClassFactory_Create(lpfnCI, pcRefDll, riidInst);
  168. if (pacf)
  169. {
  170.     (IClassFactory FAR *)*ppv = &pacf->cf;
  171.     return NOERROR;
  172. }
  173. return ResultFromScode(E_OUTOFMEMORY);
  174.     }
  175.     return ResultFromScode(E_NOINTERFACE);
  176. }