强制 COM 服务器保持打开状态
Force a COM server to remain open
我有一个由 VB6 exe.
托管的 COM 自动化服务器
COM API从Excel使用 VBA:
Dim o as MyCOMAPI.MyCOMType
Set o = new MyCOMAPI.MyCOMType
o.DoSomething
当我在 VBA 中创建对象时,exe 与 COM 自动化一起启动并且 VBA 可以使用 API.
但是 exe 被 Excel 快速关闭 "randomly",我想当 Excel 决定它不再需要 COM API 时。
此行为导致随机错误。
简单的解决方案是在 运行 VBA 代码之前启动 exe;在这种情况下,一切正常,因为 exe 不会停止 运行 直到被用户关闭。
您是否 information/documentation 了解 Excel VBA 管理对托管 API 的呼叫的方式?
有没有办法避免这种行为并让 exe 保持打开状态直到 VBA 代码决定停止它?
当取消引用最后一个对象时,这将是 COM 自动化服务器的默认行为,这意味着指向服务器的变量被设置为空。
现在,如果您的代码今天看起来像这样:
Sub MyFunction()
...
Dim o as MyCOMAPI.MyCOMType
Set o = new MyCOMAPI.MyCOMType
o.DoSomething
End Sub
然后,服务器寿命连接到o变量的寿命。当函数完成时,该变量将被设置为空,然后服务器将关闭(除非有其他变量使其保持活动状态)。
要确保您的 COM 服务器保持活动更长时间,只需将变量定义为 Public 变量,如下例所示。
此示例将启动并显示 Excel 并保持打开状态,直到调用 ShutdownExcel 函数。
Public o As Excel.Application
Sub MakeSureExcelIsRunning()
If o Is Nothing Then
Set o = New Excel.Application
o.Visible = True
End If
End Sub
Sub ShutdownExcel()
Set o = Nothing
End Sub
来自 COM 文档。
**Component Automation**
Shutting Down Objects
ActiveX objects must shut down in the following way:
If the object's application is visible, the object should shut down only in response to an explicit user command (for example, clicking Exit on the File menu) or the equivalent command from an ActiveX client.
If the object's application is not visible, the object should shut down only when the last external reference is gone.
If the object's application is visible and is controlled by an ActiveX client, it should become invisible when the user shuts it down (for example, clicking Exit on the File menu). This behavior allows the controller to continue to control the object. The controller should shut down only when the last external reference to the object has disappeared.
© Microsoft Corporation. All rights reserved.
当你编写一个 COM 服务器 exe 时,你做的第一件事就是在作为普通 exe 启动时引用你自己,否则 exe 会在初始化结束后立即关闭。
我有一个由 VB6 exe.
托管的 COM 自动化服务器COM API从Excel使用 VBA:
Dim o as MyCOMAPI.MyCOMType
Set o = new MyCOMAPI.MyCOMType
o.DoSomething
当我在 VBA 中创建对象时,exe 与 COM 自动化一起启动并且 VBA 可以使用 API.
但是 exe 被 Excel 快速关闭 "randomly",我想当 Excel 决定它不再需要 COM API 时。
此行为导致随机错误。
简单的解决方案是在 运行 VBA 代码之前启动 exe;在这种情况下,一切正常,因为 exe 不会停止 运行 直到被用户关闭。
您是否 information/documentation 了解 Excel VBA 管理对托管 API 的呼叫的方式?
有没有办法避免这种行为并让 exe 保持打开状态直到 VBA 代码决定停止它?
当取消引用最后一个对象时,这将是 COM 自动化服务器的默认行为,这意味着指向服务器的变量被设置为空。
现在,如果您的代码今天看起来像这样:
Sub MyFunction()
...
Dim o as MyCOMAPI.MyCOMType
Set o = new MyCOMAPI.MyCOMType
o.DoSomething
End Sub
然后,服务器寿命连接到o变量的寿命。当函数完成时,该变量将被设置为空,然后服务器将关闭(除非有其他变量使其保持活动状态)。
要确保您的 COM 服务器保持活动更长时间,只需将变量定义为 Public 变量,如下例所示。
此示例将启动并显示 Excel 并保持打开状态,直到调用 ShutdownExcel 函数。
Public o As Excel.Application
Sub MakeSureExcelIsRunning()
If o Is Nothing Then
Set o = New Excel.Application
o.Visible = True
End If
End Sub
Sub ShutdownExcel()
Set o = Nothing
End Sub
来自 COM 文档。
**Component Automation**
Shutting Down Objects
ActiveX objects must shut down in the following way:
If the object's application is visible, the object should shut down only in response to an explicit user command (for example, clicking Exit on the File menu) or the equivalent command from an ActiveX client.
If the object's application is not visible, the object should shut down only when the last external reference is gone.
If the object's application is visible and is controlled by an ActiveX client, it should become invisible when the user shuts it down (for example, clicking Exit on the File menu). This behavior allows the controller to continue to control the object. The controller should shut down only when the last external reference to the object has disappeared.
© Microsoft Corporation. All rights reserved.
当你编写一个 COM 服务器 exe 时,你做的第一件事就是在作为普通 exe 启动时引用你自己,否则 exe 会在初始化结束后立即关闭。