gpt4 book ai didi

c - 在 Visual Basic 中指向 double 的指针数组

转载 作者:太空宇宙 更新时间:2023-11-04 04:00:20 24 4
gpt4 key购买 nike

我编写了一个 C DLL 来创建我想在 VB.NET 中使用的光栅化图形。有一次,它使用一个指向 double double **ibuffer 的指针数组作为函数的参数。

那么如何将它从 Visual Basic 传递给 C DLL?我最好在 VB 中创建数组,但不需要在 VB 中操作或使用这些值。所以基本上,所有 VB 需要做的就是为指针数组分配内存。 C 会做所有其他的事情。如何实现?

最佳答案

我假设您正在使用 pInvoke 调用 VB.NET 中的 C 方法

首先,Jagged 数组没有可用的默认编码,这意味着您必须进行自己的自定义编码,这有点复杂但不是很困难。这是在 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

关于c - 在 Visual Basic 中指向 double 的指针数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12506278/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com