Solved

ATL: Fire events

Posted on 2001-08-31
7
478 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
[X]
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
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

738 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