Psst! Did you know DevelopmentNow is a mobile web site design agency?

Contact us for help mobilizing your site, or to sign up for our beta Mobile Web SDK!
all groups > dotnet interop > september 2003 >

dotnet interop : MarshalDirectiveException - Array size control parameter must be an integral type


Tyler
9/23/2003 10:09:22 AM
I get the following exception when my unmanaged code attempts to invoke the
delegate that follows:

Delegate Declaration:

protected delegate uint
ReadProc(
[MarshalAsUnmanagedType.LPArray,
ArraySubType=UnmanagedType.U1, SizeConst=12)] byte [] fvData,
UIntPtr pfvuiSize,
Object fvContext);

Exception:
An unhandled exception of type
'System.Runtime.InteropServices.MarshalDirectiveException' occurred in
system.windows.forms.dll
Additional information: Array size control parameter must be an integral
type.


I don't understand why the exception is occurring as I have specified an
integral SizeConst.

Background to the problem is as follows:
- allocate array in C# and pass it to P/Invoke worker function along with a
delegate
- worker invokes delegate to populate the array and specify # of bytes
copied into the array
- object is just a parameter passed to the worker function that the worker
passes to the delegate (not important)
- I know how big the array is, because I allocated it - however, I've found
that I need to specify that size when marshalling, otherwise it will think
the array is only 1 byte long in the method passed as the delegate
- when I specify the size, however, I get the exception noted above

I would greatly appreciate any insight into this issue.

Thanks, Tyler

v-yiy NO[at]SPAM online.microsoft.com
9/24/2003 10:23:01 AM
Hi Tyler,
I can reproduce your problem, but haven't got a work around. I'm
doing some research on it and will give you some update soon, thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending, Thanks!

--------------------
| From: "Tyler" <Tyler@work.com>
| Subject: MarshalDirectiveException - Array size control parameter must be
an integral type
| Date: Tue, 23 Sep 2003 10:09:22 -0400
| Lines: 40
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <usvzMxdgDHA.1764@TK2MSFTNGP09.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.interop
| NNTP-Posting-Host: g252.lweb.net 209.167.237.252
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.interop:18985
| X-Tomcat-NG: microsoft.public.dotnet.framework.interop
|
| I get the following exception when my unmanaged code attempts to invoke
the
| delegate that follows:
|
| Delegate Declaration:
|
| protected delegate uint
| ReadProc(
| [MarshalAsUnmanagedType.LPArray,
| ArraySubType=UnmanagedType.U1, SizeConst=12)] byte [] fvData,
| UIntPtr pfvuiSize,
| Object fvContext);
|
| Exception:
| An unhandled exception of type
| 'System.Runtime.InteropServices.MarshalDirectiveException' occurred in
| system.windows.forms.dll
| Additional information: Array size control parameter must be an
integral
| type.
|
|
| I don't understand why the exception is occurring as I have specified an
| integral SizeConst.
|
| Background to the problem is as follows:
| - allocate array in C# and pass it to P/Invoke worker function along with
a
| delegate
| - worker invokes delegate to populate the array and specify # of bytes
| copied into the array
| - object is just a parameter passed to the worker function that the worker
| passes to the delegate (not important)
| - I know how big the array is, because I allocated it - however, I've
found
| that I need to specify that size when marshalling, otherwise it will think
| the array is only 1 byte long in the method passed as the delegate
| - when I specify the size, however, I get the exception noted above
|
| I would greatly appreciate any insight into this issue.
|
| Thanks, Tyler
|
|
|
v-yiy NO[at]SPAM online.microsoft.com
9/25/2003 1:33:22 AM
Hello Tyler,
We already noticed that there is no good way to only specify a
sizeconst for a native array parameter. We are trying to fix this in the
next release. For now, as a work around, could you add a parameter for
passing the size of the array in your delegate signature? If yes you may
rewrite it like this
protected delegate uint ReadProc(

[MarshalAsUnmanagedType.LPArray,ArraySubType=UnmanagedType.U1,
SizeParamIndex =1 )]
byte [] fvData,
int dataSize,
UIntPtr pfvuiSize, Object fvContext);
also, you need to change your unmanaged function signature accordingly, and
pass the size in dataSize param.

