访问:在程序重置期间保留 COM 引用?
Access VBA (2003) 是否有方法将 COM 引用转换为整数,并调用 AddRef/Release? (这会出现错误“函数或接口标记为受限制,或者函数使用 Visual Basic 中不支持的自动化类型”)
我使用的是第三方 COM 对象,该对象无法处理在单个进程中实例化两次的情况(这是一个已知的错误)。因此,我考虑将引用存储为隐藏窗体上控件的标题,以保护它免受程序重置清除所有 VB 变量的影响。
编辑:我认为可以使用未记录的 ObjPtr 来完成对 int 的转换,然后使用 CopyMemory API 再次返回,并且可以隐式调用 AddRef/Release 。但还有更好的办法吗?加载项是否受到程序重置的保护?
Are there ways in Access VBA (2003) to cast a COM reference to an integer, and to call AddRef/Release? (which give the error "Function or interface marked as restricted, or the function uses an Automation type not supported in Visual Basic")
I'm using a third-party COM object which doesn't handle being instantiated twice in a single process (this is a known bug). I therefore thought of storing the reference as the caption of a control on a hidden form to protect it from Program Reset clearing all VB variables.
Edit: I think the cast to int can be done with the undocumented ObjPtr, and back again with the CopyMemory API, and AddRef/Release can be called implicitly. But is there a better way? Are add-ins protected from Program Reset?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是代码重置后仍然存在,还是代码重置后就无法重新初始化?
对于第一个问题,将顶级对象包装在函数中,并在内部使用 STATIC 变量来缓存引用。如果 STATIC 变量为 Nothing,则重新初始化。这是我用于缓存对本地数据库的引用的函数:
在代码中的任何地方:
...您可以将整个内容替换为:
...并且您不必担心在以下情况下初始化它:您的应用程序打开,或者在代码重置后 - 它是自我修复的,因为它会检查静态内部变量并在需要时重新初始化。
唯一需要注意的是,当您关闭应用程序时,您需要将 bolInitialize 参数设置为 False 进行调用,因为这会清理引用,因此当应用程序关闭时超出范围时,您的应用程序不会有挂起的风险。
对于另一个问题,我真的怀疑 VBA 中是否有任何解决方案,除非您可以进行 API 调用并终止外部进程。但我认为这是一个不太可能的事情。
Is the problem with surviving the code reset or is it that once the code is reset it can't be re-initialized?
For the first problem, wrap your top-level object in a function and use a STATIC variable internally to cache the reference. If the STATIC variable Is Nothing, re-initialize. Here's the function I use for caching a reference to the local database:
Anywhere you'd either of these in code:
...you can replace the whole thing with:
...and you don't have to worry about initializing it when your app opens, or after a code reset -- it's self-healing because it checks the Static internal variable and re-initializes if needed.
The only caveat is that you need to make a call with the bolInitialize argument set to False when you shut down your app, as this cleans up the reference so there's no risk of your app hanging when it goes out of scope as the app closes.
For the other problem, I really doubt there's any solution within VBA, unless you can make an API call and kill the external process. But that's something of a longshot, I think.