Pass a c# array to a c++ method

Emir Ahmetović

I have a CLR class library in c++:

namespace ANN_Lib {

    public ref class ANN_FF_BP
    {
    private:
        int neurons;
        int inputs;
        int outputs;

        double **wi;
        double *wl;

    public:
        ANN_FF_BP(int neurons, int inputs, int outputs);

        void Train(double **p, double *t, int pairsCount, int epochs, double goal);
    };
}

I am using this class as a reference in a WPF project:

ANN_FF_BP neuralNetwork = new ANN_FF_BP(neurons, inputs, outputs);

Now I want to call the Train() method (in WPF) from that class and pass in some arguments. I have a problem to pass the first two parameters (pointers) to the method. My current code in C#:

ANN_FF_BP neuralNetwork = new ANN_FF_BP(neurons, inputs, outputs);
double[,] P = new double[3, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
double[] T = new double[] { 2, 4, 6 };
neuralNetwork.Train(P, T, 3, 20, 0.01);

I get the following error in C#:

enter image description here

Can anybody explain me how to pass an C# array to a c++ class library method?

t3chb0t

As your ANN_FF_BP is a ref class and thus a CLR type it's better to pass .NET arrays to the C++/CLI method and do the hardwork there. This way you don't have to declare your C# code as unsafe what would be necessary here if you used pointers in C#.

This should work:

*.h:

void Train(cli::array<double, 2>^ ps, cli::array<double>^ ts, int pairsCount, int epochs, double goal);

*.cpp:

void ClrClass::Train(cli::array<double, 2>^ ps, cli::array<double>^ ts, int pairsCount, int epochs, double goal) {

    // pin pointer to first element in ps
    pin_ptr<double> _ps = &ps[0,0];   
    double* p = _ps;

    // pin pointer to first element in ts
    pin_ptr<double> _ts = &ts[0];
    double* t = _ts;

    // Now with the two arrays being pinned you can call you native method.
    NativeClass::Train((double**)p, t, pairsCount, epochs, goal);
}

To protect the arrays from being moved around in memory by the garbage-collector you need to use the pin_ptr:

A pinning pointer is an interior pointer that prevents the object pointed to from moving on the garbage-collected heap. That is, the value of a pinning pointer is not changed by the common language runtime. This is required when you pass the address of a managed class to an unmanaged function so that the address will not change unexpectedly during resolution of the unmanaged function call.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pass C array to a objective C method

From Dev

How to pass a constant array as argument to a C++ function/method

From Dev

Pass Array by Reference in C

From Dev

Pass time to a method in C#

From Java

how to pass float* array to C method from python script whose memory allocation takes place in C layer

From Dev

C - Passing an array to a method

From Dev

C++ array and method

From Dev

Pass a C array to a Rust function

From Dev

Pass array by reference in C++

From Dev

C# Pass data to array

From Dev

Pass string as a c_ubyte array to C

From Dev

C++ dll pass array to C#

From Dev

How to pass char array from C JNI function to Java method as byte[]

From Dev

How to pass char array from C JNI function to Java method as byte[]

From Java

Pass Method as Parameter using C#

From Java

Pass a C# class property as argument in a method

From Dev

C++ pass another method as argument

From Dev

How to pass optional parameter to a method in C++?

From Dev

(C++) Pass 'this' to a static method as a default parameter

From Dev

C# - How to pass and run a method in a function?

From Dev

Defining an array out of method in C

From Dev

pass array to method with condition

From Dev

C# Pass Class as parameter to method and call static method in there

From Dev

C# Pass Class as parameter to method and call static method in there

From Dev

How to pass C# method as a callback to CLI/C++ function?

From Dev

How to pass a method pointer to a struct (in C/C++)?

From Dev

How to pass C# method as a callback to CLI/C++ function?

From Dev

Pass a C++ method return to a Pro*C procedure

From Java

How to pass this numpy array to C with Ctypes?

Related Related

HotTag

Archive