Void Star
Void Star

Reputation: 2521

pointer array to doubles in Visual Basic

I have a C DLL I wrote to create rasterized graphics which I want to use in VB.NET. At one point it uses an array of pointers to doubles double **ibuffer as a parameter for a function.

So how do I pass this to a C DLL from Visual Basic? Preferably, I would create the array in VB, but I wouldn't need to manipulate or use the values in VB. So basically, all VB needs to do is allocate the memory for the array of pointers. C would do all the other stuff. How can this be accomplished?

Upvotes: 1

Views: 1482

Answers (1)

Aamir
Aamir

Reputation: 15566

I assume that you are using pInvoke to call C method in VB.NET

First of all, there is no default marshalling available for Jagged arrays which means that you will have to do your own custom marshalling which is a little complicated but not very difficult. Here is the code to do such a thing in C#. I am not that good with VB.NET Syntax so I am sure you would be able to convert this to 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;
  }

Taken from : http://social.msdn.microsoft.com/Forums/is/csharplanguage/thread/dd729947-f634-44f4-8d91-11fcef97cabe

Upvotes: 1

Related Questions