- Home /
 
return char** from C++ DLL
I'm trying to write pluging which will return array of strings from C++ DLL to C# Unity.
I have no idea how to do it. I have found some code(http://joshhendo.com/2011/02/calling-c-functions-in-c-and-passing-arrays/), but it always crash Unity. Any help?
C++
 #define ROCKGENERATOR_API __declspec(dllexport) 
     
     extern "C" {
         ROCKGENERATOR_API     char**  Permutations(char * startString);
     }
 
               __
     char**  Permutations(char * startString)
         {
                     ............
             char ** permutations = new char*[us.size()];
                      ...
             return permutations;
     
         }
 
               Unity C#
     [DllImport("StoneGeneratorDLL")]
     public static extern System.IntPtr Permutations(string s);
     public static string[] ReturnStringArray(int length)
     {
         string[] ReturnArray = new string[length];
         for (int j = 0; j < length; j++)
             ReturnArray[j] = Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(Permutations("ab"), 4 * j));
         return ReturnArray;
     }
     void Start()
     {
         string[] myArray = ReturnStringArray(3);
         foreach (string item in myArray)
             Debug.Log(item);// Console.WriteLine(item);
     }
 
              
               Comment
              
 
               
              Your answer