我有以下传统的VB6功能,我想从C#调用。
Public Function CreateMiscRepayment(ByRef objMiscRepayment As MiscRepayment) As Variant
' Code that sets objMiscRepayment here
End Function
我在C#中使用以下代码但获得异常:
dynamic vb6ComObject = Activator.CreateInstance(Type.GetTypeFromProgID(progId));
dynamic miscRepayment = null;
dynamic result = vb6ComObject.CreateMiscRepayment(ref miscRepayment);
例外是:
System.ArgumentException: Could not convert argument 0 for call to CreateMiscRepayment.
at System.Dynamic.ComRuntimeHelpers.CheckThrowException(Int32 hresult, ExcepInfo& excepInfo, UInt32 argErr, String message)
at CallSite.Target(Closure , CallSite , ComObject , Object& )
at CallSite.Target(Closure , CallSite , ComObject , Object& )
at CallSite.Target(Closure , CallSite , Object , Object& )
at CallSite.Target(Closure , CallSite , Object , Object& )
Application\ApplicationClasses.cs(65,0): at ApplicationClasses.CanInstantiateMiscRepayment()
我已尝试将ref
更改为out
但收到同样的错误。如果我省略ref
,该方法会毫无错误地执行,但当然miscRepayment
仍然是null,而不是包含应该传递出去的对象。
我尝试了其他一些方法,包括使用VB.NET(因为它总是比C#更友好)。
使用以下VB.NET代码:
Dim vb6ComObject = Activator.CreateInstance(System.Type.GetTypeFromProgID(progId))
Dim miscRepayment = Nothing
Dim result = vb6ComObject.CreateMiscRepayment(miscRepayment)
它引发了以下类似但不同的异常:
System.Runtime.InteropServices.COMException: Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))
at Microsoft.VisualBasic.CompilerServices.LateBinding.LateGet(Object o, Type objType, String name, Object[] args, String[] paramnames, Boolean[] CopyBack)
at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack)
UnitTest1.vb(19,0): at TestProject1.UnitTest1.TestMethod1()
有趣的是,如果我在C#或VB.NET示例代码中更改调用以使用null
/ Nothing
而不是miscRepayment
,则代码将执行而不会抛出异常。我甚至在VB6 COM对象的代码中设置了断点,并且可以确认代码在该端已正确执行。显然,通过将miscRepayment
参数设置为null
/ Nothing
,.NET中无法接收创建的对象。问题必须与参数的编组有关。
我还尝试将Type.InvokeMember
与ParameterModifier
参数一起使用,将miscRepayment
标记为ref
参数,但获得以下异常:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.InteropServices.COMException: Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))
--- End of inner exception stack trace ---
at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)
at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
UnitTest1.vb(18,0): at TestProject1.UnitTest1.TestMethod1()
最后,我尝试了以下VB.NET代码:
Dim vb6ComObject = Activator.CreateInstance(System.Type.GetTypeFromProgID(progId))
Dim args(0) As Object
Microsoft.VisualBasic.CompilerServices.LateBinding.LateCall(vb6ComObject, type, "CreateMiscRepayment", args, Nothing, New Boolean() {True})
抛出以下异常:
System.Runtime.InteropServices.COMException: Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))
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)
UnitTest1.vb(17,0): at TestProject1.UnitTest1.TestMethod1()
使用抛出异常的所有代码,永远不会调用VB6 COM对象。尝试编组ref
参数时,COM互操作代码必须窒息。
在我的Google搜索中,我遇到了一些使用Type.InvokeMember
的示例,但ref
参数始终用于简单类型,例如整数和字符串。
答案 0 :(得分:2)
在.NET中,似乎没有办法在COM对象上调用方法,该方法采用复杂类型的ref
参数。
我和微软一起filed a bug。如果此问题也影响您,请投票it。
有一条评论来自微软的错误报告说它已被修复。没有提到哪个版本的.NET受到了影响。
答案 1 :(得分:0)
实际上不是答案,而是一种解决方法。
在我看来,当您将COM对象的访问方式从dynamic
更改为静态时,问题就会消失。通过静态方式我的意思是使用C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\TlbImp.exe
为COM对象准备一个dll。我想这是早期约束。