I'm implementing C# wrapper to link C-library and having trouble passing Enum value as parameter to Unmanaged function call which takes pointer as input. that is throwing this exception. The code looks like this :
Function from library :
int function(infoField Field, void const *value);
Marshal in C#:
[DllImport("CLibrary.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
static extern int function(infoField Field, IntPtr value);
infoField structure :
public enum infoField {section, options, orientation};
One Enum type 'Options' has 4 values to choose from :
public enum Options { var1, var2, var3, var4};
I want to pass one of the 4 choices as value for EnumField = Options, something like this:
IntPtr value = (Intptr)Options.var1;
function (infoField.options, value);
I think I am missing correct cast convention for pointer to point to Enum type. What should be correct format to pass enum value with IntPtr to function? So far, I have tried few options but get AccessViolationException on function call, Which either means memory allocation failed or did not match what what library is expects the type.
PS : Function marshaling is successful for Other EnumFields. That's also because value passing is string value for them. But Particularly for 'options' it has to be one of 4 Enum options.