I need to wrap a c dll so that it can be called from c#, the issue is that a property of the struct passed in is complex. Frustratingly the child property is only accessed internally in the c code, but it needs to be defined in the c# wrapper.
struct outer {
.
.
.
unsigned int rsi;
unsigned int flags;
struct internal_state *state;
};
struct internal_state {
int (*mode)(struct outer *);
int (**id_table)(struct outer *);
void (*flush_output)(struct outer *);
int id;
int id_len;
.
.
.
}
In the c code there are three methods, essentially init, act, end each taking an instance of the outer struct. The offending properties are mode, id_table and flush_output if I leave them out of the struct I can call init and it returns successfully. Init sets the state property on the outer but on inspection state does not have any of the values set.
If I try to add in mode, id_table and flush_output as Func/Action types the call blows up with the following exception (unsurprisingly)
Cannot marshal field ‘mode’ of type ‘internal_state’: Non-blittable generic types cannot be marshaled.
Given the c# code does not touch the state property is there any way to “con” the c#/c code for that property?
Example c# code
public unsafe byte[] GetData(byte[] data)
{
var myOuter = new outer
{
.
.
.
rsi = 128,
flags = 8
};
var intitResponse = Init(ref myOuter);
var actResponse = Act(ref myOuter, 0);
var endResponse = End(ref myOuter);
return dataOut;
}
[DllImport("lib/aec.dll", EntryPoint = "init")]
public static extern int Init(ref outer outr);
[DllImport("lib/aec.dll", EntryPoint = "act")]
public static extern int Act(ref outer outr, nuint flush);
[DllImport("lib/aec.dll", EntryPoint = "end")]
public static extern int End(ref outer outr);
public struct outer
{
.
.
.
public nuint rsi;
public nuint flags;
public internal_state state;
}
I have tried leaving off the complex properties but that causes the state property to not be initialised properly in the init method, even though it returns a 0 from the call.
Simon King is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.