Solved

ATL: Fire events

Posted on 2001-08-31
7
472 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

708 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