Tuesday, September 30, 2014

Read dynamic data from unmanaged memory

I have images comming back from an unmanaged DLL. The size of the image is dynamic = it can change from a call to the other


For this I have a structure reprenting the image + its size that looks like :



[StructLayout(LayoutKind.Sequential)]
private unsafe struct StructImage
{
public uint imageDate;
public uint imageSize;
public byte* imageData;
}

I get from the application a pointer to my structure. I read the info like this



IntPtr imagePtr = ...
StructImage imageInfo = (StructImage)Marshal.PtrToStructure(imagePtr, typeof(StructImage));

Now I would get the size of the image and I would like to read the data like this



byte[] buffer = new byte[imageInfo.imageSize];
IntPtr srcPtr = new IntPtr(imageInfo.imageData);
Marshal.Copy(srcPtr, buffer, 0, buffer.Length);

At this point I get an "AccessViolationException"


I understand that the Marshal.PtrToStructure declares that I will be reading unmanaged memory according to the size of my structure = when it comes to the imageData field only 4 bytes are declared "readable" for the byte*


I red hundreds of forums/blogs talking about marshalling between managed and unmanaged memory but I still cannot figure out how I can extend the "reserved" unmanaged memory to the length of my data


Can someone help me out ther ?


Thanks a lot


Christophe


No comments:

Post a Comment