How to hook command and mouse events in C++?

 

Dear All, some body can help me?

How to hook command and mouse events in C++?

I want to create another ATL dll to realize the command action.

Please give me a tutorial step by step with ATL.

Thanks!

 

Posted by: assassin 007
Post date: 9/14/2009 2:18:41 AM

11 REPLIES

RE: How to hook command and mouse events in C++?

Take a look at the sample I uploaded:

 

http://www.jasonnewell.net/Forums/tabid/55/forumid/1/postid/3381/view/topic/Default.aspx

 

Posted by: R.D. Holland
Post date: 9/14/2009 9:10:44 AM

RE: How to hook command and mouse events in C++?

Hi, Holland, I've downloaded your project, but it's too complex, I tired to create my project, but there's error, please help me to modify the codes, thanks!

1. create a project named CmdTest

2. Add a ATL simple object named Create

3. Add Implement interface of ISECommandEvents,ISECommandWindowEvents,ISEMouseEvents

4. Add Method named Init to interface of ICreate, STDMETHOD(Init)(IDispatch* pDispatch);

5. Add Function named Attach to class of CCreate, HRESULT Attach(void);

When I complie my project, IDE tell me there's error, error code:C2259: 'ATL::CComObject' : cannot instantiate abstract class

 

Posted by: assassin 007
Post date: 9/15/2009 4:37:09 AM

RE: How to hook command and mouse events in C++?

Add codes to stdafx.h as following:

#include

#include

#include

#include

 

#include

#include

#include "secatids.h"

#include "addins_i.c"

 

#pragma warning( disable : 4278 )

#pragma warning( disable : 4146 )

 

#import "constant.tlb"

#import "framewrk.tlb" exclude ("UINT_PTR", "LONG_PTR")

#import "fwksupp.tlb"

#import "geometry.tlb"

#import "part.tlb"

#import "assembly.tlb"

#import "draft.tlb"

#include

 

#pragma warning( default : 4146 )

#pragma warning( default : 4278 )

 

/* Remark/Unremark usinge namespace statements as needed */

using namespace SolidEdgeFramework;

using namespace SolidEdgeFrameworkSupport;

using namespace SolidEdgeGeometry;

using namespace SolidEdgePart;

using namespace SolidEdgeAssembly;

using namespace SolidEdgeDraft;

 

Posted by: assassin 007
Post date: 9/15/2009 4:38:46 AM

RE: How to hook command and mouse events in C++?

create.h

// Create.h : CCreate ???

 

#pragma once

#include "resource.h" // ???

 

#include "CmdTest.h"

 

#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)

#error "Windows CE ??(?????? DCOM ??? Windows Mobile ??)?????????? COM ????? _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA ??? ATL ??????? COM ????????????? COM ?????rgs ?????????????“Free”,???????? DCOM Windows CE ????????????"

#endif

 

// CCreate

 

class ATL_NO_VTABLE CCreate :

public CComObjectRootEx,

public CComCoClass,

public IDispatchImpl,

public ISECommandEvents,

public ISECommandWindowEvents,

public ISEMouseEvents

