locked
Passing a C# array to a funcion in C++ RRS feed

  • Question

  • I have a function writen  in C++ and i need to call it from C#, i need to pass an array as argument but can't find how to.

    C#:

    val = func(array);

    C++:

    int func(??){...}

    how can i achieve this?
    Thursday, January 21, 2010 10:28 PM

Answers

  • What would be interesting is the signature of the C++ function.


    Hehe.. I think that's the question. Since the type wasn't provided, I'll guess it's an array of integers.

    C#
    [DllImport("mydll.dll")]
    public static extern int func(int[] lpArray, int arrayLength);


    C++
    extern "C" int __stdcall func(int* lpArray, int arrayLength)
    {
        for (int n = 0; n < arrayLength; n++)
        {
            int value = lpArray[n];
            ...
        }
    }
    • Proposed as answer by Louis.fr Sunday, January 24, 2010 4:44 AM
    • Marked as answer by Bin-ze Zhao Tuesday, January 26, 2010 9:38 AM
    Saturday, January 23, 2010 9:35 PM

All replies

  • What would be interesting is the signature of the C++ function.

    Friday, January 22, 2010 3:32 AM
  • What would be interesting is the signature of the C++ function.


    Hehe.. I think that's the question. Since the type wasn't provided, I'll guess it's an array of integers.

    C#
    [DllImport("mydll.dll")]
    public static extern int func(int[] lpArray, int arrayLength);


    C++
    extern "C" int __stdcall func(int* lpArray, int arrayLength)
    {
        for (int n = 0; n < arrayLength; n++)
        {
            int value = lpArray[n];
            ...
        }
    }
    • Proposed as answer by Louis.fr Sunday, January 24, 2010 4:44 AM
    • Marked as answer by Bin-ze Zhao Tuesday, January 26, 2010 9:38 AM
    Saturday, January 23, 2010 9:35 PM
  • What would be interesting is the signature of the C++ function.
    Hehe.. I think that's the question. Since the type wasn't provided, I'll guess it's an array of integers.

    Right. I hadn't read the question correctly.
    Sunday, January 24, 2010 4:44 AM