Hi Ying-Shen
I will try and describe my implementation in as much detail as
possible if this will be of help.
On the managed side of things, I have declared an interface which will
be the source of events. I am using the InterfaceType attribute to
mark the interface as a dispatch interface, also the DispId attribute
is used on each method so that GetIDsOfNames is not called on the sink
for each event invocation.
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IClass1Events
{
[DispId(1)]
void TextChanged();
}
The class which uses this interface is what I was referring to in the
other posts as the managed event source. I have used the
ComSourceInterfaces attribute on this class to indicate that the
IClass1Events interface is the COM source interface. An instance of
this class is created when the application first starts up and remains
alive until the application closes.
public delegate void TextChangedEvent();
[ComSourceInterfaces("CSDemo.IClass1Events")]
public class Class1
{
public event TextChangedEvent TextChanged;
public string Text
{
...
set
{
...
TextChanged();
}
}
}
The unmanaged component is an ActiveX control which I have placed onto
a Windows Form, this generated a RCW that I am using to call methods
on the ActiveX control from within managed code. One of the methods of
the ActiveX control takes an IDispatch pointer to an automation object
and then starts sinking all events from this object. The RCW expects
an object to be passed rather than an IDpsiatch pointer and so I can
easily pass an instance of my class (Class1) instead.
Inside the ActiveX control, the connection to the event source is made
using AtlAdvise (which calls IConnectionPoint::Advise internally).
if(!SUCCEEDED(AtlAdvise(m_spSourceDispatch,
(IUnknown*)this,m_SourceEventInterfaceGuid,&m_Cookie)))
{
*This error never occurs.
}
m_spSourceDispatch = the IDispatch interface for Class1.
this = the event sink.
m_SourceEventInterfaceGuid = the GUID of IClass1Events (obtained
dynamically from type information).
m_Cookie = the connection cookie.
To disconnect from the event source, AtlUnadvise is used (which calls
IConnectionPoint::Undvise internally).
if(!SUCCEEDED(AtlUnadvise(
m_spSourceDispatch,m_SourceEventInterfaceGuid,m_Cookie)))
{
*This error never occurs.
}
If I set a breakpoint on the disconnection line, I can see the
reference count of the sink remains exactly the same after AtlUnadvise
has been called as it was before, but for normal COM objects, the
reference count is decremented as part of the unadvise call. The
reference count of the sink does finally get decremented, but only
when the application exits (maybe because of garbage collection?).
I hope this is enough information, Ying-Shen, if you need anymore,
then I can post some additional code.
Many Thanks
David Shepherd.
> Hi dave,
> Thanks for your detail explaination, My understanding now is you have
[quoted text clipped - 143 lines]
> | > |
> | > | David Shepherd
Ying-Shen Yu[MS] - 11 Sep 2003 14:43 GMT
Hi David,
Thanks for your detail information, and I think I found some clue in
the MSDN,
you may find the following words,
"Each RCW maintains a cache of interface pointers on the COM object it
wraps and releases its reference on the COM object when the RCW is no
longer needed. The runtime performs garbage collection on the RCW."
for more information you may refer to this link
ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconruntimecallablewrapper.h
tm
Now I'm doing research to find if there is an work around, I'll update you
as soon as possible.
Thanks!
Best regards,
Ying-Shen Yu [MS]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------
| From: dave@daveshep.com (David Shepherd)
| Newsgroups: microsoft.public.dotnet.framework.interop
[quoted text clipped - 4 lines]
| Message-ID: <82349813.0309100838.7502903c@posting.google.com>
| References: <82349813.0309080755.6ce37d5d@posting.google.com>
<u5sGebrdDHA.2536@cpmsftngxa06.phx.gbl>
<82349813.0309090623.54870791@posting.google.com>
<9#Y5195dDHA.1120@cpmsftngxa06.phx.gbl>
| NNTP-Posting-Host: 81.129.59.76
| Content-Type: text/plain; charset=ISO-8859-1
| Content-Transfer-Encoding: 8bit
| X-Trace: posting.google.com 1063211921 11949 127.0.0.1 (10 Sep 2003
16:38:41 GMT)
| X-Complaints-To: groups-abuse@google.com
| NNTP-Posting-Date: 10 Sep 2003 16:38:41 GMT
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!sn-xit-03!sn-xit-01!sn-
xit-09!supernews.com!postnews1.google.com!not-for-mail
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.interop:18675
| X-Tomcat-NG: microsoft.public.dotnet.framework.interop
[quoted text clipped - 128 lines]
| > | NNTP-Posting-Date: 9 Sep 2003 14:23:28 GMT
| > | Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!sn-xit-03!sn-xit-01!sn-
| > xit-09!supernews.com!postnews1.google.com!not-for-mail
| > | Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.interop:18632
[quoted text clipped - 43 lines]
| > | >
| >
ms-help://MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemruntimeinteropservic
| > | > esmarshalclassreleasetopic.htm
| > | > If you still have problems on it, please reply me with more
[quoted text clipped - 29 lines]
| > | >
| >
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
| >
e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!sn-xit-03!sn-xit-06!sn-
| > | > xit-09!supernews.com!postnews1.google.com!not-for-mail
| > | > | Xref: cpmsftngxa06.phx.gbl
[quoted text clipped - 18 lines]
| > | > |
| > | > | David Shepherd
Liju Thomas [MS] - 12 Sep 2003 23:02 GMT
Hi Dave,
Can you send a repro as an attachment so that we can reproduce the
behaviour you are seeing and troubleshoot it?
thanks,
Liju Thomas
This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use.
?2001 Microsoft Corporation. All rights reserved.
--------------------
| From: dave@daveshep.com (David Shepherd)
| Newsgroups: microsoft.public.dotnet.framework.interop
[quoted text clipped - 4 lines]
| Message-ID: <82349813.0309100838.7502903c@posting.google.com>
| References: <82349813.0309080755.6ce37d5d@posting.google.com>
<u5sGebrdDHA.2536@cpmsftngxa06.phx.gbl>
<82349813.0309090623.54870791@posting.google.com>
<9#Y5195dDHA.1120@cpmsftngxa06.phx.gbl>
| NNTP-Posting-Host: 81.129.59.76
| Content-Type: text/plain; charset=ISO-8859-1
| Content-Transfer-Encoding: 8bit
| X-Trace: posting.google.com 1063211921 11949 127.0.0.1 (10 Sep 2003
16:38:41 GMT)
| X-Complaints-To: groups-abuse@google.com
| NNTP-Posting-Date: 10 Sep 2003 16:38:41 GMT
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!sn-xit-03!sn-xit-01!sn-
xit-09!supernews.com!postnews1.google.com!not-for-mail
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.interop:18675
| X-Tomcat-NG: microsoft.public.dotnet.framework.interop
[quoted text clipped - 128 lines]
| > | NNTP-Posting-Date: 9 Sep 2003 14:23:28 GMT
| > | Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!sn-xit-03!sn-xit-01!sn-
| > xit-09!supernews.com!postnews1.google.com!not-for-mail
| > | Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.interop:18632
[quoted text clipped - 43 lines]
| > | >
| >
ms-help://MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemruntimeinteropservic
| > | > esmarshalclassreleasetopic.htm
| > | > If you still have problems on it, please reply me with more
[quoted text clipped - 29 lines]
| > | >
| >
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
| >
e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!sn-xit-03!sn-xit-06!sn-
| > | > xit-09!supernews.com!postnews1.google.com!not-for-mail
| > | > | Xref: cpmsftngxa06.phx.gbl
[quoted text clipped - 18 lines]
| > | > |
| > | > | David Shepherd
This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use.
?2001 Microsoft Corporation. All rights reserved.