通过ref对象传递给VB DLL无法正常工作

时间:2014-01-22 03:42:59

标签: c# arrays object com ref

在花了三天谷歌搜索和阅读MSDN后,新手发布的第一篇文章:

从C#我试图在VB dll中调用一个方法,它的开头是这样的:

Public Function Correl(action As String, ByRef returns As Object, observation_frequency As Double) As Double(,)
Dim left As String = Strings.UCase(Strings.Trim(action))
Dim num As Double = observation_frequency
Dim objectValue As Object = RuntimeHelpers.GetObjectValue(returns)
Dim num2 As Short
Dim num3 As Short
Dim num4 As Short
Dim array As Double(,)
Dim arg_5D_0 As Short
Dim num5 As Short
' The following expression was wrapped in a checked-statement
num2 = CShort(Information.UBound(CType(objectValue, Array), 1))
num3 = CShort(Information.UBound(CType(objectValue, Array), 2))
num4 = num2 + 1
objectValue = RuntimeHelpers.GetObjectValue(returns)
array = New Double(CInt((num2 + 1)), CInt((num3 + 1)))
arg_5D_0 = 1
num5 = num2
For num6 As Short = arg_5D_0 To num5
    Dim arg_65_0 As Short = 1
    Dim num7 As Short = num3
    For num8 As Short = arg_65_0 To num7
        array(CInt(num6), CInt(num8)) = Conversions.ToDouble(NewLateBinding.LateIndexGet(objectValue, New Object()() = { num6, num8 }, Nothing))
    Next
Next '...........

从C#调用方法时,就像这样

double[] dcloses1 = new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
double[,] dcloses2 = new double[,] { { 1, 1 }, { 2, 2 }, { 3, 3 }, { 4, 4 } };
object ocloses = (object)dcloses1; // or dcloses2
double[,] nFullMatrix = corobj.Correl("AVC", ref ocloses, (double)frequency); 

我从第二个参数(ByRef返回As Object)得到一个错误,来自第12行(num2 = CShort(...),如下所示:

  1. 传递单维数组(dcloses1),错误是:“参数'Rank'对数组无效。”是的,因为它需要N:N矩阵,但至少传递了数组。

  2. 传递二维数组(dcloses2),错误是:“值不能为NULL。参数名称:Argument'Array'为Nothing。”所以似乎无法识别arraya inb对象。

  3. 似乎该方法需要一个数组包装到一个对象中,因为它使用GetObjectValue(),但到目前为止所有意图移交所需的矩阵都失败了。

    之前已经通过Excel VBA传递变量调用了这个问题,但是C#VariantWrapper也没有帮助。

    关于如何为第二个参数构造对象的任何想法? C#和VB interop之间有类似的经历吗?

    更新 在最后一天尝试了我所知道的所有其他结构,列表,阵列形式,但不知何故没有任何成功。仍然是相同的错误“值不能为NULL。参数名称:参数'数组'是Nothing。”。我可以尝试其他任何想法吗?

0 个答案:

没有答案