Tom Shelton wrote:
In article <Od************ **@TK2MSFTNGP09 .phx.gbl>, Tom wrote:
I'm getting this error when I try to pass a structure to a dll.
An unhandled exception of type 'System.Argumen tException' 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.Seq uential, CharSet:=CharSe t.Ansi, Pack:=1)> _
Public Structure udtINTER01
<VBFixedString( 8), MarshalAs(Unman agedType.ByValA rray, SizeConst:=8)>
Public action As Char()
<VBFixedString( 7), MarshalAs(Unman agedType.ByValA rray, SizeConst:=7)>
Public equip_key As Char()
<VBFixedString( 256), MarshalAs(Unman agedType.ByValA rray,
SizeConst:=25 6)> Public message As Char()
<VBFixedString( 512), MarshalAs(Unman agedType.ByValA rray,
SizeConst:=51 2)> 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.
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 ErrMessageLengt h As Short
<VBFixedString( 320), MarshalAs(Unman agedType.ByValT Str,
SizeConst:=320) > Public ErrMessage As String
<VBFixedString( 80), MarshalAs(Unman agedType.ByValT Str,
SizeConst:=80)> Public Reserved As String
End Structure
<StructLayout(L ayoutKind.Seque ntial, CharSet:=CharSe t.Ansi)> _
Public Structure udtInter01
<MarshalAs(Unma nagedType.ByVal Array, SizeConst:=8)> Public
Action As Char()
<MarshalAs(Unma nagedType.ByVal Array, SizeConst:=7)> Public
Equip_key As Char()
<MarshalAs(Unma nagedType.ByVal Array, SizeConst:=256) > Public
Message As Char()
<MarshalAs(Unma nagedType.ByVal Array, SizeConst:=512) > Public
Filler As Char()
End Structure
'Public Structure udtInter01
' <MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=8)> Public
Action As String
' <MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=7)> Public
Equip_key As String
' <MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=256) > Public
Message As String
' <MarshalAs(Unma nagedType.ByVal TStr, 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_k ey = "A12345 "
Inter01.Message = Space(512)
Inter01.Filler = Space(256)
PutServer(Contr ol1, INTER01_FMT, Inter01, INTER01_LEN)
End Sub
End Module