Problems creating COM event handler

  • Thread starter Thread starter thoatson
  • Start date Start date
T

thoatson

Guest
I have been able to successfully invoke functions in a COM interface. But I continue to struggle with creating an event handler for the same COM object.

My code contains the following import statement:

#import "LogosCOM.tlb"


And this .tlb file was created from the .exe file which contains the COM object. The import creates 2 files - logoscom.tli and logoscom.tlh - which provides the interface and the header file definitions, respectively.

In the .tlh file, I find the following:

struct __declspec(uuid("d80579b0-eb69-4217-a170-f0bec30b2672"))
ILogosApplicationEvents : IDispatch
{
// Methods:
HRESULT PanelOpened(IDispatch *Panel);
HRESULT PanelActivated(IDispatch *Panel);
HRESULT PanelChanged(IDispatch *Panel, IDispatch* Hint);
HRESULT PanelClosed(IDispatch * Panel);
HRESULT Exiting();
};


I want my program to handle the "PanelChanged" event.

I set things up in my C++ code based on this online documentation:
Event Handling in Native C++

I tried following the example, and got 95% of the way there, but ran into one small(?) error... Here's the relevant code I created:

[event_receiver(native)]
class CEventReceiver
{
public:
void OnLogosPanelChanged(IDispatch * Panel, IDispatch * Hint)
{
printf_s("OnLogosPanelChanged was called.\n");
}

void hookEvent(Logos4Lib::ILogosApplicationEvents *pEventSource)
{
__hook(&Logos4Lib::ILogosApplicationEvents::PanelChanged, pEventSource,
&CEventReceiver::OnLogosPanelChanged);
}

void unhookEvent(Logos4Lib::ILogosApplicationEvents *pEventSource)
{
__unhook(&Logos4Lib::ILogosApplicationEvents::PanelChanged, pEventSource,
&CEventReceiver::OnLogosPanelChanged);
}
};

OnLogosPanelChanged() is the callback function. The hookEvent() method tells the COM object to use this callback function to handle the specified event.

Then I put this code elsewhere in my program (where I set up the connection to the COM object):

Logos4Lib::ILogosApplicationEvents eventSource;
CEventReceiver eventReceiver;
eventReceiver.hookEvent(&eventSource);


The IDE flags an error on the declaration of eventSource: "error C2259: 'Logos4Lib::ILogosApplicationEvents': cannot instantiate abstract class".

I'm not sure why this is abstract, given its definition above. When I try to compile, I get more error messages:

linklogos.cpp(39): warning C4467: usage of ATL attributes is deprecated

This relates to the line:

[event_receiver(native)]


linklogos.cpp(50): error C3723: 'Logos4Lib::ILogosApplicationEvents::PanelChanged': could not resolve event
logoscom.tli(113): note: see declaration of 'Logos4Lib::ILogosApplicationEvents::PanelChanged'
linklogos.cpp(50): note: The event handler is one of:
linklogos.cpp(50): note: could be 'void CEventReceiver::OnLogosPanelChanged(IDispatch *,IDispatch* )'
linklogos.cpp(50): note: There are no events:
logoscom.tli(113): note: 'HRESULT Logos4Lib::ILogosApplicationEvents::PanelChanged(IDispatch *,IDispatch* )': is not an event

This error relates to the line:

__hook(&Logos4Lib::ILogosApplicationEvents::PanelChanged, pEventSource,
&CEventReceiver::OnLogosPanelChanged);


linklogos.cpp(56): error C3723: 'Logos4Lib::ILogosApplicationEvents::PanelChanged': could not resolve event
logoscom.tli(113): note: see declaration of 'Logos4Lib::ILogosApplicationEvents::PanelChanged'
linklogos.cpp(56): note: The event handler is one of:
linklogos.cpp(56): note: could be 'void CEventReceiver::OnLogosPanelChanged(IDispatch *,IDispatch* )'
linklogos.cpp(56): note: There are no events:
logoscom.tli(113): note: 'HRESULT Logos4Lib::ILogosApplicationEvents::PanelChanged(IDispatch *,IDispatch* )': is not an event

This error relates to the line:

__unhook(&Logos4Lib::ILogosApplicationEvents::PanelChanged, pEventSource,
&CEventReceiver::OnLogosPanelChanged);


linklogos.cpp(75): error C2259: 'Logos4Lib::ILogosApplicationEvents': cannot instantiate abstract class
linklogos.cpp(75): note: due to following members:
linklogos.cpp(75): note: 'HRESULT IUnknown::QueryInterface(const IID &,void **)': is abstract
c:\program files (x86)\windows kits\10\include\10.0.16299.0\um\unknwnbase.h(113): note: see declaration of 'IUnknown::QueryInterface'
linklogos.cpp(75): note: 'ULONG IUnknown::AddRef(void)': is abstract
c:\program files (x86)\windows kits\10\include\10.0.16299.0\um\unknwnbase.h(117): note: see declaration of 'IUnknown::AddRef'
linklogos.cpp(75): note: 'ULONG IUnknown::Release(void)': is abstract
c:\program files (x86)\windows kits\10\include\10.0.16299.0\um\unknwnbase.h(119): note: see declaration of 'IUnknown::Release'
linklogos.cpp(75): note: 'HRESULT IDispatch::GetTypeInfoCount(UINT *)': is abstract
c:\program files (x86)\windows kits\10\include\10.0.16299.0\um\oaidl.h(2204): note: see declaration of 'IDispatch::GetTypeInfoCount'
linklogos.cpp(75): note: 'HRESULT IDispatch::GetTypeInfo(UINT,LCID,ITypeInfo **)': is abstract
c:\program files (x86)\windows kits\10\include\10.0.16299.0\um\oaidl.h(2207): note: see declaration of 'IDispatch::GetTypeInfo'
linklogos.cpp(75): note: 'HRESULT IDispatch::GetIDsOfNames(const IID &,LPOLESTR *,UINT,LCID,DISPID* )': is abstract
c:\program files (x86)\windows kits\10\include\10.0.16299.0\um\oaidl.h(2212): note: see declaration of 'IDispatch::GetIDsOfNames'
linklogos.cpp(75): note: 'HRESULT IDispatch::Invoke(DISPID,const IID &,LCID,WORD,DISPPARAMS *,VARIANT* ,EXCEPINFO *,UINT* )': is abstract
c:\program files (x86)\windows kits\10\include\10.0.16299.0\um\oaidl.h(2219): note: see declaration of 'IDispatch::Invoke'

This error relates to the line:

Logos4Lib::ILogosApplicationEvents eventSource;


The first error message is perhaps the most troubling... Any ideas?

Continue reading...
 
Back
Top