且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Cli包装器将非托管地址传递给C#中的托管函数

更新时间:2022-10-25 18:25:45

这篇带有代码示例的文章可能会引起您的兴趣:

C#中的非托管数组 - 没问题 [ ^ ]



祝你好运!

Good morning,

I am working on a project which use C# code, a C\CLI wrapper and C++ code. Actually, I have to call an managed function inside a C++ function.
My function in C++ has this prototype :

void TestArray(float** array1, int numRows, int numCols);



Inside this function I call a C# function which has this prototype :

public void TestArray(ref float[,] arrayTest, int numRows, int numCols);



I created in the C++ function TestArray a cli::array which contains the data of the array "array1" and after I passed to the C# function. See below the code :

cli::array<float,2>^ array1_test = gcnew cli::array<float,2>(numRows,numCols); 
...
//Call the C#function 
TestArray(array1_test, numRows, numCols );


But by using this way, I used lot of copy I guess and I would like to know if it is possible to use the address of the array (the one passed in argument by float**) and passed this one directly to the C# function.

Thank you in advance for any suggestions.

This article with code example may be of interest to you:
Unmanaged Arrays in C# - No Problem[^]

Good luck!