PLUGIN.CPP

// plugin.cpp : Implementation of CPluginApp and DLL registration. 

#include "stdafx.h"
#include <initguid.h>
#include "plugin.h"
#include "pluginc.h"
#include "afxpriv.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


CPluginApp NEAR theApp;

const GUID CDECL BASED_CODE _tlid =
{ 0x85df111, 0x10e, 0x11d0, { 0xaa, 0x6b, 0, 0x80, 0x5f, 0xc, 0x92, 0x32 } };
const WORD _wVerMajor = 1;
const WORD _wVerMinor = 0;


////////////////////////////////////////////////////////////////////////////
// CPluginApp::InitInstance - DLL initialization

BOOL CPluginApp::InitInstance()
{
BOOL bInit = COleControlModule::InitInstance();

if (bInit)
{
// TODO: Add your own module initialization code here.
}

return bInit;
}


////////////////////////////////////////////////////////////////////////////
// CPluginApp::ExitInstance - DLL termination

int CPluginApp::ExitInstance()
{
// TODO: Add your own module termination code here.

return COleControlModule::ExitInstance();
}


/////////////////////////////////////////////////////////////////////////////
// DllRegisterServer - Adds entries to the system registry

STDAPI DllRegisterServer(void)
{
AFX_MANAGE_STATE(_afxModuleAddrThis);

if (!AfxOleRegisterTypeLib(AfxGetInstanceHandle(), _tlid))
return ResultFromScode(SELFREG_E_TYPELIB);

if (!COleObjectFactoryEx::UpdateRegistryAll(TRUE))
return ResultFromScode(SELFREG_E_CLASS);

return NOERROR;
}


/////////////////////////////////////////////////////////////////////////////
// DllUnregisterServer - Removes entries from the system registry

STDAPI DllUnregisterServer(void)
{
AFX_MANAGE_STATE(_afxModuleAddrThis);

if (!AfxOleUnregisterTypeLib(_tlid, _wVerMajor, _wVerMinor))
return ResultFromScode(SELFREG_E_TYPELIB);

if (!COleObjectFactoryEx::UpdateRegistryAll(FALSE))
return ResultFromScode(SELFREG_E_CLASS);

return NOERROR;
}
/////////////////////////////////////////////////////////////////////////////
// CPluginObj

IMPLEMENT_DYNCREATE(CPluginObj, CCmdTarget)

CPluginObj::CPluginObj()
{
EnableAutomation();

m_pIMprUIHost = NULL;

// To keep the application running as long as an OLE automation
//object is active, the constructor calls AfxOleLockApp.
AfxOleLockApp();
}

CPluginObj::~CPluginObj()
{
// To terminate the application when all objects created with
// with OLE automation, the destructor calls AfxOleUnlockApp.
AfxOleUnlockApp();
}


void CPluginObj::OnFinalRelease()
{
// When the last reference for an automation object is released
// OnFinalRelease is called. The base class will automatically
// deletes the object. Add additional cleanup required for your
// object before calling the base class.

CCmdTarget::OnFinalRelease();
}


BEGIN_MESSAGE_MAP(CPluginObj, CCmdTarget)
//{{AFX_MSG_MAP(CPluginObj)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BEGIN_INTERFACE_MAP(CPluginObj, CCmdTarget)
INTERFACE_PART(CPluginObj, IID_IMprUIPlugin, MprUIPlugin)
END_INTERFACE_MAP()

// {085DF11A-010E-11D0-AA6B-00805F0C9232}
IMPLEMENT_OLECREATE(CPluginObj, "PLUGIN.PLUGINOBJ", 0x85df11a, 0x10e, 0x11d0, 0xaa, 0x6b, 0x0, 0x80, 0x5f, 0xc, 0x92, 0x32)

/////////////////////////////////////////////////////////////////////////////
// CPluginObj message handlers

HRESULT CPluginObj::XMprUIPlugin::QueryInterface(REFIID riid, LPVOID FAR* ppvObj)
{
METHOD_PROLOGUE(CPluginObj, MprUIPlugin);
return (HRESULT)pThis->ExternalQueryInterface(&riid, ppvObj);
}

ULONG CPluginObj::XMprUIPlugin::AddRef()
{
METHOD_PROLOGUE(CPluginObj, MprUIPlugin);
return pThis->ExternalAddRef();
}

ULONG CPluginObj::XMprUIPlugin::Release()
{
METHOD_PROLOGUE(CPluginObj, MprUIPlugin);
return pThis->ExternalRelease();
}

HRESULT CPluginObj::XMprUIPlugin::ConnectToRouter
(LPCTSTR pRouterName, IMprUIHost *pIMprUIHost)
{
METHOD_PROLOGUE(CPluginObj, MprUIPlugin);
ASSERT(pRouterName && pIMprUIHost);

pThis->m_pIMprUIHost = pIMprUIHost;
return S_OK;
}

HRESULT CPluginObj::XMprUIPlugin::GetTitle(PWSTR* ppszTitle)
{
METHOD_PROLOGUE(CPluginObj, MprUIPlugin);
ASSERT(ppszTitle);
CString str;
WCHAR *pwc;

VERIFY(str.LoadString(IDS_TITLE));
*ppszTitle = (WCHAR*)::CoTaskMemAlloc((str.GetLength() + 1) * sizeof(WCHAR));
if (*ppszTitle == NULL)
return E_OUTOFMEMORY;

#ifndef _UNICODE
USES_CONVERSION;
pwc = A2W(str);
#else
pwc = (LPTSTR)(LPCTSTR)str;
#endif

wcscpy(*ppszTitle, pwc);
return S_OK;
}

HRESULT CPluginObj::XMprUIPlugin::GetContextMenu(HMENU* phMenu)
{
METHOD_PROLOGUE(CPluginObj, MprUIPlugin);
ASSERT(phMenu);
CMenu menu;

VERIFY(menu.LoadMenu(IDR_PLUGIN));
*phMenu = menu.Detach();

return S_OK;
}

HRESULT CPluginObj::XMprUIPlugin::ProcessCommand
(WPARAM wParam, LPARAM lParam, HWND hWndParent)
{
METHOD_PROLOGUE(CPluginObj, MprUIPlugin);

if (LOWORD(wParam) != ID_CREATEFLOATER)
return E_INVALIDARG;

if (pThis->m_pIMprUIHost == NULL)
return E_FAIL;

CString str;
VERIFY(str.LoadString(IDS_FLOAT));
WCHAR *pwc;

#ifndef _UNICODE
USES_CONVERSION;
pwc = A2W(str);
#else
pwc = (LPTSTR)(LPCTSTR)str;
#endif

HRESULT hr = pThis->m_pIMprUIHost->FloatWindow(
CPluginObj::guid, // CLSID representing the object
CPluginCtrl::guid, // CLSID of Ole Control to create
(IMprUIPlugin*)this, // Plugin in node making request
0x00000001, // constant context passed back to Ole Control
pwc // Title of floating window
);

ASSERT(hr == S_OK);

return hr;
}

HRESULT CPluginObj::XMprUIPlugin::GetCLSID(CLSID** ppClsid)
{
METHOD_PROLOGUE(CPluginObj, MprUIPlugin);
ASSERT(ppClsid);
HRESULT hr = S_OK;

*ppClsid = (CLSID*)CoTaskMemAlloc(sizeof(CLSID));
if (*ppClsid == NULL)
return E_OUTOFMEMORY;

memcpy(*ppClsid, &CPluginCtrl::guid, sizeof(CLSID));

return S_OK;
}