好的,伙计们,这是另一个尝试。您实际上可以使用“Windows 脚本组件”来包装您的 .NET COM 对象并以这种方式完成。这是使用可以添加值的简单 .NET 计算器的完整示例。我相信你会从那里得到这个概念,这完全避免了 VB 运行时、ATL 问题,并使用了每个主要 WIN32/WIN64 平台上都可用的 Windows 脚本主机。
我在名为 DemoLib 的命名空间中创建了一个名为 Calculator 的简单 COM .NET 类。请注意,这实现了 IDisposable ,出于演示目的,我在屏幕上放了一些东西以显示它已终止。我在 .NET 和脚本中完全坚持使用 vb 以保持简单,但 .NET 部分可以在 C# 等中。当您保存此文件时,您需要使用 regsvr32 注册它,它需要被保存就像 CalculatorLib.wsc 一样。
<ComClass(Calculator.ClassId, Calculator.InterfaceId, Calculator.EventsId)> _
Public Class Calculator
Implements IDisposable
#Region "COM GUIDs"
' These GUIDs provide the COM identity for this class
' and its COM interfaces. If you change them, existing
' clients will no longer be able to access the class.
Public Const ClassId As String = "68b420b3-3aa2-404a-a2d5-fa7497ad0ebc"
Public Const InterfaceId As String = "0da9ab1a-176f-49c4-9334-286a3ad54353"
Public Const EventsId As String = "ce93112f-d45e-41ba-86a0-c7d5a915a2c9"
#End Region
' A creatable COM class must have a Public Sub New()
' with no parameters, otherwise, the class will not be
' registered in the COM registry and cannot be created
' via CreateObject.
Public Sub New()
MyBase.New()
End Sub
Public Function Add(ByVal x As Double, ByVal y As Double) As Double
Return x + y
End Function
Private disposedValue As Boolean = False ' To detect redundant calls
' IDisposable
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
MsgBox("Disposed called on .NET COM Calculator.")
End If
End If
Me.disposedValue = True
End Sub
#Region " IDisposable Support "
' This code added by Visual Basic to correctly implement the disposable pattern.
Public Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above.
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
#End Region
End Class
接下来,我创建了一个名为 Calculator.Lib 的 Windows 脚本组件,它有一个方法返回一个 VB-Script COM 类,该类公开了 .NET 数学库。这里我在Construction和Destruction期间在屏幕上弹出一些东西,注意在Destruction中我们调用.NET库中的Dispose方法来释放那里的资源。请注意使用 Lib() 函数将 .NET Com Calculator 返回给调用者。
<?xml version="1.0"?>
<component>
<?component error="true" debug="true"?>
<registration
description="Demo Math Library Script"
progid="Calculator.Lib"
version="1.00"
classid="{0df54960-4639-496a-a5dd-a9abf1154772}"
>
</registration>
<public>
<method name="GetMathLibrary">
</method>
</public>
<script language="VBScript">
<![CDATA[
Option Explicit
'-----------------------------------------------------------------------------------------------------
' public Function to return back a logger.
'-----------------------------------------------------------------------------------------------------
function GetMathLibrary()
Set GetMathLibrary = New MathLibrary
end function
Class MathLibrary
private dotNetMatFunctionLib
private sub class_initialize()
MsgBox "Created."
Set dotNetMatFunctionLib = CreateObject("DemoLib.Calculator")
end sub
private sub class_terminate()
dotNetMatFunctionLib.Dispose()
Set dotNetMatFunctionLib = nothing
MsgBox "Terminated."
end sub
public function Lib()
Set Lib = dotNetMatFunctionLib
End function
end class
]]>
</script>
</component>
最后,将这一切联系在一起,这是一个示例 VB 脚本,您可以在其中获得显示创建、计算、处理在 .NET 库中调用的对话框,最后在公开 .NET 组件的 COM 组件中终止。
dim comWrapper
dim vbsCalculator
set comWrapper = CreateObject("Calculator.Lib")
set vbsCalculator = comWrapper.GetMathLibrary()
msgbox "10 + 10 = " & vbsCalculator.lib.Add(10, 10)
msgbox "20 + 20 = " & vbsCalculator.lib.Add(20, 20)
set vbsCalculator = nothing
MsgBox("Dispose & Terminate should have been called before here.")