9年后我遇到了这个问题。我想出的解决方案是将指针传递给数组的第一个元素,以及上限(VB6 中的 UBound)。然后迭代指向上限的指针并将每个元素放入一个列表中。
在 vb6 端使用
Dim myarray(3) As float
Dim ptr As integer
Dim upperbound as integer
myarray(0) = 0.1
myarray(1) = 0.2
myarray(2) = 0.3
myarray(3) = 0.4
ptr = VarPtr(myarray(0))
upperbound = UBound(myarray)
SetMyFloat(ptr, upperbound)
C# 代码
public float MyFloat {get; set;}
public unsafe void SetMyFloat(float* ptr, int ubound)
{
MyFloat = PointerToFloatArray(ptr, ubound);
}
public unsafe float[] PointerToFloatArray(float* ptr, int ubound)
//this is to deal with not being able to pass an array from vb6 to .NET
//ptr is a pointer to the first element of the array
//ubound is the index of the last element of the array
{
List<float> li = new List<float>();
float element;
for (int i = 0; i <= ubound; i++)
{
element = *(ptr + i);
li.Add(element);
}
return li.ToArray();
}