访问:跨程序复位保持COM引用? [英] Access: persist a COM reference across Program Reset?

查看:339
本文介绍了访问:跨程序复位保持COM引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Access VBA(2003)中有没有办法将COM引用转换为整数,并调用AddRef / Release? (给出错误功能或接口标记为受限,或该功能使用Visual Basic中不支持的自动化类型)

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")

我使用第三方COM对象,它不处理在单个进程中被实例化两次(这是一个已知的错误)。因此,我想到将引用存储为隐藏表单上的控件的标题,以保护其免于程序重置清除所有VB变量。

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.

编辑:我认为cast to int可以使用未记录的ObjPtr,然后再次使用CopyMemory API,AddRef / Release可以隐式调用。但是有没有更好的方法?加载项是否受到程序重置的保护?

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?

推荐答案

是否存在代码复位的问题,重置它不能重新初始化?

Is the problem with surviving the code reset or is it that once the code is reset it can't be re-initialized?

对于第一个问题,将顶级对象包装在函数中,并在内部使用STATIC变量来缓存引用。如果STATIC变量为Nothing,则重新初始化。下面是用于缓存对本地数据库的引用的函数:

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:

  Public Function dbLocal(Optional bolInitialize As Boolean = True) +
     As DAO.Database
  ' 2003/02/08 DWF added comments to explain it to myself!
  ' 2005/03/18 DWF changed to use Static variable instead
  ' uses GoTos instead of If/Then because:
  '  error of dbCurrent not being Nothing but dbCurrent being closed (3420)
  '  would then be jumping back into the middle of an If/Then statement
  On Error GoTo errHandler
    Static dbCurrent As DAO.Database
    Dim strTest As String

  If Not bolInitialize Then GoTo closeDB

  retryDB:
    If dbCurrent Is Nothing Then
       Set dbCurrent = CurrentDb()
    End If
    ' now that we know the db variable is not Nothing, test if it's Open
    strTest = dbCurrent.Name

  exitRoutine:
    Set dbLocal = dbCurrent
    Exit Function

  closeDB:
    If Not (dbCurrent Is Nothing) Then
       Set dbCurrent = Nothing
    End If
    GoTo exitRoutine

  errHandler:
    Select Case err.Number
      Case 3420 ' Object invalid or no longer set.
        Set dbCurrent = Nothing
        If bolInitialize Then
           Resume retryDB
        Else
           Resume closeDB
        End If
      Case Else
        MsgBox err.Number & ": " & err.Description, vbExclamation, "Error in dbLocal()"
        Resume exitRoutine
    End Select
  End Function

任何地方,你可以在代码中使用以下代码:

Anywhere you'd either of these in code:

  Dim db As DAO.Database
  Set db = CurrentDB()
  Set db = DBEngine(0)(0)
  db.Execute "[SQL DML]", dbFailOnError

...您可以用以下内容替换整个内容:

...you can replace the whole thing with:

  dbLocal.Execute "[SQL DML]", dbFailOnError

不必担心在应用程序打开时或代码重置后初始化它 - 它是自我修复的,因为它检查Static内部变量,并根据需要重新初始化。

...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.

唯一的警告是,您需要在关闭应用程序时调用bolInitialize参数设置为False,因为这会清除引用,因此当应用程序超出应用程序的范围时,您的应用程序不会有挂起的风险

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.

对于其他问题,我真的怀疑VBA中有任何解决方案,除非你可以进行API调用并终止外部进程。但是,这是一个很长的时间,我想。

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.

这篇关于访问:跨程序复位保持COM引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