为什么调用COM组件只返回使用VisualStudio QuickWatch窗口的值?

本文关键字:QuickWatch VisualStudio 窗口 COM 调用 组件 返回 为什么 | 更新日期: 2023-09-27 18:15:23

我目前在c# MVC3项目中使用的com组件遇到了一个小问题。

COM组件(名为YVPCUST)包含以下vb6例程:

Public Function GetDocument(ByVal a_sCode As Variant, ByRef a_sLocation As Variant, ByRef a_sDestination As Variant, ByRef a_sSuffix As Variant) As Integer
    Dim oSystem     As System
    Set oSystem = CreateObject("qtcsl32.System")
    GetDocument = oSystem.GetDocumentByRef(a_sCode, a_sLocation, a_sDestination, a_sSuffix)
    Set oSystem = Nothing
End Function

c# MVC3项目是使用COM组件层构建的,该组件层直接引用名为YVPCUST的组件:

qtcsl32.YVPCUST yvpCust = new qtcsl32.YVPCUST();
object code = (object)"RAAG";
object minimumAge = 0;
object maximumAge = 0;
object suffix = (object)string.Empty; //not to be used
int returnCode;
returnCode = yvpCust.GetDocument(code, minimumAge, maximumAge, suffix);

GetDocument()例程应该得到所提供代码的最大和最小年龄。但是,当我在编译或调试模式下运行应用程序并遍历最后一行时,minimumAge和maximumAge值没有正确设置,它们被设置为默认值0(不管我重新执行这一行的次数)。

然而,(在调试模式下)如果我打破这最后一行的代码,而是使用QuickWatch运行这行,当我从QuickWatch窗口返回到代码时,当我将鼠标悬停在它们上面时,这些值被设置为正确的最小和最大年龄。

有人知道为什么会发生这种情况吗?

这个COM组件可以在应用程序的其他地方使用(但不是这个例程),没有问题。

为什么调用COM组件只返回使用VisualStudio QuickWatch窗口的值?

我意识到COM组件的c#包装器有以下声明:

short GetDocument(object a_sCode, ref object a_sLocation, ref object a_sDestination, ref object a_sSuffix);

然后我意识到代码行应该写(包括ref):

returnCode = yvpCust.GetDocument(code, ref minimumAge, ref maximumAge, suffix);

这并不能解释为什么它使用QuickWatch窗口工作,但我已经设法得到所需的结果。