G
Guest
Hello,
I am having difficulty marshalling structures when calling DeviceIoControl.
I am importing this Win32 function as
[DllImport("kernel32.dll", SetLastError=true)]
static extern int DeviceIoControl (int hDevice,
int dwIoControlCode,
ref int[] lpInBuffer,
int nInBufferSize,
ref int lpOutBuffer,
int nOutBufferSize,
out int lpBytesReturned,
IntPtr lpOverlapped);
For most commands, the device I am communicating with expects an array of
6 integers as command data.
I try to invoke this like so
int Reply, int BytesReturned;
int[] CmdData = new int[6];
int nResult = DeviceIoControl(m_DriverHandle, ASTROPCI_COMMAND,
ref CmdData, Marshal.SizeOf(CmdData),
ref Reply, Marshal.SizeOf(Reply),
out BytesReturned, IntPtr.Zero);
Using this syntax, I get a runtime error when I perform this
DeviceIoControl invoke:
"Type System.Int32[] can not be marshaled as an unmanaged structure;
no meaningful size or offset can be computed." string
I am confused by why this is a marshalling problem. Since the size of
CmdData is easily obtainable, and
the error message clearly says the marshaller can't figure it out, I'm
assuming there's an issue here I
don't understand.
Question #1: How do I use C# to pass an array of 6 ints in this call?
Question #2: (Less important, but I'd like to understand, if someone has
a quick explanation)
Why is the marshaller unable to figure out the size?
I am having difficulty marshalling structures when calling DeviceIoControl.
I am importing this Win32 function as
[DllImport("kernel32.dll", SetLastError=true)]
static extern int DeviceIoControl (int hDevice,
int dwIoControlCode,
ref int[] lpInBuffer,
int nInBufferSize,
ref int lpOutBuffer,
int nOutBufferSize,
out int lpBytesReturned,
IntPtr lpOverlapped);
For most commands, the device I am communicating with expects an array of
6 integers as command data.
I try to invoke this like so
int Reply, int BytesReturned;
int[] CmdData = new int[6];
int nResult = DeviceIoControl(m_DriverHandle, ASTROPCI_COMMAND,
ref CmdData, Marshal.SizeOf(CmdData),
ref Reply, Marshal.SizeOf(Reply),
out BytesReturned, IntPtr.Zero);
Using this syntax, I get a runtime error when I perform this
DeviceIoControl invoke:
"Type System.Int32[] can not be marshaled as an unmanaged structure;
no meaningful size or offset can be computed." string
I am confused by why this is a marshalling problem. Since the size of
CmdData is easily obtainable, and
the error message clearly says the marshaller can't figure it out, I'm
assuming there's an issue here I
don't understand.
Question #1: How do I use C# to pass an array of 6 ints in this call?
Question #2: (Less important, but I'd like to understand, if someone has
a quick explanation)
Why is the marshaller unable to figure out the size?