- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我已经改写了这个问题。
当 .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 与我们集成的客户的实用程序和辅助函数。
我不想做的事
谢谢
体重
已接受的答案
千人感谢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/
我注意到 Javascript 排序功能在低于 9 的 Internet Explorer 版本中非常慢(与 Firefox 和其他版本相比通常慢一个数量级。我正在实现我自己的版本以查看是否可以做得更
我有一个实现ContainerRequestFilter的类,我想向其中注入(inject)一些spring依赖项,所以我需要让Spring知道Jersey过滤器。过滤器本身是在我的 web.xml
有没有办法让 Mypy 意识到我知道我正在处理什么类型的对象,而无需在每个 if 语句中调用 isinstance?我希望有辅助函数来做那种事情,但即使我在辅助函数中有 isinstance,Mypy
我必须用法语翻译一个网站。现在,法语和许多其他语言一样,有性别和屈折变化。问题是: 如何创建包含可变部分为男性或女性的消息的 yaml 文件... 如何修改 i18n 生成器以支持此功能? 是否有任何
正如我发现的那样,Chrome 不会忽略空的 src,而是将当前查看的文件加载到其中。由于这个“功能”,我的统计数据出现了错误。 用这段代码创建 file.php... Here is my pag
我是一名优秀的程序员,十分优秀!