Board index » cppbuilder » Client-side Events BCB4

Client-side Events BCB4

Hi gang -

Who's worked with client side events in BCB4?

I wrote a small BCB server and checked the "generate events" code. I see how
the generated code on the server works and how to fire_ the events.

But things aren't so clear on the client side. For example, how do I
implement the outgoing interface that the server will fire_?  And who
handles Advise and Unadvise?

I looked through Borland's site and found little..

Any input or direction would be great. In the meantime I'm going to paw
through UTICLS and other files and see if I can figure it out from the
inside-out.

Thanks!
Michael

 

Re:Client-side Events BCB4


Michael,

Here is a sample client code, modified from example provided either by Bruneau
or Robert (don't remember who, sorry):

class SampleEventHandler : public TEventDispatcher<SampleEventHandler,
&DIID_ISampleServiceEvents>
{
protected:
  // This is the only function we need to override to handle events.
  // The guts of the support is handled by TEventDispatcher<>
  HRESULT InvokeEvent(DISPID id, TVariant* params) {
      if (id == 1)
        HandleEvent1(params);
      // else if (id == 2) ... etc.
      return S_OK;
  }

  void HandleEvent1(TVariant * params) {
     // do whatever needs to be done
 }

public:
  // Connect to Server
  void Connect() {
    if (!server) {
      server = CoSampleService::Create();
      ConnectEvents(server);
    }
  }

  // Disconnect from Server
  void Disconnect() {
    if (server) {
      DisconnectEvents(server);
      server.Unbind();
    }
  }

 SampleServicePtr  server;    // Copy of smart pointer to server

Quote
};

//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
   : TForm(Owner)
{
Quote
}

//---------------------------------------------------------------------------

void __fastcall TMainForm::Button1Click(TObject *Sender)
{
   TInitOle initOle;
   SampleEventHandler eh;
   eh.Connect();
   eh.server->SampleMethod(); // triggers events
   eh.Disconnect();

Quote
}

//---------------------------------------------------------------------------

Quote
Michael Lovett wrote:

[snip]

Re:Client-side Events BCB4


TEventsDispatcher ( utilcls.h, after patch #1 ) provides the plumbing
for sinking events. Code Central has a sample of sinking events
generated by MSWord.

HTH,
Alex

Quote
Michael Lovett wrote:

[snip]

--
HotSend - portable documents technology
http://www.hotsend.com/
eFax - get your faxes via email - Free !
http://www.efax.com

Re:Client-side Events BCB4


Thanks, Jack.

At first this didn't help because I didn't understand the bigger picture of
IConnectionPoint, IConnectionPointContainer, and ATL's support for these.
Now that I do, TEventDispatcher makes a lot of sense and I feel comfortable
using it :-)

BUT! The compiler is erroring when I base a class on TEventDispatcher and
try to compile it. There error I'm getting is:

[C++ Error] utilcls.h(2411): E2093 'operator*' not implemented in type
'_GUID' for arguments of the same type.

All I have to do is try to instantiate my derived class to get this error.
Here's
the paragraph of code in utilcls.h:
-------------------

// TEventsDispatcher::IUnknown::QueryInterface
//
template <class T, const IID *pEVENTSIID, class BASE> HRESULT
STDMETHODCALLTYPE
TEventDispatcher<T, pEVENTSIID, BASE>::QueryInterface(REFIID iid, void
**ppvObject)
{
  *ppvObject = 0;
  if ((iid == IID_IDispatch) || (iid == *pEVENTSIID))
!!!!!!!!!!!!!!!ERROR
  {
    /*static_cast<T*>*/(this)->AddRef();
    *ppvObject = static_cast<IDispatch*>(&m_Dispatch);
  }
  else if (iid == IID_IUnknown)
  {
    /*static_cast<T*>*/(this)->AddRef();
    *ppvObject = static_cast<IUnknown*>(&m_Dispatch);
  }
  else
    return E_NOINTERFACE;
  return S_OK;

Quote
}

-----------------------
Any ideas?

Thanks,
Michael

Re:Client-side Events BCB4


Oops!

When I used the template, I forgot to put the leading & before the DIID!

Thanks, all is working well now!

Michael

Other Threads