By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,569 Members | 1,615 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,569 IT Pros & Developers. It's quick & easy.

Marshaling Fixed Length Strings in Structure

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
In article <OH*************@TK2MSFTNGP11.phx.gbl>, Tom wrote:
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.

--
Tom Shelton [MVP]
Nov 20 '05 #2

P: n/a
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
Tom Shelton wrote:
In article <OH*************@TK2MSFTNGP11.phx.gbl>, Tom wrote:
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.

Nov 20 '05 #3

P: n/a
In article <#x*************@tk2msftngp13.phx.gbl>, Tom wrote:
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.

--
Tom Shelton [MVP]
Nov 20 '05 #4

P: n/a
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
Tom Shelton wrote:
In article <#x*************@tk2msftngp13.phx.gbl>, Tom wrote:
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.

Nov 20 '05 #5

P: n/a
On Wed, 28 Jul 2004 09:24:35 -0500, Tom wrote:
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
Tom Shelton wrote:
In article <#x*************@tk2msftngp13.phx.gbl>, Tom wrote:
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.


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.

--
Tom Shelton [MVP]
Nov 20 '05 #6

P: n/a
Tom
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
Tom Shelton wrote:
On Wed, 28 Jul 2004 09:24:35 -0500, Tom wrote:

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
Tom Shelton wrote:
In article <#x*************@tk2msftngp13.phx.gbl>, Tom wrote:
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.

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.

Nov 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.