[英]pointer array to doubles in Visual Basic
我有一个C DLL,我创建了一个C DLL来创建要在VB.NET中使用的栅格化图形。 在某一时刻,它使用一个指针数组将double **ibuffer
作为函数的参数加倍。
那么如何将其从Visual Basic传递到C DLL? 最好是,我将在VB中创建数组,但不需要在VB中操作或使用值。 因此,基本上,VB要做的就是为指针数组分配内存。 C会做所有其他事情。 如何做到这一点?
我假设您正在使用pInvoke在VB.NET中调用C方法
首先,锯齿状数组没有默认的封送处理,这意味着您将必须进行自己的自定义封送处理,这有点复杂,但并不是很困难。 这是在C#中执行此操作的代码。 我对VB.NET语法不太满意,所以我确定您可以将其转换为VB.NET
[DllImport( "yourdll.dll", EntryPoint="YourMethodName", CallingConvention=CallingConvention.Cdecl)]
static extern void YouMethodName(IntPtr matrix);
static void Main( string[] args )
{
double[][] test_matrix = { new double[] {1.1,2.2},
new double[] {3.3,4.4},
new double[] {5.5,6.6}};
IntPtr pa1 = marshalJaggedArray( test_matrix );
YourMethodName( pa1 );
}
static private IntPtr marshalJaggedArray( double[][] array )
{
int sizeofPtr = Marshal.SizeOf( typeof( IntPtr ) );
int sizeofDouble = Marshal.SizeOf( typeof( double ) );
IntPtr p1 = Marshal.AllocCoTaskMem( array.Length * sizeofPtr );
for ( int i = 0 ; i < array.Length ; i++ )
{
IntPtr v1 = Marshal.AllocCoTaskMem( array[i].Length * sizeofDouble );
Marshal.Copy( array[i], 0, v1, array[i].Length );
Marshal.WriteIntPtr( p1, i * sizeofPtr, v1 );
}
return p1;
}
摘自: http : //social.msdn.microsoft.com/Forums/is/csharplanguage/thread/dd729947-f634-44f4-8d91-11fcef97cabe
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.