float arrays from C++ to C#

J

joesfer

I'm trying to develop a graphical user interface for a renderer i've
got written in an unmanaged C++ DLL with C#. During the rendering
process, several images are sent to a delegate as float* arrays as
follows:

[C++ DLL code ] -------------------------------

void (__stdcall *m_ImageProgressReporter)(float* RGB, int width, int
height); <-- function pointer obtained from C# client's delegate
(below)


float* RGB = new [3*Imagewidth*ImageHeight]
<... fill the RGB array..>
ImageProgressReporter(RGB, ImageWidth, ImageHeight);

--------------------------------------------------------

And i'm trying my C# client to get these float* arrays to map them into
Bitmap instances. Nonetheless i'm not being able to get these arrays
properly. I've tried two approaches for the delegate:

1. Using float[] parameter

[ C# ] --------------------------------------------------------------

public delegate void ImageProgressReporterDelegate(float[] RGB, int
width, int height);

[DllImport("mydll.dll", CallingConvention = CallingConvention.StdCall)]
public static extern bool
RenderFromCSharp(ImageProgressReporterDelegate
ImageProgressReporterCallback);

RenderFromCSharp(new
ImageProgressReporterDelegate(this.ImageProgressReporter));

where:

private void ImageProgressReporter(float[] RGB, int width, int height)
{
<process RGB values>
}
-------------------------------------------------------------------------

With this approach the float[] RGB array I get contains only one float
value (length = 1), the rest is missing. So I tried to work around it
like this:

2. Using IntPtr to get the array:

[ C# ] --------------------------------------------------------------

public delegate void ImageProgressReporterDelegate(IntPtr pRGB, int
width, int height);

[DllImport("mydll.dll", CallingConvention = CallingConvention.StdCall)]
public static extern bool
RenderFromCSharp(ImageProgressReporterDelegate
ImageProgressReporterCallback);

RenderFromCSharp(new
ImageProgressReporterDelegate(this.ImageProgressReporter));

where:

private void ImageProgressReporter(IntPtr pRGB, int width, int height)
{
// try to get back the array data
float[] RGB = new float[width*height*3];
Marshal.Copy(pRGB, RGB, 0, width*height*3);
<process RGB values>
}
-------------------------------------------------------------------------

Alas, this second approach seems to get corrupted data (first values
look right but then I get random huge values)

Can anybody please hint me about what can I be doing wrong?

Thanks in advance,

Jose.
 
B

Ben Voigt

joesfer said:
I'm trying to develop a graphical user interface for a renderer i've
got written in an unmanaged C++ DLL with C#. During the rendering
process, several images are sent to a delegate as float* arrays as
follows:

You will save yourself a lot of headaches by writing the glue code in
C++/CLI. As they say... It Just Works.

[C++ DLL code ] -------------------------------

void (__stdcall *m_ImageProgressReporter)(float* RGB, int width, int
height); <-- function pointer obtained from C# client's delegate
(below)


float* RGB = new [3*Imagewidth*ImageHeight]
<... fill the RGB array..>
ImageProgressReporter(RGB, ImageWidth, ImageHeight);

--------------------------------------------------------

And i'm trying my C# client to get these float* arrays to map them into
Bitmap instances. Nonetheless i'm not being able to get these arrays
properly. I've tried two approaches for the delegate:

1. Using float[] parameter

[ C# ] --------------------------------------------------------------

public delegate void ImageProgressReporterDelegate(float[] RGB, int
width, int height);

[DllImport("mydll.dll", CallingConvention = CallingConvention.StdCall)]
public static extern bool
RenderFromCSharp(ImageProgressReporterDelegate
ImageProgressReporterCallback);

RenderFromCSharp(new
ImageProgressReporterDelegate(this.ImageProgressReporter));

where:

private void ImageProgressReporter(float[] RGB, int width, int height)
{
<process RGB values>
}
-------------------------------------------------------------------------

With this approach the float[] RGB array I get contains only one float
value (length = 1), the rest is missing. So I tried to work around it
like this:

2. Using IntPtr to get the array:

[ C# ] --------------------------------------------------------------

public delegate void ImageProgressReporterDelegate(IntPtr pRGB, int
width, int height);

[DllImport("mydll.dll", CallingConvention = CallingConvention.StdCall)]
public static extern bool
RenderFromCSharp(ImageProgressReporterDelegate
ImageProgressReporterCallback);

RenderFromCSharp(new
ImageProgressReporterDelegate(this.ImageProgressReporter));

where:

private void ImageProgressReporter(IntPtr pRGB, int width, int height)
{
// try to get back the array data
float[] RGB = new float[width*height*3];
Marshal.Copy(pRGB, RGB, 0, width*height*3);
<process RGB values>
}
-------------------------------------------------------------------------

Alas, this second approach seems to get corrupted data (first values
look right but then I get random huge values)

Can anybody please hint me about what can I be doing wrong?

Thanks in advance,

Jose.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top