Pinvoking a native function with array arguments

后端 未结 2 1151
无人及你
无人及你 2021-01-16 06:53

I am completely confused with how to go about calling functions in native dll with array arguments.

Example:

The function is defined in the C# project as:

2条回答
  •  感情败类
    2021-01-16 07:30

    Your C++ code is broken. The caller allocates the array, and the callee populates it. Like this:

    extern "C" _declspec(dllexport) void modifyArray(int* x, int len)
    {   
        for (int i=0; i

    As far as your p/invoke call goes, SetLastError should not be true. The function is not calling SetLastError. It should be:

    [DllImport("Project2.dll", CallingConvention = CallingConvention.Cdecl)]
    static extern void modifyArray(int[] x, int len);
    

提交回复
热议问题