Solved

ATL: Fire events

Posted on 2001-08-31
7
477 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Is AWS.RDS considered a part of "Big Data"? 4 129
Where to obtain Oracle Data Provider for .NET 3.5 7 58
Trouble with References... 5 63
designing in object programming 12 105
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

749 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