Array to MemoryBlock

Which is the best way to create a MemoryBlock that holds all the values of an array of doubles (two dimensions).
I must be able to send this array to a Declare for an external DLL, and I don’t know how.

If this conversion is automatic (I wish it was, but I think it’s not), how are values stored in the MemoryBlock, by columns or by rows?

Thanks for any help.

It’s going to depend on what the external DLL is expecting. What do the docs say?

Assuming this is a C based DLL means you’ll have to write all the values from your Xojo array in row major order

Create the memory block to hold all the values - width * height * sizeof double (which is 8 bytes)
Then iterate over the values row by row & plop them in the mb

   dim offset as integer 
   for row as integer = 0 to ubound(myArrayOfDoubles,1)
         for col as integer = 0 to ubound(myArrayOfDoubles,2)
                   memBlock.doublevalue(offset) = myArrayOfDoubles(row,col)
                   offset = offset + 8
          next
   next

EDIT : DUH there’s a bug … remember to increment the offset each time :stuck_out_tongue:

Thanks Norman.
That’s what I supposed, but I needed someone with more knowledge than me to confirm.

note my edit :slight_smile:

[quote=134400:@Norman Palardy]note my edit :)[/quote]ok

C arrays are fairly simple
You can calculate the position of any element (say in a 2 D array - I’d have to poke around to be sure about more dimensions)

position = ((row * column count) * size of element) + (col * sizeof element)

So in this case your element size is 8 (thats the size of a double)
If it we an int32 thats 4 bytes and away you go