开发者

Call C++ from C# Array MarshalAs Problems?

开发者 https://www.devze.com 2023-03-20 21:28 出处:网络
So I have a function inside of a C++ library: double MyFunc(double** data, 开发者_如何学编程int length)

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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消