c++ - C# calling deviceIOControl with complex structures -
so trying write c# wrapper talk 1 of our device drivers. (creating unit test) driver new, coded against old c++ headers, construction layouts defined, , can't change.
so have replicated c++ structures device expecting deviceiocontrol pass in.
update #3 - changing code demo code has same issue. cleaning question more usable others, see reply below
[structlayout(layoutkind.sequential, pack=1)] public class points { public int id; [marshalas(unmanagedtype.byvalarray, sizeconst=10)] public int[] x = new int[10]; [marshalas(unmanagedtype.byvalarray, sizeconst=10)] public int[] y = new int[10]; }; [structlayout(layoutkind.sequential, pack=1)] public class shape { public int name; [marshalas(unmanagedtype.byvalarray, sizeconst=10)] public points[] p = new points[10]; }; [structlayout(layoutkind.sequential,pack1)] public class geoshape:shape { public int top; public int left; [marshalas(unmanagedtype.byvalarray, sizeconst=10)] public int[] v = new int[10]; }; my phone call deviceiocontrol fails, because on driver side checks size of buffer passed in. on c# side, object little marshal.sizeof() returns 52 size, when should 852, if add together size= structlayout attribute, function "pass", sure info not beingness correctly passed.
i sure issue public points[] p = new points[10]; think marshal.structtoptr() not correctly marshaling multi-dimensional array.
so guess questions possible? seems c# smart plenty know how create right amount of space in memory array of structure.. maybe not?
alternatives thought of "could" work.
write custom serailizers convert object byte[] , back, 0 meta-data. - not ideal.
would possible write mixed clr c++ dll , seek utilize wedge. concerns, going have same issue, in managed c++? or in mixed mode have write managed class wrap un-manged object utilize in c#. issue becomes how pass deviceiocontrol, if c# have current issue of trying marshal stuff correctly? or if pass c++ phone call calls deviceiocontrol, need know how un-manged type of each manged object passed in.
just write c++ functions create objects , phone call deviceiocontrol, less thought parameters out of control?
give , in c++, seek write unit test hardware, , newer cpp unit test in vs integrate nicely...
i saw before question, , gave try, think scenerio little different. un-/marshalling nested structures containing arrays of structures
struct points { int id; int x[10]; int y[10]; }; struct shape { int name; points p[10]; }; struct geoshape :shape { int top; int left; int v[10]; }; updated 2 should clarify trying send object driver, not receive 1 (not yet atleast)
this how calling it.
public static bool setobject(safefilehandle device, devicecontrolcode ioctlcode, object obj) { int size = marshal.sizeof(obj.gettype()); intptr ptr = marshal.allochglobal(size); marshal.structuretoptr(obj, ptr, false); // phone call dviceiocontrol method homecoming control(device, ref ioctlcode, ptr, size, intptr.zero, 0); }
i don't know what's on headers starting point consider reading this when utilize struct or class
as alert... sure pack = 1? have #pragma setting 1?
if provide related .h code easier check might wrong. anyway, available information, this:
[structlayout(layoutkind.sequential, pack = 1)] public struct venus_format4 { public uint top; public uint left; public uint rows; public uint columns; [marshalas(unmanagedtype.byvalarray, sizeconst = constants.max_cd_rows)] public uint[] v65rows; [marshalas(unmanagedtype.byvalarray, sizeconst = constants.max_cd_cols_dd2)] public uint[] cdcols; [marshalas(unmanagedtype.byvalarray, sizeconst = constants.max_dd_sections)] public uint[] ddsections; } the rest same above except in venus_vm4_device_format4il have "replicate" fields because cannot inherit when using structs (in c# (type values)).
also, if on c++ side have unions not going work , should utilize layoutkind.explicit , fieldoffset.
c# c++ pinvoke marshalling deviceiocontrol
No comments:
Post a Comment