call a c++ dll func returning a int array from a c# code

hello!

I need a help for this:
i need to call a c++ dll that allocates and returns an int array something like this
struct vett_int {
int vettore_interi[10];
};
static struct vett_int new_vett_int;
extern "C" __declspec (dllexport) vett_int __stdcall intvect(int scelta)
{
{
switch (scelta) {
case 1 : return new_vett_int;
}
return new_vett_int;
}
}
i am calling intvect from a c# application like this
[DllImport("dataex.dll")]
public static extern vettore_di_interi intvect(int choice);
public struct vettore_di_interi
{
public int[] vettore_di_int;
}
main
prova_chiamata_a_dll.Program.vettore_di_interi vettore_interi_instance = new Program.vettore_di_interi();
vettore_di_interi = Program.intvect(1);
at runtime i get the error ::
"Method's type signature is not PInvoke compatible."
The same does not happen when i call another dll returning a simple two field structure like this

other c++ dll

struct retta_per_due_punti {
double Y;
short valido;
};

extern "C" __declspec (dllexport) retta_per_due_punti __stdcall rettadueP(double X_mis, double X2, double X1, double Y2, double Y1)
{ ... }

c# code like this

[DllImport("fumat.dll")]
public static extern retta_due_P rettadueP(
double misura, double X2, double X1, double Y2,double Y1);
public struct retta_due_P
{
public double liv;
public short valido;
}
main
prova_chiamata_a_dll.Program.retta_due_P retta_due_p_instance = new Program.retta_due_P();
retta_due_p_instance = rettadueP(10, 15, 5, 0, 20);
this works correctly


Answer this question

call a c++ dll func returning a int array from a c# code

  • Suganya Mahadevan

    Hi.

    In C# code, try the following:

    public struct vettore_di_interi
    {
    [MarshalAs(UnmanagedType.LPArray)]
    public int[] vettore_di_int;
    }

    I have not tried, but you can use the MarshalAs attribute to pass arrays as parameters, why why not with structs.

  • seu

    Thank you for your help

    The reality is that I already try what you suggest but I got the same behaviour.

    Let me know if you have other ideas.

    Best regards

    rodrigo


  • call a c++ dll func returning a int array from a c# code