Solved

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

Posted on 2002-04-03
4
425 Views
Last Modified: 2013-11-25
Hello,

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();
          }
     }
     else
     {
          _com_error e(hr);
          m_DOSError= true;
          m_LastDOSCommand= "CreateInstance(__uuidof(SparkyObject::SparkyCOM)))";
          m_LastError= e.ErrorMessage();
     }
     if (aPtr)
          aPtr.Release();

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.


0
Comment
Question by:aquila98
4 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 6915533
Check out http://www.codeguru.com/atl/events.html on this issue...
0
 
LVL 2

Author Comment

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

??????????

0
 

Accepted Solution

by:
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:

VERIFY(m_YourComSink);
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...

enjoy
0
 
LVL 49

Expert Comment

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

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

776 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