Dealing with array of IntPtr
Posted
by
Padu Merloti
on Stack Overflow
See other posts from Stack Overflow
or by Padu Merloti
Published on 2011-01-06T06:27:33Z
Indexed on
2011/01/06
6:53 UTC
Read the original article
Hit count: 252
I think I'm close and I bet the solution is something stupid.
I have a C++ native DLL where I define the following function:
DllExport bool __stdcall Open(const char* filePath, int *numFrames, void** data);
{
//creates the list of arrays here... don't worry, lifetime is managed somewhere else
//foreach item of the list:
{
BYTE* pByte = GetArray(i);
//here's where my problem lives
*(data + i * sizeofarray) = pByte;
}
*numFrames = total number of items in the list
return true;
}
Basically, given a file path, this function creates a list of byte arrays (BYTE*) and should return a list of pointers via the data param. Each pointing to a different byte array.
I want to pass an array of IntPtr from C# and be able to marshal each individual array in order. Here's the code I'm using:
[DllImport("mydll.dll",EntryPoint = "Open")]
private static extern bool MyOpen(
string filePath, out int numFrames, out IntPtr[] ptr);
internal static bool Open(
string filePath, out int numFrames, out Bitmap[] images)
{
var ptrList = new IntPtr[512];
MyOpen(filePath, out numFrames, out ptrList);
images = new Bitmap[numFrames];
var len = 100; //for sake of simplicity
for (int i=0; i<numFrames;i++)
{
var buffer = new byte[len];
Marshal.Copy(ptrList[i], buffer, 0, len);
images[i] = CreateBitmapFromBuffer(buffer, height, width);
}
return true;
}
Problem is in my C++ code. When I assign *(data + i * sizeofarray) = pByte; it corrupts the array of pointers... what am I doing wrong?
© Stack Overflow or respective owner