Go Premium for a chance to win a PS4. Enter to Win

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 454
  • 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

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

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