COM RCW 上的地址引用

发布于 2024-08-29 11:08:12 字数 483 浏览 4 评论 0原文

是否可以增加未知接口上的 RCW 引用计数? (即不是底层 COM 对象的引用计数)

我有一些旧的 COM 服务器代码

int Method1(object comobject) {
    try {
        // do something with comobject
        return 0;
    }
    finally {
        Marshal.ReleaseComObject(comobject);
    }
 }

此代码工作正常,但现在我需要从另一个方法调用它。

int Method2(object comobject) {
    int result = Method1(comobject);
    // Do something with combject
}

comobject 的类型会有所不同(这就是它是 object 的原因)

Is it possible to increase the RCW reference count on an unknown interface?
(i.e. not the reference count on the underlying COM object)

I have some old COM server code

int Method1(object comobject) {
    try {
        // do something with comobject
        return 0;
    }
    finally {
        Marshal.ReleaseComObject(comobject);
    }
 }

This code works fine but now I need to call it from another method.

int Method2(object comobject) {
    int result = Method1(comobject);
    // Do something with combject
}

The type of comobject will vary (that is why it is object)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ㄖ落Θ余辉 2024-09-05 11:08:12

有一种方法,RCW 计数计算对象已被封送的次数,您可以通过执行额外的封送来增加此数字。

public static T AddRcwRef<T>(T t) 
{
    IntPtr ptr = Marshal.GetIUnknownForObject(t);
    try {
        return (T)Marshal.GetObjectForIUnknown(ptr);
    }
    finally {
         Marshal.Release(ptr); // done with the IntPtr
    }
}

我不确定是否会推荐使用此方法,最好尝试摆脱 ReleaseComObject 调用。

如需进一步阅读,请参阅我撰写的关于该主题的博客文章

There is a way, the RCW count counts how many times the object has been marshaled you can increase this number by performing an additional marshal.

public static T AddRcwRef<T>(T t) 
{
    IntPtr ptr = Marshal.GetIUnknownForObject(t);
    try {
        return (T)Marshal.GetObjectForIUnknown(ptr);
    }
    finally {
         Marshal.Release(ptr); // done with the IntPtr
    }
}

I'm not sure I would recommend using this method, it's probably better to try and get rid of your ReleaseComObject calls.

For further reading, see this blog post on the subject I wrote.

难得心□动 2024-09-05 11:08:12

有 Marshal.AddRef() 方法,但引用计数更改错误。我很确定直接增加 RCW 计数是不可能的。把自己从深渊中挖出来,修复旧代码。

There's the Marshal.AddRef() method, wrong reference count change though. I'm pretty sure incrementing the RCW count directly is not possible. Dig yourself out of the deep hole you're in and fix the old code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文