{

public:

CCreate()

{

}

 

DECLARE_REGISTRY_RESOURCEID(IDR_CREATE)

 

 

BEGIN_COM_MAP(CCreate)

COM_INTERFACE_ENTRY(ICreate)

COM_INTERFACE_ENTRY(IDispatch)

COM_INTERFACE_ENTRY(ISECommandEvents)

COM_INTERFACE_ENTRY(ISECommandWindowEvents)

COM_INTERFACE_ENTRY(ISEMouseEvents)

END_COM_MAP()

 

 

 

DECLARE_PROTECT_FINAL_CONSTRUCT()

 

HRESULT FinalConstruct()

{

return S_OK;

}

 

void FinalRelease()

{

}

 

public:

 

 

// ISECommandEvents Methods

public:

STDMETHOD(Activate)()

{

MessageBox(NULL,"Command Active",NULL,MB_OK);

return E_NOTIMPL;

}

STDMETHOD(Deactivate)()

{

return E_NOTIMPL;

}

STDMETHOD(Terminate)()

{

return E_NOTIMPL;

}

STDMETHOD(Idle)(long lCount, BOOL * pbMore)

{

return E_NOTIMPL;

}

STDMETHOD(KeyDown)(short * KeyCode, short Shift)

{

return E_NOTIMPL;

}

STDMETHOD(KeyPress)(short * KeyAscii)

{

return E_NOTIMPL;

}

STDMETHOD(KeyUp)(short * KeyCode, short Shift)

{

return E_NOTIMPL;

}

 

// ISECommandWindowEvents Methods

public:

STDMETHOD(WindowProc)(LPDISPATCH pTheDoc, LPDISPATCH pTheView, unsigned int nMsg, UINT_PTR wParam, LONG_PTR lParam, LONG_PTR * lResult)

{

return E_NOTIMPL;

}

 

// ISEMouseEvents Methods

public:

STDMETHOD(MouseDown)(short sButton, short sShift, double dX, double dY, double dZ, LPDISPATCH pWindowDispatch, long lKeyPointType, LPDISPATCH pGraphicDispatch)

{

if(sButton==1)

MessageBox(NULL,"Left Mouse Button",NULL,MB_OK);

return E_NOTIMPL;

}

STDMETHOD(MouseUp)(short sButton, short sShift, double dX, double dY, double dZ, LPDISPATCH pWindowDispatch, long lKeyPointType, LPDISPATCH pGraphicDispatch)

{

return E_NOTIMPL;

}

STDMETHOD(MouseMove)(short sButton, short sShift, double dX, double dY, double dZ, LPDISPATCH pWindowDispatch, long lKeyPointType, LPDISPATCH pGraphicDispatch)

{

return E_NOTIMPL;

}

STDMETHOD(MouseClick)(short sButton, short sShift, double dX, double dY, double dZ, LPDISPATCH pWindowDispatch, long lKeyPointType, LPDISPATCH pGraphicDispatch)

{

return E_NOTIMPL;

}

STDMETHOD(MouseDblClick)(short sButton, short sShift, double dX, double dY, double dZ, LPDISPATCH pWindowDispatch, long lKeyPointType, LPDISPATCH pGraphicDispatch)

{

return E_NOTIMPL;

}

STDMETHOD(MouseDrag)(short sButton, short sShift, double dX, double dY, double dZ, LPDISPATCH pWindowDispatch, short DragState, long lKeyPointType, LPDISPATCH pGraphicDispatch)

{

return E_NOTIMPL;

}

STDMETHOD(Init)(IDispatch* pDispatch);

 

private:

SolidEdgeFramework::ApplicationPtr pApplication;

DWORD m_eventToken;

DWORD m_wndEventToken;

HWND m_hwnd;

ISECommandPtr m_cmd;

ISolidEdgeRibbonBarPtr m_ribbonBar;

IUnknownPtr m_punkCP;

SolidEdgeFramework::ISEMousePtr m_Mouse;

 

public:

HRESULT Attach(void);

};

 

OBJECT_ENTRY_AUTO(__uuidof(Create), CCreate)

 

 

Posted by: assassin 007
Post date: 9/15/2009 4:39:34 AM

RE: How to hook command and mouse events in C++?

Create.cpp

// Create.cpp : CCreate ???

 

#include "stdafx.h"

#include "Create.h"

 

// CCreate

 

STDMETHODIMP CCreate::Init(IDispatch* pDispatch)

{

// TODO: ????????

MessageBox(NULL,"haha","xx",MB_OK);

pApplication=pDispatch;

m_cmd = pApplication->CreateCommand(SolidEdgeConstants::seNoDeactivate);

HRESULT hr = Attach();

hr = m_cmd->Start();

 

return S_OK;

}

 

HRESULT CCreate::Attach(void)

{

HRESULT hr = m_cmd->get_Window(&m_punkCP);

 

if (FAILED(hr)) return hr;

 

/* Attach to the command events */

hr = AtlAdvise(m_punkCP,

GetUnknown(),

IID_ISECommandEvents,

&m_eventToken);

 

if (FAILED(hr))

{

m_punkCP = NULL;

return hr;

}

 

/* Attach to the CommandWindow events */

hr = AtlAdvise(m_punkCP,

GetUnknown(),

IID_ISECommandWindowEvents,

&m_wndEventToken);

 

if (FAILED(hr))

{

AtlUnadvise(m_punkCP,

IID_ISECommandEvents,

m_eventToken);

m_punkCP = NULL;

return hr;

}

 

m_Mouse=m_cmd->Mouse;

 

hr = AtlAdvise(m_punkCP,

GetUnknown(),

IID_ISEMouseEvents,

&m_wndEventToken);

 

if (FAILED(hr))

{

AtlUnadvise(m_punkCP,

IID_ISEMouseEvents,

m_eventToken);

m_punkCP = NULL;

return hr;

}

 

return S_OK;

}

 

 

Posted by: assassin 007
Post date: 9/15/2009 4:41:10 AM

RE: How to hook command and mouse events in C++?

Note:

Step3: Right click CCreate in class view, and select Add->Implement Interface

 

Error Information:

 

1>Create.cpp

1>d:\microsoft visual studio 8\vc\atlmfc\include\atlcom.h(1868) : error C2259: 'ATL::CComObject' : cannot instantiate abstract class

1> with

1> [

1> Base=CCreate

1> ]

1> due to following members:

