SHELLEXT.CPP

// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF 
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright 1993 - 1998 Microsoft Corporation. All Rights Reserved.
//
// MODULE: shellext.cpp
//
// Purpose: Implements the class factory code as well as CShellExt::QI,
// CShellExt::AddRef and CShellExt::Release code.

#include "priv.h"

//
// Initialize GUIDs (should be done only and at-least once per DLL/EXE)
//
#pragma data_seg(".text")
#define INITGUID
#include <initguid.h>
#include <shlguid.h>
#include "shellext.h"
#pragma data_seg()

//
// Global variables
//
UINT g_cRefThisDll = 0; // Reference count of this DLL.
HINSTANCE g_hmodThisDll = NULL;// Handle to this DLL itself.

extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
if (dwReason == DLL_PROCESS_ATTACH)
{
ODS("In DLLMain, DLL_PROCESS_ATTACH\r\n");

// Extension DLL one-time initialization

g_hmodThisDll = hInstance;
}
else if (dwReason == DLL_PROCESS_DETACH)
{
ODS("In DLLMain, DLL_PROCESS_DETACH\r\n");
}

return 1; // ok
}

//---------------------------------------------------------------------------
// DllCanUnloadNow
//---------------------------------------------------------------------------

STDAPI DllCanUnloadNow(void)
{
ODS("In DLLCanUnloadNow\r\n");

return (g_cRefThisDll == 0 ? S_OK : S_FALSE);
}

STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppvOut)
{
ODS("In DllGetClassObject\r\n");

*ppvOut = NULL;

if (IsEqualIID(rclsid, CLSID_ShellExtension))
{
CShellExtClassFactory *pcf = new CShellExtClassFactory;

return pcf->QueryInterface(riid, ppvOut);
}

return CLASS_E_CLASSNOTAVAILABLE;
}

CShellExtClassFactory::CShellExtClassFactory()
{
ODS("CShellExtClassFactory::CShellExtClassFactory()\r\n");

m_cRef = 0L;

g_cRefThisDll++;
}

CShellExtClassFactory::~CShellExtClassFactory()
{
g_cRefThisDll--;
}

STDMETHODIMP CShellExtClassFactory::QueryInterface(REFIID riid,
LPVOID FAR *ppv)
{
ODS("CShellExtClassFactory::QueryInterface()\r\n");

*ppv = NULL;

// Any interface on this object is the object pointer

if (IsEqualIID(riid, IID_IUnknown) || IsEqualIID(riid, IID_IClassFactory))
{
*ppv = (LPCLASSFACTORY)this;

AddRef();

return NOERROR;
}

return E_NOINTERFACE;
}

STDMETHODIMP_(ULONG) CShellExtClassFactory::AddRef()
{
return ++m_cRef;
}

STDMETHODIMP_(ULONG) CShellExtClassFactory::Release()
{
if (--m_cRef)
return m_cRef;

delete this;

return 0L;
}

STDMETHODIMP CShellExtClassFactory::CreateInstance(LPUNKNOWN pUnkOuter,
REFIID riid,
LPVOID *ppvObj)
{
ODS("CShellExtClassFactory::CreateInstance()\r\n");

*ppvObj = NULL;

// Shell extensions typically don't support aggregation (inheritance)

if (pUnkOuter)
return CLASS_E_NOAGGREGATION;

// Create the main shell extension object. The shell will then call
// QueryInterface with IID_IShellExtInit--this is how shell extensions are
// initialized.

LPCSHELLEXT pShellExt = new CShellExt(); //Create the CShellExt object

if (NULL == pShellExt)
return E_OUTOFMEMORY;

return pShellExt->QueryInterface(riid, ppvObj);
}


STDMETHODIMP CShellExtClassFactory::LockServer(BOOL fLock)
{
return NOERROR;
}

// *********************** CShellExt *************************
CShellExt::CShellExt()
{
ODS("CShellExt::CShellExt()\r\n");

m_cRef = 0L;
m_pDataObj = NULL;

g_cRefThisDll++;
}

CShellExt::~CShellExt()
{
if (m_pDataObj)
m_pDataObj->Release();

g_cRefThisDll--;
}

STDMETHODIMP CShellExt::QueryInterface(REFIID riid, LPVOID FAR *ppv)
{
*ppv = NULL;

if (IsEqualIID(riid, IID_IShellExtInit) || IsEqualIID(riid, IID_IUnknown))
{
ODS("CShellExt::QueryInterface()==>IID_IShellExtInit\r\n");

*ppv = (LPSHELLEXTINIT)this;
}
else if (IsEqualIID(riid, IID_IContextMenu))
{
ODS("CShellExt::QueryInterface()==>IID_IContextMenu\r\n");

*ppv = (LPCONTEXTMENU)this;
}
else if (IsEqualIID(riid, IID_IExtractIcon))
{
ODS("CShellExt::QueryInterface()==>IID_IExtractIcon\r\n");

*ppv = (LPEXTRACTICON)this;
}
else if (IsEqualIID(riid, IID_IPersistFile))
{
ODS("CShellExt::QueryInterface()==>IPersistFile\r\n");

*ppv = (LPPERSISTFILE)this;
}
else if (IsEqualIID(riid, IID_IShellPropSheetExt))
{
ODS("CShellExt::QueryInterface()==>IShellPropSheetExt\r\n");

*ppv = (LPSHELLPROPSHEETEXT)this;
}
else if (IsEqualIID(riid, IID_IShellCopyHook))
{
ODS("CShellExt::QueryInterface()==>ICopyHook\r\n");

*ppv = (LPCOPYHOOK)this;
}

if (*ppv)
{
AddRef();

return NOERROR;
}

ODS("CShellExt::QueryInterface()==>Unknown Interface!\r\n");

return E_NOINTERFACE;
}

STDMETHODIMP_(ULONG) CShellExt::AddRef()
{
ODS("CShellExt::AddRef()\r\n");

return ++m_cRef;
}

STDMETHODIMP_(ULONG) CShellExt::Release()
{
ODS("CShellExt::Release()\r\n");

if (--m_cRef)
return m_cRef;

delete this;

return 0L;
}