When we are receiving an incoming event how we are calling the event function from the COM STDMETHOD using C++

  • Thread starter Thread starter bhavya internship
  • Start date Start date
B

bhavya internship

Guest
Hi All,

We are using COM library in our Windows C++ application.

The COM library (DeviceInterfaceLib) related Deviceinterface.tli and Deviceinterface.tlh are included in my project to use COM functionality.
I am new to COM technology and trying to understand COM and how it is using in my application.

In our application, we created PowerchuteDefines.h file by including .tlh file and the DeviceInterfaceLib in this file like as shown below:

PowerchuteDefines.h file:

//******************************************************************************
#include "Deviceinterface.tlh"

//******************************************************************************
using namespace DeviceInterfaceLib;
using namespace std;
//******************************************************************************

namespace PowerchuteDefines
{
template <class event_handler_class, typename device_interface, typename device_event_interface>
class TPowerchuteEventHandler : IDispatch
{
friend class class_event_handler;

typedef HRESULT (event_handler_class::*parent_on_invoke)
(
TPowerchuteEventHandler<event_handler_class, device_interface, device_event_interface>* pthis,
DISPID dispidMember,
REFIID riid,
LCID lcid,
WORD wFlags,
DISPPARAMS* pdispparams,
VARIANT* pvarResult,
EXCEPINFO* pexcepinfo,
UINT* puArgErr
);

public :
TPowerchuteEventHandler
(
event_handler_class& parent,
device_interface* pdevice_interface,
parent_on_invoke parent_on_invoke_function
) :
m_cRef(1),
m_parent(parent),
m_parent_on_invoke(parent_on_invoke_function),
m_pIConnectionPoint(0),
m_dwEventCookie(0)
{
SetupConnectionPoint(pdevice_interface);
}

~TPowerchuteEventHandler()
{
ShutdownConnectionPoint();
}

STDMETHOD_(ULONG, AddRef)()
{
InterlockedIncrement(&m_cRef);

return m_cRef;
}

When we are receiving any incoming event then the control is coming to the below function by hitting the break point. And then from here it is going to OnPowerchuteEvent function in PowerchuteImpl.cpp file.

I didn't understand how the below function is working?

Also from this function how it is going to OnPowerchuteEvent function as we are not invoking that function?

And also please help me why we are using this file (PowerchuteDefines.h) ?

STDMETHOD(Invoke)(DISPID dispidMember, REFIID riid,
LCID lcid, WORD wFlags, DISPPARAMS* pdispparams, VARIANT* pvarResult,
EXCEPINFO* pexcepinfo, UINT* puArgErr)
{
return (m_parent.*m_parent_on_invoke)(this, dispidMember, riid, lcid, wFlags, pdispparams, pvarResult, pexcepinfo, puArgErr);
}

protected :
LONG m_cRef;
event_handler_class& m_parent;
IConnectionPoint* m_pIConnectionPoint;
DWORD m_dwEventCookie;
parent_on_invoke m_parent_on_invoke;


void SetupConnectionPoint(device_interface* pdevice_interface)
{
IConnectionPointContainer* pIConnectionPointContainerTemp = NULL;
IUnknown* pIUnknown = NULL;

this -> QueryInterface(IID_IUnknown, (void**)&pIUnknown);

if (pIUnknown)
{
pdevice_interface -> QueryInterface (IID_IConnectionPointContainer, (void**)&pIConnectionPointContainerTemp);

if (pIConnectionPointContainerTemp)
{
pIConnectionPointContainerTemp -> FindConnectionPoint(__uuidof(device_event_interface), &m_pIConnectionPoint);
pIConnectionPointContainerTemp -> Release();
pIConnectionPointContainerTemp = NULL;
}

if (m_pIConnectionPoint)
{
m_pIConnectionPoint -> Advise(pIUnknown, &m_dwEventCookie);
}

pIUnknown -> Release();
pIUnknown = NULL;
}
}

enum PowerchuteDispId
{
DISPID_SESSIOM_CREATED_EVENT = 1,
DISPID_ESTABLISHED_EVENT = 2,
DISPID_INCOMING_SESSION_EVENT = 3,
};
}


Then in PowerchuteImpl.cpp file, the event function is written like as shown below:

HRESULT CPowerchuteImpl::OnPowerchuteEvent(
IPowerchuteEventHandler* ,
DISPID dispidMember,
REFIID ,
LCID ,
WORD ,
DISPPARAMS* pdispparams,
VARIANT* ,
EXCEPINFO* ,
UINT*
)
{
short lValue = 0;
lValue = V_I2(&(pdispparams->rgvarg[0]));

switch(dispidMember)
{
case DISPID_SESSIOM_CREATED_EVENT:
CPowerchuteImpl::onSessionCreatedEvent(lValue);
break;
case DISPID_ESTABLISHED_EVENT:
CPowerchuteImpl::onSessionEstablishedEvent(lValue);
break;
case DISPID_INCOMING_SESSION_EVENT:
CPowerchuteImpl::onIncomingSessionEvent(lValue);
break;
}
return S_OK;
}

In the constructor we are initializing the COM library like as shown below:

CPowerchuteImpl::CPowerchuteImpl(
{
CoInitialize(NULL);
HRESULT hr = m_IPowerchuteObject.CreateInstance(__uuidof(PowerchuteInterface));
}

Thank you in advance.

Continue reading...
 
Back
Top