What does it want? "... does not match declared length ..."

T

Tom

I'm getting this error when I try to pass a structure to a dll.

An unhandled exception of type 'System.ArgumentException' occured in
Test1.exe
Additional Information: Type could not be marshaled because the length
of an embedded array instance does not match the declared length in the
layout

What does it mean?


Here's the structure:

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Pack:=1)> _
Public Structure udtINTER01
<VBFixedString(8), MarshalAs(UnmanagedType.ByValArray, SizeConst:=8)>
Public action As Char()
<VBFixedString(7), MarshalAs(UnmanagedType.ByValArray, SizeConst:=7)>
Public equip_key As Char()
<VBFixedString(256), MarshalAs(UnmanagedType.ByValArray,
SizeConst:=256)> Public message As Char()
<VBFixedString(512), MarshalAs(UnmanagedType.ByValArray,
SizeConst:=512)> Public filler As Char()
End Structure


I reviewed the length of each element of the structure at runtime and
they all match the lengths in the structure.
I don't want to use ByValTStr because of the trailing null it adds.
I've tried Byte() instead of Char(). No difference.
I've removed VBFixedString(). No difference.

What does it want??

Tom
 
T

Tom Shelton

I'm getting this error when I try to pass a structure to a dll.

An unhandled exception of type 'System.ArgumentException' occured in
Test1.exe
Additional Information: Type could not be marshaled because the length
of an embedded array instance does not match the declared length in the
layout

What does it mean?


Here's the structure:

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Pack:=1)> _
Public Structure udtINTER01
<VBFixedString(8), MarshalAs(UnmanagedType.ByValArray, SizeConst:=8)>
Public action As Char()
<VBFixedString(7), MarshalAs(UnmanagedType.ByValArray, SizeConst:=7)>
Public equip_key As Char()
<VBFixedString(256), MarshalAs(UnmanagedType.ByValArray,
SizeConst:=256)> Public message As Char()
<VBFixedString(512), MarshalAs(UnmanagedType.ByValArray,
SizeConst:=512)> Public filler As Char()
End Structure


I reviewed the length of each element of the structure at runtime and
they all match the lengths in the structure.
I don't want to use ByValTStr because of the trailing null it adds.
I've tried Byte() instead of Char(). No difference.
I've removed VBFixedString(). No difference.

What does it want??

Tom

Tom, you may need to post some code that is actually using this
defintion. Are you sure that the Pack is neccesary? Can you give the C
definition of the structure and the function that it you're trying to
call. Something odd is going on.
 
T

Tom

Tom said:
Tom, you may need to post some code that is actually using this
defintion. Are you sure that the Pack is neccesary? Can you give the C
definition of the structure and the function that it you're trying to
call. Something odd is going on.
Tom

Thanks for your replies.

I don't have any C definitions for the dll I'm calling. Here's a
complete module that illustrates the problem. The udtInter01 structure
is the one in question. The commented out udtInter01 structure will not
produce the error but it exhibits the null terminator on fixed length
strings problem (not acceptable in our application). The project imports
System.Runtime.InteropServices.


Module Module1

Declare Function PutServer Lib "myclient32.dll" Alias "PutServer"
(ByRef Control1 As udtControl1, ByVal Fmt As String, ByRef inp As
udtInter01, ByVal InpLen As Integer) As Integer

Structure udtControl1
Dim Memory As Integer
Dim Task As Integer
Dim MessageCode As Integer
Dim ReceivedLength As Integer
Dim RecordCount As Integer
Dim ErrMessageLength As Short
<VBFixedString(320), MarshalAs(UnmanagedType.ByValTStr,
SizeConst:=320)> Public ErrMessage As String
<VBFixedString(80), MarshalAs(UnmanagedType.ByValTStr,
SizeConst:=80)> Public Reserved As String
End Structure

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Public Structure udtInter01
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=8)> Public
Action As Char()
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=7)> Public
Equip_key As Char()
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=256)> Public
Message As Char()
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=512)> Public
Filler As Char()
End Structure

'Public Structure udtInter01
' <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=8)> Public
Action As String
' <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=7)> Public
Equip_key As String
' <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=256)> Public
Message As String
' <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=512)> Public
Filler As String
'End Structure

Const INTER01_FMT As String = "%s8 %s7 %s256 %s512"
Const INTER01_LEN = 8 + 7 + 256 + 512

Public Sub Main()

Dim Inter01 As udtInter01
Dim Control1 As udtControl1

Inter01.Action = "Get1Equp"
Inter01.Equip_key = "A12345 "
Inter01.Message = Space(512)
Inter01.Filler = Space(256)

PutServer(Control1, INTER01_FMT, Inter01, INTER01_LEN)

End Sub

End Module
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top