1> 'HRESULT SolidEdgeFramework::ISECommandEvents::raw_Activate(void)' : is abstract

1> e:\documents\visual studio 2005\projects\cmdtest\cmdtest\debug\framewrk.tlh(4091) : see declaration of 'SolidEdgeFramework::ISECommandEvents::raw_Activate'

1> 'HRESULT SolidEdgeFramework::ISECommandEvents::raw_Deactivate(void)' : is abstract

1> e:\documents\visual studio 2005\projects\cmdtest\cmdtest\debug\framewrk.tlh(4092) : see declaration of 'SolidEdgeFramework::ISECommandEvents::raw_Deactivate'

1> 'HRESULT SolidEdgeFramework::ISECommandEvents::raw_Terminate(void)' : is abstract

1> e:\documents\visual studio 2005\projects\cmdtest\cmdtest\debug\framewrk.tlh(4093) : see declaration of 'SolidEdgeFramework::ISECommandEvents::raw_Terminate'

1> 'HRESULT SolidEdgeFramework::ISECommandEvents::raw_Idle(long,VARIANT_BOOL *)' : is abstract

1> e:\documents\visual studio 2005\projects\cmdtest\cmdtest\debug\framewrk.tlh(4094) : see declaration of 'SolidEdgeFramework::ISECommandEvents::raw_Idle'

1> 'HRESULT SolidEdgeFramework::ISECommandEvents::raw_KeyDown(short *,short)' : is abstract

1> e:\documents\visual studio 2005\projects\cmdtest\cmdtest\debug\framewrk.tlh(4097) : see declaration of 'SolidEdgeFramework::ISECommandEvents::raw_KeyDown'

1> 'HRESULT SolidEdgeFramework::ISECommandEvents::raw_KeyPress(short *)' : is abstract

1> e:\documents\visual studio 2005\projects\cmdtest\cmdtest\debug\framewrk.tlh(4100) : see declaration of 'SolidEdgeFramework::ISECommandEvents::raw_KeyPress'

1> 'HRESULT SolidEdgeFramework::ISECommandEvents::raw_KeyUp(short *,short)' : is abstract

1> e:\documents\visual studio 2005\projects\cmdtest\cmdtest\debug\framewrk.tlh(4102) : see declaration of 'SolidEdgeFramework::ISECommandEvents::raw_KeyUp'

1> 'HRESULT SolidEdgeFramework::ISECommandWindowEvents::raw_WindowProc(IDispatch *,IDispatch *,unsigned int,UINT_PTR,LONG_PTR,LONG_PTR *)' : is abstract

1> e:\documents\visual studio 2005\projects\cmdtest\cmdtest\debug\framewrk.tlh(4032) : see declaration of 'SolidEdgeFramework::ISECommandWindowEvents::raw_WindowProc'

1> 'HRESULT SolidEdgeFramework::ISEMouseEvents::raw_MouseDown(short,short,double,double,double,IDispatch *,long,IDispatch *)' : is abstract

1> e:\documents\visual studio 2005\projects\cmdtest\cmdtest\debug\framewrk.tlh(3876) : see declaration of 'SolidEdgeFramework::ISEMouseEvents::raw_MouseDown'

1> 'HRESULT SolidEdgeFramework::ISEMouseEvents::raw_MouseUp(short,short,double,double,double,IDispatch *,long,IDispatch *)' : is abstract

1> e:\documents\visual studio 2005\projects\cmdtest\cmdtest\debug\framewrk.tlh(3885) : see declaration of 'SolidEdgeFramework::ISEMouseEvents::raw_MouseUp'

1> 'HRESULT SolidEdgeFramework::ISEMouseEvents::raw_MouseMove(short,short,double,double,double,IDispatch *,long,IDispatch *)' : is abstract

1> e:\documents\visual studio 2005\projects\cmdtest\cmdtest\debug\framewrk.tlh(3894) : see declaration of 'SolidEdgeFramework::ISEMouseEvents::raw_MouseMove'

1> 'HRESULT SolidEdgeFramework::ISEMouseEvents::raw_MouseClick(short,short,double,double,double,IDispatch *,long,IDispatch *)' : is abstract

1> e:\documents\visual studio 2005\projects\cmdtest\cmdtest\debug\framewrk.tlh(3903) : see declaration of 'SolidEdgeFramework::ISEMouseEvents::raw_MouseClick'

1> 'HRESULT SolidEdgeFramework::ISEMouseEvents::raw_MouseDblClick(short,short,double,double,double,IDispatch *,long,IDispatch *)' : is abstract

1> e:\documents\visual studio 2005\projects\cmdtest\cmdtest\debug\framewrk.tlh(3912) : see declaration of 'SolidEdgeFramework::ISEMouseEvents::raw_MouseDblClick'

