Call C++ from C# Array MarshalAs Problems?
So I have a function inside of a C++ library:
double MyFunc(double** data, 开发者_如何学编程int length)
{
//data elements are accessed like this
(*data)[i] = 5.0;
}
In C# I access this function in this way:
//import
[DllImport(@"MYDLL.dll")]
public static extern double MyFunc(ref double[] data, int length);
//usage
MyFunc(ref data, data.Length);
This is silly since I would rather write:
double MyFunc(double* data, int length)
{
//data elements are accessed like this
data[i] = 5.0;
}
The problem is, I do not know how I could access the desired C++ function from C#...I am not well versed in Marshaling values...How would I do this?
You can simply pass a double[]
directly.
If you are asking bout how to create that same function in c#
, then you are asking about unsafe
code in C# check this and this.
your code would be:
unsafe double MyFunc(double* data, int length)
{
//data elements are accessed like this
data[i] = 5.0;
}
精彩评论