0

I have spent a lot of days trying to solve this problem. I have a c++ dll function that takes as a parameter a "std::vector<int*> *list", I need to pass a compatible pointer from c# in Unity. I tried in this way:

UPDATE c++ code:

#include <stdio.h>
#include <array>
#include <vector>
#if _MSC_VER 
#define EXPORT_API  
#else
#define EXPORT_API __declspec(dllexport)
#endif

extern "C"{
    /* Other function */
 

    EXPORT_API void GetBlocks(std::vector<int*> *list, int* listSize){
                
        list->push_back(std::vector<int>(16*16*16, 1).data());
        (*block_size)++;
    }

}

c# code:

[DllImport("dllLib", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
unsafe private static extern void GetBlocks(ref List<int*[]> block, ref int blockSize);

void Start() {
    int blockSize = 0;
    List<int*[]> block = new List<int*[]>();

    unsafe
    {
        GetBlocks(ref block, ref blockSize);
    }
}

Unity returns this error:

MarshalDirectiveException: Type System.Collections.Generic.List`1<int[]> which is passed to unmanaged code must have a StructLayout attribute. *

I suppose that I have to manage somehow the conversion using Marshal, but right now I don't know. Maybe there could be another type instead of List<int*[]> to pass a list of the pointer to an array.

Can you help me?

Thanks

  • 1
    Is you r c++ dll managed or unmanaged? A managed c++ does not need DllImport. You can call the c++ methods like any c# method. – jdweng Nov 30 '20 at 15:20
  • If I understood the difference between managed and unmanaged, my c++ is unmanaged. For completeness, I add the code listing in the main massage – user14653525 Nov 30 '20 at 16:29
  • See following : https://stackoverflow.com/questions/31417688/passing-a-vector-array-from-unmanaged-c-to-c-sharp – jdweng Nov 30 '20 at 16:41
  • Thanks I try to adapt that solution to my case – user14653525 Nov 30 '20 at 17:33

0 Answers0