home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winui / shell / shellext / shellext.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-05  |  5.7 KB  |  248 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993-1997  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE:   shellext.cpp
  9. //
  10. // Purpose:  Implements the class factory code as well as CShellExt::QI,
  11. //           CShellExt::AddRef and CShellExt::Release code.
  12.  
  13. #include "priv.h"
  14.  
  15. //
  16. // Initialize GUIDs (should be done only and at-least once per DLL/EXE)
  17. //
  18. #pragma data_seg(".text")
  19. #define INITGUID
  20. #include <initguid.h>
  21. #include <shlguid.h>
  22. #include "shellext.h"
  23. #pragma data_seg()
  24.  
  25. //
  26. // Global variables
  27. //
  28. UINT      g_cRefThisDll = 0;    // Reference count of this DLL.
  29. HINSTANCE g_hmodThisDll = NULL;    // Handle to this DLL itself.
  30.  
  31. extern "C" int APIENTRY
  32. DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
  33. {
  34.     if (dwReason == DLL_PROCESS_ATTACH)
  35.     {
  36.         ODS("In DLLMain, DLL_PROCESS_ATTACH\r\n");
  37.  
  38.         // Extension DLL one-time initialization
  39.  
  40.         g_hmodThisDll = hInstance;
  41.     }
  42.     else if (dwReason == DLL_PROCESS_DETACH)
  43.     {
  44.         ODS("In DLLMain, DLL_PROCESS_DETACH\r\n");
  45.     }
  46.  
  47.     return 1;   // ok
  48. }
  49.  
  50. //---------------------------------------------------------------------------
  51. // DllCanUnloadNow
  52. //---------------------------------------------------------------------------
  53.  
  54. STDAPI DllCanUnloadNow(void)
  55. {
  56.     ODS("In DLLCanUnloadNow\r\n");
  57.  
  58.     return (g_cRefThisDll == 0 ? S_OK : S_FALSE);
  59. }
  60.  
  61. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppvOut)
  62. {
  63.     ODS("In DllGetClassObject\r\n");
  64.  
  65.     *ppvOut = NULL;
  66.  
  67.     if (IsEqualIID(rclsid, CLSID_ShellExtension))
  68.     {
  69.         CShellExtClassFactory *pcf = new CShellExtClassFactory;
  70.  
  71.         return pcf->QueryInterface(riid, ppvOut);
  72.     }
  73.  
  74.     return CLASS_E_CLASSNOTAVAILABLE;
  75. }
  76.  
  77. CShellExtClassFactory::CShellExtClassFactory()
  78. {
  79.     ODS("CShellExtClassFactory::CShellExtClassFactory()\r\n");
  80.  
  81.     m_cRef = 0L;
  82.  
  83.     g_cRefThisDll++;    
  84. }
  85.                                                                 
  86. CShellExtClassFactory::~CShellExtClassFactory()                
  87. {
  88.     g_cRefThisDll--;
  89. }
  90.  
  91. STDMETHODIMP CShellExtClassFactory::QueryInterface(REFIID riid,
  92.                                                    LPVOID FAR *ppv)
  93. {
  94.     ODS("CShellExtClassFactory::QueryInterface()\r\n");
  95.  
  96.     *ppv = NULL;
  97.  
  98.     // Any interface on this object is the object pointer
  99.  
  100.     if (IsEqualIID(riid, IID_IUnknown) || IsEqualIID(riid, IID_IClassFactory))
  101.     {
  102.         *ppv = (LPCLASSFACTORY)this;
  103.  
  104.         AddRef();
  105.  
  106.         return NOERROR;
  107.     }
  108.  
  109.     return E_NOINTERFACE;
  110. }    
  111.  
  112. STDMETHODIMP_(ULONG) CShellExtClassFactory::AddRef()
  113. {
  114.     return ++m_cRef;
  115. }
  116.  
  117. STDMETHODIMP_(ULONG) CShellExtClassFactory::Release()
  118. {
  119.     if (--m_cRef)
  120.         return m_cRef;
  121.  
  122.     delete this;
  123.  
  124.     return 0L;
  125. }
  126.  
  127. STDMETHODIMP CShellExtClassFactory::CreateInstance(LPUNKNOWN pUnkOuter,
  128.                                                       REFIID riid,
  129.                                                       LPVOID *ppvObj)
  130. {
  131.     ODS("CShellExtClassFactory::CreateInstance()\r\n");
  132.  
  133.     *ppvObj = NULL;
  134.  
  135.     // Shell extensions typically don't support aggregation (inheritance)
  136.  
  137.     if (pUnkOuter)
  138.         return CLASS_E_NOAGGREGATION;
  139.  
  140.     // Create the main shell extension object.  The shell will then call
  141.     // QueryInterface with IID_IShellExtInit--this is how shell extensions are
  142.     // initialized.
  143.  
  144.     LPCSHELLEXT pShellExt = new CShellExt();  //Create the CShellExt object
  145.  
  146.     if (NULL == pShellExt)
  147.         return E_OUTOFMEMORY;
  148.  
  149.     return pShellExt->QueryInterface(riid, ppvObj);
  150. }
  151.  
  152.  
  153. STDMETHODIMP CShellExtClassFactory::LockServer(BOOL fLock)
  154. {
  155.     return NOERROR;
  156. }
  157.  
  158. // *********************** CShellExt *************************
  159. CShellExt::CShellExt()
  160. {
  161.     ODS("CShellExt::CShellExt()\r\n");
  162.  
  163.     m_cRef = 0L;
  164.     m_pDataObj = NULL;
  165.  
  166.     g_cRefThisDll++;
  167. }
  168.  
  169. CShellExt::~CShellExt()
  170. {
  171.     if (m_pDataObj)
  172.         m_pDataObj->Release();
  173.  
  174.     g_cRefThisDll--;
  175. }
  176.  
  177. STDMETHODIMP CShellExt::QueryInterface(REFIID riid, LPVOID FAR *ppv)
  178. {
  179.     *ppv = NULL;
  180.  
  181.     if (IsEqualIID(riid, IID_IShellExtInit) || IsEqualIID(riid, IID_IUnknown))
  182.     {
  183.         ODS("CShellExt::QueryInterface()==>IID_IShellExtInit\r\n");
  184.  
  185.         *ppv = (LPSHELLEXTINIT)this;
  186.     }
  187.     else if (IsEqualIID(riid, IID_IContextMenu))
  188.     {
  189.         ODS("CShellExt::QueryInterface()==>IID_IContextMenu\r\n");
  190.  
  191.         *ppv = (LPCONTEXTMENU)this;
  192.     }
  193.     else if (IsEqualIID(riid, IID_IExtractIcon))
  194.     {
  195.         ODS("CShellExt::QueryInterface()==>IID_IExtractIcon\r\n");
  196.  
  197.         *ppv = (LPEXTRACTICON)this;
  198.     }
  199.     else if (IsEqualIID(riid, IID_IPersistFile))
  200.     {
  201.         ODS("CShellExt::QueryInterface()==>IPersistFile\r\n");
  202.  
  203.         *ppv = (LPPERSISTFILE)this;
  204.     }
  205.     else if (IsEqualIID(riid, IID_IShellPropSheetExt))
  206.     {
  207.         ODS("CShellExt::QueryInterface()==>IShellPropSheetExt\r\n");
  208.  
  209.         *ppv = (LPSHELLPROPSHEETEXT)this;
  210.     }
  211.     else if (IsEqualIID(riid, IID_IShellCopyHook))
  212.     {
  213.         ODS("CShellExt::QueryInterface()==>ICopyHook\r\n");
  214.  
  215.         *ppv = (LPCOPYHOOK)this;
  216.     }
  217.  
  218.     if (*ppv)
  219.     {
  220.         AddRef();
  221.  
  222.         return NOERROR;
  223.     }
  224.  
  225.     ODS("CShellExt::QueryInterface()==>Unknown Interface!\r\n");
  226.  
  227.     return E_NOINTERFACE;
  228. }
  229.  
  230. STDMETHODIMP_(ULONG) CShellExt::AddRef()
  231. {
  232.     ODS("CShellExt::AddRef()\r\n");
  233.  
  234.     return ++m_cRef;
  235. }
  236.  
  237. STDMETHODIMP_(ULONG) CShellExt::Release()
  238. {
  239.     ODS("CShellExt::Release()\r\n");
  240.  
  241.     if (--m_cRef)
  242.         return m_cRef;
  243.  
  244.     delete this;
  245.  
  246.     return 0L;
  247. }
  248.