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

Windows编程

开发平台:

Visual C++

  1. //+---------------------------------------------------------------------------
  2. //
  3. //  Microsoft Windows
  4. //  Copyright 1992 - 1997 Microsoft Corporation.
  5. //
  6. //  File:       mandel.cxx
  7. //
  8. //  Contents:   implementation for Mandelbrot Fractal engine
  9. //
  10. //  Classes:    CQuadrantEngineCF
  11. //
  12. //  Functions:  DllEntryPoint
  13. //              DllGetClassObject
  14. //              DllCanUnloadNow
  15. //
  16. //  History:    4-14-94   stevebl   Created
  17. //
  18. //----------------------------------------------------------------------------
  19. #include <windows.h>
  20. #include <ole2.h>
  21. #include <quadcid.h>
  22. #include "quad.h"
  23. ULONG gcRef, gcLock;
  24. HINSTANCE ghinst;
  25. extern "C"
  26. {
  27.     BOOL WINAPI DllMain(HINSTANCE, DWORD, LPVOID);
  28. }
  29. //+---------------------------------------------------------------------------
  30. //
  31. //  Function:   DllEntryPoint
  32. //
  33. //  Synopsis:   the DLL's entry point
  34. //
  35. //  Arguments:  [hinst]      - instance handle for the dll
  36. //              [fdwReason]  - reason for calling the entry point
  37. //              [lpReserved] - reserved
  38. //
  39. //  Returns:    TRUE if DLL_PROCESS_ATTACH initialization succeeds
  40. //              FALSE otherwise
  41. //
  42. //  History:    4-14-94   stevebl   Created
  43. //
  44. //----------------------------------------------------------------------------
  45. BOOL WINAPI DllMain(
  46.     HINSTANCE hinst,
  47.     DWORD fdwReason,
  48.     LPVOID lpReserved)
  49. {
  50.     switch (fdwReason)
  51.     {
  52.     case DLL_PROCESS_ATTACH:
  53.         ghinst = hinst;
  54.         gcRef = gcLock = 0;
  55.         return(TRUE); // initialization succeeded
  56.     case DLL_THREAD_ATTACH:
  57.         break;
  58.     case DLL_THREAD_DETACH:
  59.         break;
  60.     case DLL_PROCESS_DETACH:
  61.         break;
  62.     }
  63.     return(FALSE);  // initialization failed (ignored if not DLL_PROCESS_ATTACH)
  64. }
  65. //+---------------------------------------------------------------------------
  66. //
  67. //  Function:   DllGetClassObject
  68. //
  69. //  Synopsis:   function called by OLE to get a class object
  70. //
  71. //  Arguments:  [rclsid] - the class id of the desired class object
  72. //              [riid]   - the id of the desired interface
  73. //              [ppv]    - pointer to recieve the interface
  74. //
  75. //  Returns:    S_OK if the class and interface are implemented
  76. //
  77. //  History:    4-14-94   stevebl   Created
  78. //
  79. //----------------------------------------------------------------------------
  80. HRESULT PASCAL DllGetClassObject(
  81.     REFCLSID rclsid,
  82.     REFIID riid,
  83.     LPVOID * ppv)
  84. {
  85.     if (!IsEqualGUID(rclsid, CLSID_QUADRANTENGINE))
  86.     {
  87.         return(E_FAIL);
  88.     }
  89.     if ((!IsEqualGUID(riid, IID_IUnknown)) &&
  90.         (!IsEqualGUID(riid, IID_IClassFactory)))
  91.     {
  92.         return(E_NOINTERFACE);
  93.     }
  94.     *ppv = (LPVOID) new CQuadrantEngineCF();
  95.     if (NULL == *ppv)
  96.     {
  97.         return(E_OUTOFMEMORY);
  98.     }
  99.     ((IUnknown*)*ppv)->AddRef();
  100.     return(S_OK);
  101. }
  102. //+---------------------------------------------------------------------------
  103. //
  104. //  Function:   DllCanUnloadNow
  105. //
  106. //  Synopsis:   determines if there are no outstanding references on this DLL
  107. //
  108. //  Returns:    S_OK if the DLL can be unloaded
  109. //              S_FALSE if not
  110. //
  111. //  History:    4-14-94   stevebl   Created
  112. //
  113. //----------------------------------------------------------------------------
  114. STDAPI DllCanUnloadNow(void)
  115. {
  116.     // return S_OK if there are no references on myself
  117.     if (0 == gcRef && 0 == gcLock)
  118.     {
  119.         return(S_OK);
  120.     }
  121.     return(S_FALSE);
  122. }
  123. //+---------------------------------------------------------------------------
  124. //
  125. //  Member:     CQuadrantEngineCF::CQuadrantEngineCF
  126. //
  127. //  Synopsis:   constructor
  128. //
  129. //  History:    4-14-94   stevebl   Created
  130. //
  131. //----------------------------------------------------------------------------
  132. CQuadrantEngineCF::CQuadrantEngineCF()
  133. {
  134.     _cRef = 0;
  135. }
  136. //+---------------------------------------------------------------------------
  137. //
  138. //  Member:     CQuadrantEngineCF::~CQuadrantEngineCF
  139. //
  140. //  Synopsis:   destructor
  141. //
  142. //  History:    4-14-94   stevebl   Created
  143. //
  144. //----------------------------------------------------------------------------
  145. CQuadrantEngineCF::~CQuadrantEngineCF()
  146. {
  147. }
  148. //+---------------------------------------------------------------------------
  149. //
  150. //  Member:     CQuadrantEngineCF::QueryInterface
  151. //
  152. //  Synopsis:   Standard OLE interface
  153. //
  154. //  Arguments:  [riid] - id of the desired interface
  155. //              [ppv]  - pointer to receive the interface pointer
  156. //
  157. //  Returns:    S_OK on success
  158. //
  159. //  History:    4-14-94   stevebl   Created
  160. //
  161. //----------------------------------------------------------------------------
  162. HRESULT STDMETHODCALLTYPE CQuadrantEngineCF::QueryInterface(REFIID riid, LPVOID * ppv)
  163. {
  164.     *ppv = NULL;
  165.     if ((!IsEqualGUID(riid, IID_IUnknown)) &&
  166.         (!IsEqualGUID(riid, IID_IClassFactory)))
  167.     {
  168.         return(E_NOINTERFACE);
  169.     }
  170.     *ppv = (IClassFactory *) this;
  171.     AddRef();
  172.     return(S_OK);
  173. }
  174. //+---------------------------------------------------------------------------
  175. //
  176. //  Member:     CQuadrantEngineCF::AddRef
  177. //
  178. //  Synopsis:   increment reference count
  179. //
  180. //  Returns:    new reference count
  181. //
  182. //  History:    4-14-94   stevebl   Created
  183. //
  184. //----------------------------------------------------------------------------
  185. ULONG STDMETHODCALLTYPE CQuadrantEngineCF::AddRef(void)
  186. {
  187.     return(++_cRef);
  188. }
  189. //+---------------------------------------------------------------------------
  190. //
  191. //  Member:     CQuadrantEngineCF::Release
  192. //
  193. //  Synopsis:   decrement reference count
  194. //
  195. //  Returns:    new reference count
  196. //
  197. //  History:    4-14-94   stevebl   Created
  198. //
  199. //----------------------------------------------------------------------------
  200. ULONG STDMETHODCALLTYPE CQuadrantEngineCF::Release(void)
  201. {
  202.     ULONG cRef = --_cRef;
  203.     if (0 == cRef)
  204.     {
  205.         delete(this);
  206.     }
  207.     return(cRef);
  208. }
  209. //+---------------------------------------------------------------------------
  210. //
  211. //  Member:     CQuadrantEngineCF::CreateInstance
  212. //
  213. //  Synopsis:   creates an instance of a Mandelbrot Engine
  214. //
  215. //  Arguments:  [pUnkOuter] - pointer to the controlling unknown (must be NULL)
  216. //              [riid]      - id of the desired interface
  217. //              [ppv]       - pointer to receive the interface
  218. //
  219. //  Returns:    S_OK - success
  220. //              CLASS_E_NOAGGREATION - the caller tried to aggregate
  221. //              CLASS_E_CLASSNOTAVAILABLE - couldn't initialize the class
  222. //              E_OUTOFMEMORY - not enough memory to instantiate class
  223. //
  224. //  History:    4-14-94   stevebl   Created
  225. //
  226. //----------------------------------------------------------------------------
  227. HRESULT STDMETHODCALLTYPE CQuadrantEngineCF::CreateInstance(LPUNKNOWN pUnkOuter, REFIID riid, LPVOID * ppv)
  228. {
  229.     HRESULT hr = S_OK;
  230.     *ppv = NULL;
  231.     if (pUnkOuter != NULL)
  232.     {
  233.         return(CLASS_E_NOAGGREGATION);
  234.     }
  235.     CQuadrantEngine * pObj = new CQuadrantEngine;
  236.     if (NULL == pObj)
  237.     {
  238.         return(E_OUTOFMEMORY);
  239.     }
  240.     if (!pObj->Initialize())
  241.     {
  242.         delete ppv;
  243.         return(CLASS_E_CLASSNOTAVAILABLE);
  244.     }
  245.     hr = pObj->QueryInterface(riid, ppv);
  246.     if (FAILED(hr))
  247.     {
  248.         delete ppv;
  249.     }
  250.     else
  251.     {
  252.         gcRef++;
  253.     }
  254.     return(hr);
  255. }
  256. //+---------------------------------------------------------------------------
  257. //
  258. //  Member:     CQuadrantEngineCF::LockServer
  259. //
  260. //  Synopsis:   locks the server, preventing it from being unloaded
  261. //
  262. //  Arguments:  [fLock] - TRUE to lock, FALSE to unlock
  263. //
  264. //  Returns:    S_OK
  265. //
  266. //  Modifies:   gcLock
  267. //
  268. //  History:    4-14-94   stevebl   Created
  269. //
  270. //----------------------------------------------------------------------------
  271. HRESULT STDMETHODCALLTYPE CQuadrantEngineCF::LockServer(BOOL fLock)
  272. {
  273.     if (fLock)
  274.     {
  275.         gcLock++;
  276.     }
  277.     else
  278.     {
  279.         gcLock--;
  280.     }
  281.     return(S_OK);
  282. }