473,396 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Marshaling Fixed Length Strings in Structure

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
6 4184
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
3
by: Nikolay Petrov | last post by:
Guys, please help. I am trying to make this work from at least 4 months. I am new to programming and some things are difficult to me, but I really need to make my project work. I can't find...
3
by: Ken Kast | last post by:
I'm using VBFixedString to create fixed length strings. But I find that the string will merrily allow a string of greater length to be assigned to it. Is this the way things are supposed to work?...
3
by: Sin | last post by:
Hello everybody, I'm currently trying to understand how marshaling can use used for accessing Win32 API functions as well as custom C/C++ code we design which exposes functions the same way as...
4
by: Scott Lemen | last post by:
Hi, Some Win APIs expect a structure with a fixed length string. How is it defined in VB .Net 2003? When I try to use the FixedLengthString class I get an "Array bounds cannot appear in type...
1
by: Jim Corey | last post by:
I have to parse a bar code. Back in VB6, I can remember putting a string into a structure that had members with fixed-length strings and then I could pick off the memebers by name. But we can...
6
by: Eric | last post by:
.... my eternal gratitude!!! :p Here is the problem. A sample of my original VB6 code : '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public...
1
by: Rick Knospler | last post by:
I am trying to convert a vb6 project to vb.net. The conversion worked for the most part except for the fixed length strings and fixed length string arrays. Bascially the vb6 programmer stored all...
0
by: kishor14 | last post by:
I need an example of how to marshal a structure that has an array of another structures as a member. An example as mentioned below public struct strcutdata { ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.