C# MarshalException C code
Hi all!.
I would like use unmanaged code from C in C#.
I built a DLL with C code with this functions:
struct GetPluginData
{
int data[22];
};
DLLEXPORT extern "C" __declspec (dllexport) GetPluginData GetDataArray(int number);
In C# I've got this code:
[StructLayoutAttribute(LayoutKind.Sequential, Pack=1)]
public unsafe struct GetPluginData
{
/// int[22]
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 22, ArraySubType = UnmanagedType.I4)]
public int[] data;
}
[DllImport("RBRPlugin.dll", CallingConvention = CallingConvention.StdCall, EntryPoint = "GetDataArray")]
[return: MarshalAs(UnmanagedType.Struct)]
public static extern GetPluginData GetDataArray(int number);
In button event or othe place code, I wrote this:
GetPluginData tes = GetDataArray(1);
And I'm getting the error: The type signature of this method is not PInvoke compatible.
I'm looking for information throught google, but no result found...
Thanks in advance!
Re: C# MarshalException C code
You can't return structs from PInvoke methods.
You'll have to do it like this :
Code:
// C++
struct GetPluginData
{
int data[22];
};
DLLEXPORT extern "C" __declspec (dllexport) void GetDataArray(int number, GetPluginData *pData);
And in c#
Code:
[StructLayoutAttribute(LayoutKind.Sequential, Pack=1)]
public unsafe struct GetPluginData
{
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 22, ArraySubType = UnmanagedType.I4)]
public int[] data;
}
[DllImport("RBRPlugin.dll", CallingConvention = CallingConvention.CDecl, EntryPoint = "GetDataArray")]
public static extern void GetDataArray(int number, ref GetPluginData val);
Also note the calling convention is CDecl by default for __declspec(dllexport). The extern "C" means the method name exported by the dll isn't decorated.
Darwen.