How can you Marshal a byte array in C#?
Asked Answered
A

4

8

I'm trying to call the following C++ function that's wrapped up into a DLL:

unsigned char * rectifyImage(unsigned char *pimg, int rows, int cols)

My import statement looks like the following:

[DllImport("mex_rectify_image.dll")]
unsafe public static extern IntPtr rectifyImage(
byte[] data, int rows, int columns);

And my call routine looks like the following:

byte[] imageData = new byte[img.Height * img.Width * 3];
// ... populate imageData
IntPtr rectifiedImagePtr = rectifyImage(imageData, img.Height, img.Width);
Byte[] rectifiedImage = new Byte[img.Width * img.Height * 3];
Marshal.Copy(rectifiedImagePtr, rectifiedImage, 0, 3 * img.Width * img.Height);

However, I keep getting a runtime error:

A first chance exception of type System.AccessViolationException occurred in xxx.dll Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

I'm just wondering if the fault lies in the way I'm marshaling my data or in my imported DLL file... anyone have any ideas?

Allo answered 6/8, 2010 at 18:6 Comment(3)
You might wanna have a look at this question: #289576Stpierre
Is the return value from rectifyImage supposed to be freed, and if so, how?Wooldridge
The return value for rectifyImage is used to create a C# Bitmap object, and then freed. I haven't gotten around to trying to figure out how to actually free it yet.Allo
K
1

This is likely occurring because the calling convention of the method is not as the marshaller is guessing it to be. You can specify the convention in the DllImport attribute.

You don't need the 'unsafe' keyword in the C# declaration here as it's not 'unsafe' code. Perhaps you were trying it with a 'fixed' pointer at one point and forgot to remove the unsafe keyword before posting?

Keffer answered 6/8, 2010 at 18:42 Comment(0)
A
1

not sure if this is your problem, but in general C++ pointers map to IntPtr. so try modifying your import statement to this:

[DllImport("mex_rectify_image.dll")]
unsafe public static extern IntPtr rectifyImage(
IntPtr pData, int rows, int columns);
Adena answered 19/4, 2013 at 16:0 Comment(1)
not sure why i am getting voted down, but i took the time to answer this so i'd at least expect a comment on why my answer is not to your liking! have you actually tried my suggestion and it didnt work?Adena
A
0

unsigned char *pimg can't be marshalled to byte[] automatically.

In order to pass the byte[] imageData you need to marshal it manually to IntPtr and pass said IntPtr to rectifyImage.

This is the recommended pattern for implementing such marshaling:

[DllImport("mex_rectify_image.dll", EntryPoint = "rectifyImage")]
private static extern IntPtr rectifyImageInternal(IntPtr data, int rows, int cols);

public static byte[] rectifyImage(byte[] data, int rows, int cols)
{
    IntPtr dataPtr = IntPtr.Zero;
    byte[] result = new byte[data.Length]; // assumes result is the same size as data
    
    try {
        dataPtr = Marshal.AllocHGlobal(data.Length);
        
        Marshal.Copy(data, 0, dataPtr, data.Length);
        
        IntPtr resultPtr = rectifyImageInternal(dataPtr, rows, cols);
        
        Marshal.Copy(resultPtr, result, 0, result.Length);
        
        // TODO:
        //  - Is resultPtr perhaps the same as dataPtr (in-place transform on source image)?
        //    If not:
        //      - Who is responsible for freeing resultPtr?
        //      - What allocation method was used for resultPtr (global/cotask/etc)?
        //  - Error handling
    } finally {
        Marshal.FreeHGlobal(dataPtr);
    }
    
    return Result;
}
Ary answered 5/8 at 11:59 Comment(0)
L
-1

rectifyImage Is looking for a ponter to a the 1st byte in a block for data you are sending in the block. Try imageData[0]

Luster answered 23/9, 2013 at 7:11 Comment(1)
That's not going to work, since the marshaller would pass a copy of the first byte, rather than its reference. So you'd have to use unsafe code to do that, and pass &imageData[0] rather than just imageData[0].Kitty

© 2022 - 2024 — McMap. All rights reserved.