Is it possible to intercept (or be aware of) COM Reference counting on CLR objects exposed to COM

前端 未结 10 1087
礼貌的吻别
礼貌的吻别 2020-12-08 02:56

I have rephrased this question.

When .net objects are exposed to COM Clients through COM iterop, a CCW (COM Callable Wrapper) is created, this sits between

10条回答
  •  醉酒成梦
    2020-12-08 03:47

    I guess the reason for this not being possible is that a refcount of 0 does not mean that the object is not in use, because you might have a call graph like

    VB_Object
       |
       V
       |
    Managed1 -<- Managed2
    

    in which case the object Managed1 is still in use even if the VB object drops its reference to it and its refcount therefore is 0.

    If you really need to do what you say, I guess you could create wrapper classes in unmanaged C++, which invokes the Dispose method when the refcount drops to 0. These classes could probably be codegen'd from metadata, but I have no experience whatsoever in how to do implement this kind of thing.

提交回复
热议问题