Marshaling Fixed Length Strings in Structure

  • Thread starter Thread starter Tom
  • Start date Start date
T

Tom

I'm trying to pass this structure to a dll:

<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

When I pass it to my dll I get this message:

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

I reviewed the length of each element of the structure and they all
match the declared lengths.

What does it want??

Tom
 
I'm trying to pass this structure to a dll:

<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'm not sure if VBFixedString actually works for Interop. Try changing
it to:

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Pack:=1)> _
Public Structure udtINTER01
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=8)>
Public Action As String
...
End Structure

Also, make sure that you set the charset expected by the function call
to be Ansi as well.
 
I'm avoiding ByValTStr because it replaces the last byte of the field
with a null. If there were a way to stop Interop from putting the null
on the end that would be great.

Tom
 
I'm avoiding ByValTStr because it replaces the last byte of the field
with a null. If there were a way to stop Interop from putting the null
on the end that would be great.

Tom

Then use ByValArray instead if this is a reqirement. The SizeConst
thing still works the same.
 
That's where I started. When I do I get

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 want?

Tom
 
That's where I started. When I do I get

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 want?

Tom

Aaah, looking back I see that :) What I'm saying is remove the
VBFixedLength string stuff:

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Pack:=1)> _
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

If for some reason that doesn't work, then you may want to change the type
to byte and then use the System.Text.Encoding class to convert the members
to and from strings.
 
Didn't help. I used

System.Text.ASCIIEncoding.ASCII.GetBytes("Get1Equp")

to get the text into the byte array and I still get

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

I don't understand in what way embedded array does not match the
declared length in the layout.

Tom
 
Back
Top