473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

problem marshalling structures

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?

--
PC
Jul 21 '05 #1
3 2809
[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);


Yuo should probably remove the ref keyword on the lpInBuffer
parameter.

As for calculating the array size, just do

CmdData.Length * 4

or if you don't want to hardcode the size of an int

CmdData.Length * Marshal.SizeOf(typeof(int))

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jul 21 '05 #2
Hi Mattias,
Yuo should probably remove the ref keyword on the lpInBuffer parameter. The presence or absence of ref does not have an effect.

There is a MarshalAs attribute that contains a SizeConst field, apparently
instructions to the marshaler as to how many bytes to copy. However, this
only applies to fields, not to arguments to a function. what I'm looking for
(I think) is a way to tell the marshaller, "Here's how many bytes this
pointer is pointing at." This will vary from call to call, as this is the
nature of DeviceIoControl.
As for calculating the array size...
Yes, I think I mentioned that obtaining the array size is simple. I was
wondering why the marshaller was unable to do so.

PC

"Mattias Sjögren" wrote:
[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);


Yuo should probably remove the ref keyword on the lpInBuffer
parameter.

As for calculating the array size, just do

CmdData.Length * 4

or if you don't want to hardcode the size of an int

CmdData.Length * Marshal.SizeOf(typeof(int))

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Jul 21 '05 #3
The presence or absence of ref does not have an effect.
It should.

There is a MarshalAs attribute that contains a SizeConst field, apparently
instructions to the marshaler as to how many bytes to copy. However, this
only applies to fields, not to arguments to a function. what I'm looking for
(I think) is a way to tell the marshaller, "Here's how many bytes this
pointer is pointing at." This will vary from call to call, as this is the
nature of DeviceIoControl.


It's not needed, since the runtime already knows how much to copy. It
knows the size of each array element, and it knows the length of the
array.

Keep in mind that the nInBufferSize parameter is only information
needed by the callee, not the CLR marshaler.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jul 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: alfacom | last post by:
Hi, I have two C++ structures like these : typedef struct answer_series_item { int32_t contract_size_i; int32_t price_quot_factor_i; char ins_id_s ; char isin_code_s ; uint8_t suspended_c;
26
by: Olaf Baeyens | last post by:
I am trying to port some C++ structures to C# but I have troubles with this one. Note: this is a file record, so I must keep this format. #pragma pack( push, 1 ) typedef struct { char title; ...
1
by: John Ye | last post by:
Hello Folks! I have a vendor that has supplied me with a VB6 Module and DLL (and it works in VB6) that I would really like to migrate to VB.Net. I have used the Upgrade Wizard in VS.Net but it...
3
by: PHil Coveney | last post by:
Hello, I am having difficulty marshalling structures when calling DeviceIoControl. I am importing this Win32 function as static extern int DeviceIoControl (int hDevice, int...
5
by: PickwickBob3 | last post by:
I am trying to obtain a list of HID devices and am trying to use UINT GetRawInputDeviceList( PRAWINPUTDEVICELIST pRawInputDeviceList, PUINT puiNumDevices, UINT cbSize); a USER32.dll. but...
11
by: Bob Yang | last post by:
Hi, I have this in C++ and I like to call it from c# to get the value but I fail. it will be good if you can give me some information. I tried it in VB.net it works but I use almost the same way as...
2
by: d-42 | last post by:
Hi, I'm pretty sure I've just got a Marshalling problem, but I'm completely stumped. If there is a better newsgroup to post this in, please point me towards it. First I'm trying to use...
1
by: d-42 | last post by:
Hi, I'm pretty sure I've just got a Marshalling problem, but I'm completely stumped. If there is a better newsgroup to post this in, please point me towards it. First I'm trying to use...
1
by: roche72 | last post by:
I am having some trouble marshalling data between c++/C# Here is the C++ code: --------------------------------------------------------------------------------- typedef struct { ...... ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.