c# - Can't convert a Marshal pointer of a struct back to struct -
i'm coding windows service communicates driver, such i'm trying to, test, pass struct , returned struct.
in c++ working in c# i'm not being able convert intptr struct using marshal , that's why i'm not able struct returned driver.
struct:
[structlayout(layoutkind.sequential)] struct testest { public int value; }
then in driver:
typedef struct _test_est { int value; } test_est, *ptest_est;
and code going pass struct via ioctl is:
void sendioctl<t>(intptr hdevice, uint dwiocontrolcode, ref t inobj, ref t outobj) { intptr inpointer = intptr.zero; intptr outpointer = intptr.zero; int inobjsize = 0; int outobjsize = 0; uint bytesreturned = 0; if(inobj != null) { inobjsize = marshal.sizeof(inobj.gettype()); inpointer = marshal.allochglobal(inobjsize); marshal.structuretoptr(inobj, inpointer, false); if (dwiocontrolcode == test_ctl) // test ioctl { testest lets = new testest(); logger.log("isnull: " + (inpointer == intptr.zero)); logger.log("sizeobj: " + inobjsize); marshal.ptrtostructure(inpointer, lets); logger.log("working!: " + lets.value); } } } public void sendtest() { testest request = new testest(); testest result = new testest(); request.value = 30; sendioctl(hdriver, test_ctl, ref request, ref result); logger.log("ra: " + result.value + " " + request.value); }
logger.log() writes entry windows event viewer.
i omitted actual deviceiocontrol because that's not fails, happening in the
marshal.ptrtostructure(inpointer, lets);
is causing service show in logs:
and inpointer not null , inobjsize 4
i tried removing t, sendioctl , placing t it's same.
thanks in advance
to marshal pointer structure use approach
var test = (t) marshal.ptrtostructure(inpointer, typeof(t));
do not forget use marshal.freehglobal(inpointer)
when you're done our allocated memory blocks.
Comments
Post a Comment