1> 'HRESULT SolidEdgeFramework::ISEMouseEvents::raw_MouseDrag(short,short,double,double,double,IDispatch *,short,long,IDispatch *)' : is abstract

1> e:\documents\visual studio 2005\projects\cmdtest\cmdtest\debug\framewrk.tlh(3921) : see declaration of 'SolidEdgeFramework::ISEMouseEvents::raw_MouseDrag'

1> d:\microsoft visual studio 8\vc\atlmfc\include\atlcom.h(1857) : while compiling class template member function 'HRESULT ATL::CComCreator::CreateInstance(void *,const IID &,LPVOID *)'

1> with

1> [

1> T1=ATL::CComObject

1> ]

1> d:\microsoft visual studio 8\vc\atlmfc\include\atlcom.h(1948) : see reference to class template instantiation 'ATL::CComCreator' being compiled

1> with

1> [

1> T1=ATL::CComObject

1> ]

1> d:\microsoft visual studio 8\vc\atlmfc\include\atlcom.h(1944) : while compiling class template member function 'HRESULT ATL::CComCreator2::CreateInstance(void *,const IID &,LPVOID *)'

1> with

1> [

1> T1=ATL::CComCreator>,

1> T2=ATL::CComCreator>

1> ]

1> e:\documents\visual studio 2005\projects\cmdtest\cmdtest\create.h(137) : see reference to class template instantiation 'ATL::CComCreator2' being compiled

1> with

1> [

1> T1=ATL::CComCreator>,

1> T2=ATL::CComCreator>

1> ]

1>Generating Code...

1>Skipping... (no relevant changes detected)

1>CmdTest.cpp

1>???????“file://e:\Documents\Visual Studio 2005\Projects\CmdTest\CmdTest\Debug\BuildLog.htm”

1>CmdTest - 1 error,0 wanrning

 

 

Posted by: assassin 007
Post date: 9/15/2009 4:46:53 AM

RE: How to hook command and mouse events in C++?

Somebody can help me to solve the problem? Thanks!

 

Posted by: assassin 007
Post date: 9/15/2009 4:48:33 AM

RE: How to hook command and mouse events in C++?

Hi, All, I've solved the problem. After Step3, add prefix -- "raw_" to all functions which added by Visual Studio automatically. And then the command can be run, but all events is not be hooked.

Now, the issue is that how to modify the function named Attach()? Please help me, thanks!

 

Posted by: assassin 007
Post date: 9/16/2009 2:29:20 AM

RE: How to hook command and mouse events in C++?

Dear All, congratulate myself, I solved all issues. Summary:

1. Create an ATL Project

2. Add an ATL Simple Object, for example, CreateXXX;

3. Right click on CCreateXXX in Class View of Visual Studio IDE, and select add->Implement Interface

3. Select SolidEdgeFramework in combobox, and select ISECommandEvents, ISECommandWindowEvents,ISEMouseEvents in left listbox and add to right listbox; click OK;

4. Add prefix -- "raw_" to all functions which created automatically by Implement Interface Wizard, for example, STDMETHOD (raw_Activate)(void);

5. Add codes to stdafx.h, as following:

#include

#include

#include

#include

 

#include

#include

#include "secatids.h"

#include "addins_i.c"

 

#pragma warning( disable : 4278 )

#pragma warning( disable : 4146 )

 

#import "constant.tlb"

#import "framewrk.tlb" exclude ("UINT_PTR", "LONG_PTR")

#import "fwksupp.tlb"

#import "geometry.tlb"

#import "part.tlb"

#import "assembly.tlb"

#import "draft.tlb"

#include

 

#pragma warning( default : 4146 )

#pragma warning( default : 4278 )

 

/* Remark/Unremark usinge namespace statements as needed */

using namespace SolidEdgeFramework;

using namespace SolidEdgeFrameworkSupport;

using namespace SolidEdgeGeometry;

using namespace SolidEdgePart;

using namespace SolidEdgeAssembly;

using namespace SolidEdgeDraft;

6. Add method named Init(SolidEdgeFramework::ApplicationPtr app) to interface of ICreateXXX;

7. Add function named Attach() to class CCreateXXX;

8. Add codes to Attach() for hooking events as following:

hr = AtlAdvise(m_cmd,GetUnknown(), IID_ISECommandEvents, &m_eventToken);//hook command events;

HRESULT hr = m_cmd->get_Window(&m_punkCP);

AtlAdvise(m_punkCP, GetUnknown(), IID_ISECommandWindowEvents, &m_wndEventToken);//hook command window events

AtlAdvise(m_Mouse, GetUnknown(), IID_ISEMouseEvents, &m_wndEventToken);//hook mouse events

9. Add your codes

 

Posted by: assassin 007
Post date: 9/16/2009 4:21:44 AM