I have been playing with some interop over the last copule of days and
have a small problem. I am trying to use DeviceIOControl to talk to a
custom driver. I have successfull called CreateFile and have used
DeviceIOControl to initialise the device. The problem I have is when i
try to send data.
I have a struct
typedef struct POLL_STRUCT {
UINT nSize; // size of struct
UINT nPollSize; // number of bytes to send
UINT nMaxSize; // max chars expected
BYTE zBuffer[MAX_RX_SIZE]; // (poll->driver,
driver->poll+response)
} TPOLL_STRUCT;
MAX_RX_SIZE is always 2048 bytes.
I have declared the c# struct as:
[StructLayout(LayoutKind.Sequential)]
struct POLL_STRUCT {
public uint nSize;
public uint nPollSize;
public uint nMaxSize;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2048)]
byte [] zBuffer;
};
Now I thought that MarshalAs attribute would create the zbuffer
instance, but when i try to reference it it is null?
Now when i try and marshal the TPOLL_STRUCT;
lpPollBuffer =
Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TPOLL_STRUCT)));
I seems to work fine, however, if i try to copy data to the array,
Array.Copy(fooArray,abyPollBuffer,fooArray.Length); <-- Exception
The Exception is an argument null exception. This makes sense, as i
already know that zBuffer is null. But AllocHGlobal has obviously
allocated the 2048 bytes on the heap, where is it in the managed code?
If I create and instance of the buffer like so;
abyPollBuffer.zbuffer = new byte[2048];
does this data get marshaled to the unmanaged heap?
I can for the life of me find and way to view the actual memory in the
c# debugger either, so I cant confirm.
Furthermore, If I was to make a struct like so:
[StructLayout(LayoutKind.Sequential)]
struct FOO{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2048)]
byte [] data;
uint uiMoreData;
};
Is uiMoreData 2048 bytes after of data[0] ?? How in VS2005 do I view
the addresses of the data?
Sorry if these questions sound stupid. I was all excited about moving
to c# (from c++) now I am getting increasingly frustrated. I am sure
that once I adjust all will be good. I do miss my pointers though ;).
The Real Andy - 20 Mar 2006 12:20 GMT
>I have been playing with some interop over the last copule of days and
>have a small problem. I am trying to use DeviceIOControl to talk to a
[quoted text clipped - 66 lines]
>
>
One more question,
If I make struct FOO{..};
class FOO {
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2048)]
public byte [] data;
public uint uiMoreData;
FOO()
{
data = new data[2048];
}
};
Will this marhsal correctly to the c version of the struct?