modifying a c struct in vb.net -
i'm trying utilize else dll coded in c in vb.net application. vb.net application sends callbacks pointers dll , dll calls functions within vb.net application.
the dll calling function within vb.net , passing pointer of c struct. vb.net application has modify struct values, dll can work new struct values. cant modify dlls code because else dll.
typedef struct { uint8_t index; uint8_t value; uint8_t leds[4]; struct { int8_t x; int8_t y; int8_t z; } input[10]; } report;
my vb.net construction definition is
<structlayout(layoutkind.sequential)> _ public construction inputs public x sbyte public y sbyte public z sbyte end construction <structlayout(layoutkind.sequential)> _ public construction study public index byte public value byte <marshalas(unmanagedtype.byvalarray, sizeconst:=4)> public leds() byte <marshalas(unmanagedtype.byvalarray, sizeconst:=10)> public input() inputs sub new(numleds integer, numinputs integer) redim me.leds(numleds) redim me.input(numinputs) end sub end construction
my vb.net delegate declared as
public delegate function del_read(byval lpreport intptr) byte
vb.net sends vb.net delegate c dll
public ext_read del_read = new del_read(addressof read) sendtodll(marshal.getfunctionpointerfordelegate(ext_read))
my vb.net function declared as
public function read(byval lpreport intptr) byte dim study new report(3, 9) report.index = 0 report.value = 5 report.input(0).x = 90 report.input(0).y = 0 report.input(0).z = 0 report.input(1).x = 180 report.input(1).y = 0 report.input(1).z = 0 dim size integer = marshal.sizeof(report) dim ptrmem intptr = marshal.alloccotaskmem(size) marshal.structuretoptr(report, ptrmem, false) dim bytes byte() = new byte(size - 1) {} marshal.copy(ptrmem, bytes, 0, bytes.length) marshal.freecotaskmem(ptrmem) marshal.copy(bytes, 0, lpreport, size) homecoming 1 end function
i next error when function ends , nil else.
additional information: attempted read or write protected memory. indication other memory corrupt.
i have tried different codes, marshal.allochglobal or delcaring function
public function read(byref localreport report) byte
even though code compiles fine, error because of marshal.structuretoptr somehow, because if alter the code 1 bellow no errors , dll obtains new values, problem have calculate manually offsets report.input(0), report.input(1), etc
public function read(byval lpreport intptr) byte dim study new report(3, 9) report.index = 0 report.value = 5 dim size integer = marshal.sizeof(report) dim bytes byte() = new byte(size - 1) {} bytes(0) = report.index bytes(1) = report.value marshal.copy(bytes, 0, lpreport, size) homecoming 1 end function
any ideas why code fails? or simpler way modify construction sent dll? give thanks you.
c vb.net dll interopservices
No comments:
Post a Comment