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

Windows编程

开发平台:

Visual C++

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