Symptoms
When you make a late-bound call in Microsoft Visual Basic .NET, the late-binding support fails when the ByRef decimal is converted to ByRef currency. You receive the following error message:
Unhandled Exception: System.Runtime.InteropServices.COMException (0×80020005): Type mismatch. at Microsoft.VisualBasic.CompilerServices.LateBinding.InternalLateCall(Object o, Type objType, String name, Object[] args, String[] paramnames, Boolean[] CopyBack, Boolean IgnoreReturn) at Microsoft.VisualBasic.CompilerServices.LateBinding.LateCall(Object o, Type objType, String name, Object[] args, String[] paramnames, Boolean[] CopyBack) at ConsoleApplication2.Module1.main() in C:\ConsoleApplication\Module.vb:line number
Press any key to continueNote In this error message, C:\ConsoleApplication\Module.vb:line number is a placeholder for the actual path where the application is saved. In the application module, line number represents the line where the error occurs.
Resolution
To work around this bug, use either of the following options: Use the CurrencyWrapper type.Force the decimal value to be passed by using the ByVal keyword.
To do this, put parentheses around the variable that contains the decimal value.To implement either of these workaround options, create a DLL by using Visual Basic 6.0, create a console application, and reference the DLL in the console application, as follows:Create a DLL by using Visual Basic 6.0Start Visual Basic 6.0.On the File menu, click New Project.
The New Project dialog box appears.Click ActiveX DLL, and then click OK.
By default, the Class1 class is created.Add the following code to the Class1 class:
Public Function cedrbank(name As String, ACCNUM As String, ACCBAL As Currency) As LongMsgBox ACCBALACCBAL = 4.321End FunctionOn the File menu, click Save Class1.cls, and then click Save Project As.In the File name box, type the name of the project.On the File menu, click Make Project.dll.
The Make Project dialog box appears.
Note Project is a placeholder for the actual name of the project.Click OK.Create a console applicationStart Visual Studio .NET.On the File menu, point to New, and then click Project.
The New Project dialog box appears.Under Project Types, click Visual Basic Projects.Under Templates, click Console Application.Click OK.
By default, the Module1.vb module is created.Replace the existing code in the Module1.vb module with the following code:
Imports SystemImports System.Runtime.InteropServicesImports System.ReflectionImports Project1Public Module Module1Sub Main()Dim x As Objectx = New Class1Dim dc As Object = New CurrencyWrapper(5)Dim d As Decimal = 1.987Dim args(2) As Objectargs(0) = “name”args(1) = “num”args(2) = dcDim c(0) As Reflection.ParameterModifierc(0) = New Reflection.ParameterModifier(3)c(0).Item(0) = Truec(0).Item(1) = Truec(0).Item(2) = TrueCObj(x).GetType().InvokeMember(“cedrbank”, BindingFlags.InvokeMethod, Nothing, x, args, c, Nothing, Nothing)Console.WriteLine(args(2))dc = New CurrencyWrapper(6)’Use the CurrencyWrapper type.x.cedrbank(“name”, “num”, dc)Console.WriteLine(dc)’Force ‘d’ to be passed ByVal by using parentheses around ‘d.’x.cedrbank(“name”, “num”, (d))Console.WriteLine(d)End SubEnd ModuleAdd a reference to the DLL that you created in the “Create a DLL by using Visual Basic 6.0″ section of this article.On the Build menu, click Build Solution.On the Debug menu, click Start.
You do not receive the error message that is mentioned in the “Symptoms” section of this article.