Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Marshalling double* from C++ DLL?

I have a C++ DLL with an exported function:

extern "C" __declspec(dllexport) double* fft(double* dataReal, double* dataImag)
{
  [...]
}

The function calculates the FFT of the two double arrays (real and imaginary) an returns a single double array with the real an imaginary components interleaved: { Re, Im, Re, Im, ... }

I'm not sure how to call this function in C#. What I'm doing is:

[DllImport("fft.dll")]
static extern double[] fft(double[] dataReal, double[] dataImag);

and when I test it like this:

double[] foo = fft(new double[] { 1, 2, 3, 4 }, new double[] { 0, 0, 0, 0 });

I get a MarshalDirectiveException exception:

Cannot marshal 'return value': Invalid managed/unmanaged type combination.

I'm assuming this is because C++ double* isn't quite the same as C# double[], but I'm not sure how to fix it. Any ideas?

Edit: I've changed the signatures so that I now pass some extra information:

extern "C" __declspec(dllexport) void fft(double* dataReal, double* dataImag, int length, double* output);

We always know the length of output will be 2x length

and

[DllImport("fft.dll")]
static extern void fft(double[] dataReal, double[] dataImag, int length, out double[] output);

tested like this:

double[] foo = new double[8];
fft(new double[] { 1, 2, 3, 4 }, new double[] { 0, 0, 0, 0 }, 4, out foo);

Now I'm getting an AccessViolationException rather than a MarshalDirectiveException.

like image 670
Ozzah Avatar asked Feb 21 '11 23:02

Ozzah


2 Answers

There are a few problems with your example:

  1. The C++ code has no idea how big those arrays are. The marshaller will pass them a valid pointer, but without a corresponding length parameter, there's no way to tell how big they are. sizeof(dataReal) and sizeof(dataImag) is likely 4 or 8 on most platforms (i.e. sizeof(void*)). Probably not what you intended.
  2. While it's possible to marshal a pointer back as the return value (you could then use it to populate a managed array), there is no implied owner of the return value's memory, leaving open the possibility of memory leaks. Was the buffer allocated inside of fft with new? If so, then you'd need another export the managed code can call to free the memory or use LocalAlloc instead (and then Marshal.FreeHGlobal on the managed side). That's problematic at best.

Instead, my suggestion would be to define fft this way:


extern "C" __declspec(dllexport) void __stdcall fft(double const* dataReal, int dataRealLength, double const* dataImag, int dataImagLength, double* result, int resultLength)
{
  // Check that dataRealLength == dataImagLength
  // Check that resultLength is twice dataRealLength
}

The corresponding P/Invoke signature would be:


[DllImport("fft.dll")]
static extern void fft(double[] dataReal, int dataRealLength, double[] dataImag, int dataImagLength, double[] result, int resultLength);

And then an example of a call:


double[] dataReal = new double[] { 1.0, 2.0, 3.0, 4.0 };
double[] dataImag = new double[] { 5.0, 6.0, 7.0, 8.0 };
double[] result = new double[8];
fft(dataReal, dataReal.Length, dataImag, dataImag.Length, result, result.Length);

Edit: updating based on what fft is described to do.

like image 169
Peter Huene Avatar answered Oct 01 '22 14:10

Peter Huene


There is no need to change signature. You can use following:

[DllImport( "fft.dll", EntryPoint = "fft" )]
public static extern IntPtr fft( double[] dataReal, double[] dataImag );

Then you will need to copy bytes from returned IntPtr. Since you know the size of the output is double of the input, you do it in following way:

double[] result = new double[ doubleSize ];
Marshal.Copy( pointer, result, 0, doubleSize );

The result will contain bytes returned by the fft function.

EDIT: I believe you will find P/Invoke Interop Assistant tool helpful: Managed, Native, and COM Interop

like image 33
Rest Wing Avatar answered Oct 01 '22 14:10

Rest Wing