Solved

ATL: Fire events

Posted on 2001-08-31
7
476 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

860 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