I am new to .NET compact framework. I need to call a DeviceIoControl function and pass structures as input and output parameters to the IOControl function.
In PInvoke/DeviceIoControl I found how to get access to the function itself. But how can I pass a pointer the structure as InBuf
and OutBuf
parameter?
The DeviceIoControl is defined as P/Invoke:
[DllImport("coredll", EntryPoint = "DeviceIoControl", SetLastError = true)]
internal static extern int DeviceIoControlCE(
int hDevice, int dwIoControlCode,
byte[] lpInBuffer, int nInBufferSize,
byte[] lpOutBuffer, int nOutBufferSize,
ref int lpBytesReturned, IntPtr lpOverlapped);
The structures in question have this layout:
struct Query
{
int a;
int b;
char x[8];
}
struct Response
{
int result;
uint32 success;
}
void DoIoControl ()
{
Query q = new Query();
Response r = new Response();
int inSize = System.Runtime.InteropServices.Marshal.SizeOf(q);
int outSize = System.Runtime.InteropServices.Marshal.SizeOf(r);
NativeMethods.DeviceIoControlCE((int)handle, (int)IOCTL_MY.CODE,
ref q, inSize, ref r, outSize, ref bytesReturned, IntPtr.Zero);
}
Edit: When I try to compile this code I get the error:
cannot convert from 'ref MyNamespace.Response' to 'byte[]'
How can I pass the address of the struct to the DeviceIoControl function what expects a pointer to byte instead of struct ref?