gpt4 book ai didi

c# - 是否有可能拦截(或意识到)COM 对暴露给 COM 的 CLR 对象的引用计数

转载 作者:可可西里 更新时间:2023-11-01 03:00:41 34 4
gpt4 key购买 nike

我已经改写了这个问题。

当 .net 对象通过 COM 迭代操作暴露给 COM 客户端时,将创建一个 CCW ( COM Callable Wrapper ),它位于 COM 客户端和托管 .net 对象之间。

在 COM 世界中,对象会记录其他对象对它的引用数。当引用计数变为零时,对象将被删除/释放/收集。这意味着 COM 对象终止是确定性的(我们在 .net 中使用 Using/IDispose 来确定性终止,对象终结器是非确定性的)。

每个 CCW 都是一个 COM 对象,并且它像任何其他 COM 对象一样被引用计数。当 CCW 终止(引用计数变为零)时,GC 将无法找到 CCW 包装的 CLR 对象,并且 CLR 对象符合收集条件。快乐的日子,世界上一切都好。

我想做的是在 CCW 终止时(即当它的引用计数变为零时)进行捕获,并以某种方式向 CLR 对象发出信号(例如,通过在托管对象上调用 Dispose 方法)。

那么,是否有可能知道 COM Callable Wrapper 的引用计数何时发生? CLR 类的归零?
和/或
是否可以在 .net 中为 CCW 提供我的 AddRef 和 ReleaseRef 实现?

如果不是,另一种方法是在 ATL 中实现这些 DLL(我不需要任何 ATL 方面的帮助,谢谢)。这不会是火箭科学,但我不愿意这样做,因为我是唯一拥有任何真实世界 C++ 或任何 ATL 的内部开发人员。

背景
我正在 .net 中重写一些旧的 VB6 ActiveX DLL(准确地说是 C#,但这更像是一个 .net/COM 互操作问题,而不是 C# 问题)。一些旧的 VB6 对象依赖于引用计数来在对象终止时执行操作(请参阅上面对引用计数的解释)。这些 DLL 不包含重要的业务逻辑,它们是我们提供给使用 VBScript 与我们集成的客户的实用程序和辅助函数。

我不想做的事

  • 改为引用计数 .net 对象使用垃圾收集器。我对 GC 很满意,我的问题不在于 GC。
  • 使用对象终结器。终结者是不确定的,在这种情况下我需要确定性终止(如.net 中的 Using/IDispose 习语)
  • 在非托管 C++ 中实现 IUnknown
    如果我必须走 C++ 路线,我会使用ATL,谢谢。
  • 使用 Vb6 解决这个问题,或者重新使用VB6 对象。整个点这个练习是删除我们的构建依赖于 Vb6。

谢谢
体重

已接受的答案
千人感谢Steve Steiner ,谁提出了唯一的(可能可行的)基于 .net 的答案,以及 Earwicker ,他想出了一个非常简单的 ATL 解决方案。

然而,接受的答案是 Bigtoe ,他建议将 .net 对象包装在 VbScript 对象中(我不认为这是诚实的),有效地为 VbScript 问题提供了一个简单的 VbScript 解决方案。

感谢大家。

最佳答案

我意识到这是一个有点老的问题,但我确实在一段时间前收到了实际的工作请求。

它所做的是用自定义实现替换所创建对象的 VTBL(s) 中的 Release,当所有引用都已释放时调用 Dispose。请注意,不能保证这将始终有效。主要假设是标准 CCW 的所有接口(interface)上的所有 Release 方法都是相同的方法。

使用风险自负。 :)

