- Home /
How do i use typedefs, enums declared in my C plugin .h?
I am learning to write C plugins for Unity.
From C# I can access functions declared in my C code using dllImport. But now I would like to also access typedef enums etc declared in it .h file.
For instance in my C .h file I have something like:
typedef struct _myStruct * myStruct_handle_t;
and
void myCFunction( myStruct_handle_t myStruct );
now when i try importing this function in C# script with dllImport, I get the following error:
Assets/PluginImport.cs(28,49): error CS0246: The type or namespace name `myStruct_handle_t' could not be found. Are you missing a using directive or an assembly reference?
How can I use types defined in my C code? For instance is there a way to include my .h file in my c# script?
thanks,
Baba
Answer by baba · Apr 09, 2014 at 10:13 PM
Actually in my case the type i want to use is an opaque pointer. Therefore I do not need (want) to expose the internals of my struct to the c# script. Therefore the solution is to simply write IntPtr instead of myStruct_handle_t in my dllImport extern declaration and it works.
For the enum I just redefined them as public enum myEnum_t : int { / .. enums, /} in my C# script
_met44 solution seems to be the correct solution in the case where you want to have access to your struct fieldsfrom C# but I haven't tried it.
Answer by _met44 · Apr 07, 2014 at 11:25 PM
you need to redefine that in C# with [StructLayout(LayoutKind.Sequential)] check out this msdn page it has some good example (and that is a rare thing :D) of what you have to do :
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.layoutkind.aspx
Thanks! Would that process be any easier if I used javascript ins$$anonymous$$d of c# in unity? (I am equally beginner in both javascript and c#)
I have no idea since I haven't done that using JS, but I would go with C# since it is well documented and really easy to implement...