Red Gate forums :: View topic - Array of arrays (float**)
Return to www.red-gate.com RSS Feed Available

Search  | Usergroups |  Profile |  Messages |  Log in  Register 
Go to product documentation
PInvoke.Net
PInvoke.Net forum

Array of arrays (float**)

Search in PInvoke.Net forum
Post new topic   Reply to topic
Jump to:  
Author Message
yoavhacohen



Joined: 20 Dec 2008
Posts: 2
Location: Israel

PostPosted: Thu Dec 25, 2008 12:03 pm    Post subject: Array of arrays (float**) Reply with quote

Hi,

I have a native function that gets an array of pointers to arrays:


Code:

extern __declspect(dllexport) void foo(float**);


And I have a 2D managed array which I want to give the function as a parameter, *WITHOUT* copy its data.

I wrote this code, but sometimes I get an AccessViolationException when I call to the function (in other cases it works fine):

Code:

[DllImport(DLL_NAME)]
internal static extern void foo(IntPtr ptr);

public void FooWrapper(float[,] array)
{
   int dim = array.GetLength(1);
   int nRows = array.GetLength(0);

   GCHandle _pinnedArray = GCHandle.Alloc(array, GCHandleType.Pinned);
   unsafe
   {
      fixed (float* fixedFirstRow = &array[0,0])
      {
           float* rowPtr = (float*)(new IntPtr(fixedFirstRow).ToPointer());
           // Allocate an array of float* pointers
           int[] pointersToRows = new int[nRows];
           for (int row = 0; row < nRows; ++row, rowPtr += dim)
           {
                pointersToRows[row] = new IntPtr(rowPtr).ToInt32;
           }
           GCHangle _rowPtrPinned = GCHandle.Alloc(pointersToRows, GCHanfleType.Pinned);
           fixed (int* pointersToRowsHandle = &pointersToRows[0])
           {
               FooWrapper(new IntPtr(pointersToRowsHandle));
            }
       }
}



What am I doing wrong?
Is there a better way to do this?

Thanks in advance,
Yoav
_________________
www.YoavHaCohen.com
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic All times are GMT + 1 Hour
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group