/// <summary>
/// I base class to provide a mechanism where <see cref="IDisposable.Dispose"/>
/// will be called when the last reference count is released.
///
/// </summary>
public abstract class DisposableComObject: IDisposable
{
#region Release Handler, ugly, do not look

//You were warned.


//This code is to enable us to call IDisposable.Dispose when the last ref count is released.
//It relies on one things being true:
// 1. That all COM Callable Wrappers use the same implementation of IUnknown.


//What Release() looks like with an explit "this".
private delegate int ReleaseDelegate(IntPtr unk);

//GetFunctionPointerForDelegate does NOT prevent GC ofthe Delegate object, so we'll keep a reference to it so it's not GC'd.
//That would be "bad".
private static ReleaseDelegate myRelease = new ReleaseDelegate(Release);
//This is the actual address of the Release function, so it can be called by unmanaged code.
private static IntPtr myReleaseAddress = Marshal.GetFunctionPointerForDelegate(myRelease);


//Get a Delegate that references IUnknown.Release in the CCW.
//This is where we assume that all CCWs use the same IUnknown (or at least the same Release), since
//we're getting the address of the Release method for a basic object.
private static ReleaseDelegate unkRelease = GetUnkRelease();
private static ReleaseDelegate GetUnkRelease()
{
object test = new object();
IntPtr unk = Marshal.GetIUnknownForObject(test);
try
{
IntPtr vtbl = Marshal.ReadIntPtr(unk);
IntPtr releaseAddress = Marshal.ReadIntPtr(vtbl, 2 * IntPtr.Size);
return (ReleaseDelegate)Marshal.GetDelegateForFunctionPointer(releaseAddress, typeof(ReleaseDelegate));
}
finally
{
Marshal.Release(unk);
}
}

//Given an interface pointer, this will replace the address of Release in the vtable
//with our own. Yes, I know.
private static void HookReleaseForPtr(IntPtr ptr)
{
IntPtr vtbl = Marshal.ReadIntPtr(ptr);
IntPtr releaseAddress = Marshal.ReadIntPtr(vtbl, 2 * IntPtr.Size);
Marshal.WriteIntPtr(vtbl, 2 * IntPtr.Size, myReleaseAddress);
}

//Go and replace the address of CCW Release with the address of our Release
//in all the COM visible vtables.
private static void AddDisposeHandler(object o)
{
//Only bother if it is actually useful to hook Release to call Dispose
if (Marshal.IsTypeVisibleFromCom(o.GetType()) && o is IDisposable)
{
//IUnknown has its very own vtable.
IntPtr comInterface = Marshal.GetIUnknownForObject(o);
try
{
HookReleaseForPtr(comInterface);
}
finally
{
Marshal.Release(comInterface);
}
//Walk the COM-Visible interfaces implemented
//Note that while these have their own vtables, the function address of Release
//is the same. At least in all observed cases it's the same, a check could be added here to
//make sure the function pointer we're replacing is the one we read from GetIUnknownForObject(object)
//during initialization
foreach (Type intf in o.GetType().GetInterfaces())
{
if (Marshal.IsTypeVisibleFromCom(intf))
{
comInterface = Marshal.GetComInterfaceForObject(o, intf);
try
{
HookReleaseForPtr(comInterface);
}
finally
{
Marshal.Release(comInterface);
}
}
}
}
}

//Our own release. We will call the CCW Release, and then if our refCount hits 0 we will call Dispose.
//Note that is really a method int IUnknown.Release. Our first parameter is our this pointer.
private static int Release(IntPtr unk)
{
int refCount = unkRelease(unk);
if (refCount == 0)
{
//This is us, so we know the interface is implemented
((IDisposable)Marshal.GetObjectForIUnknown(unk)).Dispose();
}
return refCount;
}
#endregion

/// <summary>
/// Creates a new <see cref="DisposableComObject"/>
/// </summary>
protected DisposableComObject()
{
AddDisposeHandler(this);
}

/// <summary>
/// Calls <see cref="Dispose"/> with false.
/// </summary>
~DisposableComObject()
{
Dispose(false);
}

/// <summary>
/// Override to dispose the object, called when ref count hits or during GC.
/// </summary>
/// <param name="disposing"><b>true</b> if called because of a 0 refcount</param>
protected virtual void Dispose(bool disposing)
{

}

void IDisposable.Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}

关于c# - 是否有可能拦截(或意识到)COM 对暴露给 COM 的 CLR 对象的引用计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2223147/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com