OK 皆さん、これは別の試みです。実際には、「Windows スクリプト コンポーネント」を使用して .NET COM オブジェクトをラップし、その方法でファイナライズすることができます。値を加算できる単純な .NET Calculator を使用した完全なサンプルを次に示します。そこから概念を理解できると確信しています。これにより、VB ランタイム、ATL の問題が完全に回避され、すべての主要な WIN32/WIN64 プラットフォームで利用可能な Windows Scripting Host が使用されます。
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 スクリプト コンポーネントを作成します。このコンポーネントには、.NET 数学ライブラリを公開する VB スクリプト COM クラスを返す単一のメソッドがあります。ここでは、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.")