问题
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.
回答1:
There are a few problems with your example:
- 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.
- 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.
回答2:
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
回答3:
This is not good prototype for interop. You can two ways to solve this:
- Change C++ function prototype and return all doubles through parameters (I hope you have source of C++ func)
- Use IntPtr as described by Rest Wing
Returning memory reference would be working properly only in case that the memory was allocated with CoTaskMemAlloc function. Othrwise you will take runtime exception during memory freeing (the CLR always try to free returnning memory with CoTaskMemFree).
I think the best would be the first way, it described in Peter Huene's answer.
来源:https://stackoverflow.com/questions/5072340/c-sharp-marshalling-double-from-c-dll