unity crash when call a pointer type variable in csharp

I don’t know how to use a ANSI C in unity, so I am trying to rewrite a c code into csharp.

The original code in C is:
// int32 __CFUNC CreateTask (const char taskName[ ], void* *taskHandle); //define in the header file
CreateTask(“”,&taskHandle);

My csharp code:

[DllImport(“dllfilename”)]
private static extern Int32 CreateTask(char taskName, System.IntPtr taskHandle);
void Start () {
System.IntPtr taskHandle=(IntPtr)0;
char taskname = ‘W’;
CreateTask(taskname, taskHandle);
}

But unity crashed when I try to run the code, anyone knows why? Do I use pointer variable correctly here?

I’m gonna take a stab at this since I’m a better C coder than C#, but, I dobnt know if the info im giving even relates to your problem.

  1. you need to enclose code with pointers with the “unsafe” keyword. But since you arnt declaring or returning a pointer int he traditional way, this may not apply to you. If it does, then that brings us to our 2nd point
  2. you cant use "unsafe"code (pointers) with unity.

so, if you need to use a pointer, then you are out of luck. You will have to re-do your dll code to not pass pointers to unity. Your dll code can use pointers for its own internal uses, just don’t expect unity to be able to use those pointers.