Solved

ATL: Fire events

Posted on 2001-08-31
7
473 Views
Last Modified: 2013-11-25
i have a component in which i wish to fire an event like

     dispinterface _IMyComponentEvents
     {
          properties:
          methods:
          [id(0x00000009), helpstring("method Validate")] HRESULT Validate(VARIANT_BOOL *Cancel);
     }

so.. i have a "Validate" event..
somewhere in my code i wish to fire that event and, if implemented by the containing form, continue or not the execution by checking the returned "Cancel" variable..
thus.. somewhere in my code (let's say when i loose focus) i do

     VARIANT_BOOL *pbCancel = new VARIANT_BOOL(0);

     Fire_Validate(pbCancel);
     
     if (*pbCancel)
     {
          SetFocus();
     }
     else
     {
          //do my stuff
     }

maybe i am wrong (because my deficitary knowledge in ATL/COM), but doing this seems the right thing to me..
even so, this code crashes on Fire_Validate.. it's clear is a problem when passing the pointer further, but i've tried to allocate it staticaly or dynamically, but i have the same result (access violation).

maybe someone had this problem before or knows how to handle it..
0
Comment
Question by:lame_programmer
  • 4
  • 3
7 Comments
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6444200
Is this code operating in a separate thread?
0
 

Author Comment

by:lame_programmer
ID: 6444235
yes, it is in a separate thread..
the control was created having apartment model.. and it also has free threaded marshaler (but this thing does not have any incidence here)..
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6444316
I think you are facing the standard problem here about firing events from a worker thread,  IMO ATL
is not too helpful here since this is probably one of the most common usage patterns for this sort of
thing.

Probably you have two choices,  you can marshal the interface pointer that you pass to the worker thread
in a stream using:

CoMarshalInterThreadInterfaceInStream

and then, inside the thread, you unmarshal it again with:

CoGetInterfaceAndReleaseStream

and then you can call the event from this unmarshaled pointer.

Another possibility is to use the Global Interface Table (GIT) to handle the marshaling of the pointers.

Take a look at this MSDN article, it demonstrates some of the techniques:

http://support.microsoft.com/support/kb/articles/Q206/0/76.ASP 
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:lame_programmer
ID: 6444561
i've modified the Fire_Validate

HRESULT Fire_Validate(VARIANT_BOOL * pCancel)
{
     [..snip..]
     pvars[0] = pCancel;
     DISPPARAMS disp = { pvars, NULL, 1, 0 };
     pDispatch->Invoke(0x9, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &disp, &varResult, NULL, NULL);
}

to
     [..]
     pvars[0] = long(pCancel);
     [..]

and it worked fine..

what is the problem with VARIANT_BOOL (which in fact is short*) and how can i prevent it without modifying connection points' file?..
0
 

Author Comment

by:lame_programmer
ID: 6444593
- i don't think i need marshaling for VARIANT_BOOL*, which should be done "by default"..
- i still want to give the points to someone who shows me "how this must be done", i don't accept my solution, even if it works, because _it's lame_.. there must be a way to be done "like it should be"..
0
 
LVL 9

Accepted Solution

by:
jasonclarke earned 200 total points
ID: 6444827
Oh, ok, this is something different...usually you have problems calling an event from a separate thread from the main component apartment thread,  your use of the FTM may be saving you here in this case.

The auto-generated code to fire events is not perfect - I am not sure it is actually well suited to code like this that returns a value.  I suspect the VARIANT tag that it passes to IDispatch->Invoke should be VT_BOOL | VT_BYREF and I don't think the CComVariant constructor can handle that.

It is not a terrible thing to modify this code yourself - you can copy this method out of the generated file and include it in your own COM class header...the generated code can then be discarded.

A similar thing is recommended by MSDN in relation to a related bug:

http://support.microsoft.com/support/kb/articles/Q250/8/47.ASP
0
 

Author Comment

by:lame_programmer
ID: 6445001
thank you for your help (and for the time you spent reading my bad english)..
this solved my problem, and seems to be right.. but still, if i try to implement my Validate event in visual basic it crashes after getting out of my handle.. it works only in Visual C (it's ok for me, because there i'm gonna use it, but i guess something more i'll have to do to make it ok in VB too - to make the component "reliable")
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now