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

Posted on 2002-04-03
Last Modified: 2013-11-25

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.

Question by:aquila98
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 86

Expert Comment

ID: 6915533
Check out on this issue...

Author Comment

ID: 6915872
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...)



Accepted Solution

amidale earned 250 total points
ID: 6928707
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...

LVL 49

Expert Comment

ID: 7016876
hi aquila98,
Are you still looking for an answer to this question?
-- Dan

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question