1

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?

2

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

I'll get back to this soon. I'm moving to my dorm today, for my first year at university, so I am a bit busy.
I'm pretty darn sure this is going to work as soon as I compile my DLL in 64 bit, that's the stumbling block now I think!
Yes it works. There are some differences between VB and C#, but that's to be expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.