VB6 中的 AddRef()

发布于 2024-12-01 09:00:52 字数 78 浏览 2 评论 0原文

是否可以从 Visual Basic 6 中的接口 IUnknown 调用方法 AddRef() 或实现一些 hack 来增加对象引用计数器?

is it possible to call method AddRef() from interface IUnknown in visual basic 6 or implement some hack to increment object reference counter?

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

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

发布评论

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

评论(2

命硬 2024-12-08 09:00:52

如果您尝试人为地增加引用计数器(在 VB6 完成的自动引用计数之外),那么您可以执行以下操作:

Public Sub AddOneToRefCount(target As Object)

    Dim tempObj As Object
    Set tempObj = target 'VB6 calls AddRef for you here

    Call CopyMemory(ByVal ObjPtr(tempObj), 0&, 4&) 'trick VB into not calling Release

End Sub

您可能还需要实现相反的操作才能释放,否则您将得到一个各种内存泄漏。

If you are trying to artificially increment the refrence counter (outside of the automatic reference counting that is done by VB6) then you can do the following:

Public Sub AddOneToRefCount(target As Object)

    Dim tempObj As Object
    Set tempObj = target 'VB6 calls AddRef for you here

    Call CopyMemory(ByVal ObjPtr(tempObj), 0&, 4&) 'trick VB into not calling Release

End Sub

You'll probably need to implement the reverse of this as well to release or you will get a memory leak of sorts.

§普罗旺斯的薰衣草 2024-12-08 09:00:52

我相当确定 VB6 不支持直接访问 IUnknown 方法。但解决方法很简单。将此声明添加到模块中:

  Dim ReferenceHolder As Variant

然后使用简单的赋值来调用 AddRef() 。

  Set ReferenceHolder = someObject

相反,使用 Release() 来调用

  Set ReferenceHolder = Nothing

这是正常的 VB6 内存管理工作。

I'm fairly sure that VB6 doesn't support directly accessing the IUnknown methods. But the workaround is simple. Add this declaration to a Module:

  Dim ReferenceHolder As Variant

And then get AddRef() to get called with a simple assignment

  Set ReferenceHolder = someObject

Reversely, get Release() to get called with

  Set ReferenceHolder = Nothing

This is normal VB6 memory management at work.

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