• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 483
  • Last Modified:

How do I set up an Event handler in C++ for VB COM object???


I am using a VB com object which has an Event triggered...
I would like to use the see behavior in C++ as in VB but I do not know how to
set up the event handler in C++ for my COM object!

In VB I just do a dim WithEvents and I get to set up a event function...
But in C++ how do I do the equivalent???

Here is my code:
#import "myvbcom.dll"


SparkyObject::_SparkyCOMPtr aPtr= NULL;
HRESULT hr= aPtr.CreateInstance(__uuidof(SparkyObject::SparkyCOM));
//SparkyObject::__SparkyCOMPtr aPtrCallback= NULL;
if (SUCCEEDED(hr))
          COleDateTime t(y,m,d,0,0,0);
          DATE d= (DATE)t;
          if (!aPtr->PreparePeopleData(&d))
               m_DOSError= true;
               m_LastDOSCommand= "PreparePeopleData";
               m_LastError= (char*)aPtr->getLastErrorText();
          _com_error e(hr);
          m_DOSError= true;
          m_LastDOSCommand= "CreateInstance(__uuidof(SparkyObject::SparkyCOM)))";
          m_LastError= e.ErrorMessage();
     if (aPtr)

In the .tlh file (from the #import statement) I see that I have this:
a _SparkyCOM ptr which I use and a __SparkyCOM which seems to be handling
the event... But how do I use it?
The __SparkyCOM object is defined thus in the .tlh file:
__SparkyCOM : IDispatch
    // Wrapper methods for error-handling

    // Methods:
    HRESULT FileProcessing (
        BSTR * strProcess,
        BSTR * strProcessInfo,
        VARIANT_BOOL * btnCancel );

The "FileProcessing" is the event I'd like to trap in C++.

How Do I set up my event handler (say a static function with the same prototype)
in my code so that my com object (aPtr) "knows" to use it???

Thanks for any informations.

1 Solution
Check out http://www.codeguru.com/atl/events.html on this issue...
aquila98Author Commented:
not much help there, my event class does not support IUnknown (its all made in VB)

I tried to use atladvise thus:
hr= AtlAdvise(aPtr, aPtrCallback,aPtrCallback.GetIID(), &m_dwCookie);
but I get a E_NOINTERFACE error...
both aPtrCallback and aPtr are created from the same com object type
(HRESULT hr= aPtr.CreateInstance(__uuidof(SparkyObject::SparkyCOM)); and
hr= aPtrCallback.CreateInstance(__uuidof(SparkyObject::SparkyCOM)); since
this is the ONLY COM object I can create...)


All you need to do is to create a class derievd from CCmdTarget...
In it there will be a member responsible for handling your VB event.

Now to link the VB com to this MFC class all you need to do is use AtlAdvise/AtlUnadvise thusly:

LPUNKNOWN pUnk= m_YourComSink->GetIDispatch(FALSE);
AfxConnectionAdvise(m_YourVBCom, DIID___VBEVENT, pUnk, FALSE, &m_YourEventHandlerComCookie);

Now getting DIID__ is somewhat tricky... Use oleview to get
the idl for you VB dll. Compile it under VC to get the _i.c
file. In it each event if a dispatch and gets a DIID_ tag.

CUT and paste the DIID def in your code and voila!

Don't forget to use atlunadvise once you're done.

I can send a bit of my code if your need further examples...

hi aquila98,
Are you still looking for an answer to this question?
-- Dan
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now