- Home /
Passing a string as a const char* to C++ library plugin
Hi,
I have a C++ audio library (ZenGarden) that I'm running as a plugin in Unity, this library opens and plays back Pure Data patches.
I'm trying to pass the filepath of a patch to open as a const char* to a function declared in the imported C++ library, but struggling on converting it correctly.
The function is defined as follows in the C++ library:
ZGGraph *zg_context_new_graph_from_file(ZGContext *context, const char *directory, const char *filename);
And in the C# script as follows:
using UnityEngine;
using System;
using System.Runtime.InteropServices;
using System.IO;
public class ZGUnity : MonoBehaviour {
[DllImport ("__Internal")]
private static extern IntPtr zg_context_new_graph_from_file(IntPtr context,
[MarshalAs(UnmanagedType.LPStr)]String directory,
[MarshalAs(UnmanagedType.LPStr)]String filename);
void Awake () {
IntPtr graph = zg_context_new_graph_from_file(context, Application.dataPath, "/ball.pd");
}
}
When building the app on an iPad it crashes on the splash screen. If I comment out the code in Awake() it runs fine.
I've found a few others having similar issues but none of the suggested solutions have worked for me, for example. I've tried just declaring it as a string, looked at the StringBuilder class and marshalling (as seen in the code I've posted above). I'm very new to C#, if anyone has any pointers I'd much appreciate your input.
Thanks, Joe
Have you tried it on a stub C function that just prints its arguments, to make sure it's the binding itself causing problems?