SizeParamIndex above indicates that the size of the array will be given by
the parameter at index 1 (with the array parameter at index 0 for a
signature that is not marked with PreserveSig=true ­if the sig is marked
with PreserveSig then the indexing is 1 based).

If you still have problem , please let me know.
Thanks!



Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending, Thanks!

--------------------
| X-Tomcat-ID: 330961814
| References: <usvzMxdgDHA.1764@TK2MSFTNGP09.phx.gbl>
| MIME-Version: 1.0
| Content-Type: text/plain
| Content-Transfer-Encoding: 7bit
| From: v-yiy@online.microsoft.com (Ying-Shen Yu[MSFT])
| Organization: Microsoft
| Date: Wed, 24 Sep 2003 10:23:01 GMT
| Subject: RE: MarshalDirectiveException - Array size control parameter
must be an integral type
| X-Tomcat-NG: microsoft.public.dotnet.framework.interop
| Message-ID: <#CedRXogDHA.2624@cpmsftngxa06.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.interop
| Lines: 68
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.interop:19022
| NNTP-Posting-Host: TOMCATIMPORT2 10.201.218.182
|
| Hi Tyler,
| I can reproduce your problem, but haven't got a work around. I'm
| doing some research on it and will give you some update soon, thanks!
|
| Best regards,
|
| Ying-Shen Yu [MSFT]
| Microsoft Online Partner Support
| Get Secure! - www.microsoft.com/security
|
| This posting is provided "AS IS" with no warranties and confers no rights.
| You should not reply this mail directly, "Online" should be removed
before
| sending, Thanks!
|
| --------------------
| | From: "Tyler" <Tyler@work.com>
| | Subject: MarshalDirectiveException - Array size control parameter must
be
| an integral type
| | Date: Tue, 23 Sep 2003 10:09:22 -0400
| | Lines: 40
| | X-Priority: 3
| | X-MSMail-Priority: Normal
| | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| | Message-ID: <usvzMxdgDHA.1764@TK2MSFTNGP09.phx.gbl>
| | Newsgroups: microsoft.public.dotnet.framework.interop
| | NNTP-Posting-Host: g252.lweb.net 209.167.237.252
| | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
| | Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.interop:18985
| | X-Tomcat-NG: microsoft.public.dotnet.framework.interop
| |
| | I get the following exception when my unmanaged code attempts to invoke
| the
| | delegate that follows:
| |
| | Delegate Declaration:
| |
| | protected delegate uint
| | ReadProc(
| | [MarshalAsUnmanagedType.LPArray,
| | ArraySubType=UnmanagedType.U1, SizeConst=12)] byte [] fvData,
| | UIntPtr pfvuiSize,
| | Object fvContext);
| |
| | Exception:
| | An unhandled exception of type
| | 'System.Runtime.InteropServices.MarshalDirectiveException' occurred in
| | system.windows.forms.dll
| | Additional information: Array size control parameter must be an
| integral
| | type.
| |
| |
| | I don't understand why the exception is occurring as I have specified an
| | integral SizeConst.
| |
| | Background to the problem is as follows:
| | - allocate array in C# and pass it to P/Invoke worker function along
with
| a
| | delegate
| | - worker invokes delegate to populate the array and specify # of bytes
| | copied into the array
| | - object is just a parameter passed to the worker function that the
worker
| | passes to the delegate (not important)
| | - I know how big the array is, because I allocated it - however, I've
| found
| | that I need to specify that size when marshalling, otherwise it will
think
| | the array is only 1 byte long in the method passed as the delegate
| | - when I specify the size, however, I get the exception noted above
| |
| | I would greatly appreciate any insight into this issue.
| |
| | Thanks, Tyler
| |
| |
| |
|
|
Tyler
9/26/2003 2:46:01 PM
Unfortunately, the code that invoked the delegate was not mine, so I was not
able to modify it. To work around the issue I wrote a wrapper in C++ that
modifies the parameters and then the interop works successfully. I had
really wanted to do everything in C#/managed code ... but I guess I'll have
to wait.

Thank-you very much for your help,
Tyler


[quoted text, click to view]

AddThis Social Bookmark Button