473,657 Members | 2,594 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Marshaling Fixed Length Strings in Structure

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

<StructLayout(L ayoutKind.Seque ntial, 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:=256) > Public message As Char()
<VBFixedString( 512), MarshalAs(Unman agedType.ByValA rray,
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.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

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 4212
In article <OH************ *@TK2MSFTNGP11. phx.gbl>, Tom wrote:
I'm trying to pass this structure to a dll:

<StructLayout( LayoutKind.Sequ ential, 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:=256) > Public message As Char()
<VBFixedString( 512), MarshalAs(Unman agedType.ByValA rray,
SizeConst:=512) > Public filler As Char()
End Structure


I'm not sure if VBFixedString actually works for Interop. Try changing
it to:

<StructLayout(L ayoutKind.Seque ntial, CharSet:=CharSe t.Ansi, Pack:=1)> _
Public Structure udtINTER01
<MarshalAs(Unma nagedType.ByVal TStr, 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.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'm not sure if VBFixedString actually works for Interop. Try changing
it to:

<StructLayout(L ayoutKind.Seque ntial, CharSet:=CharSe t.Ansi, Pack:=1)> _
Public Structure udtINTER01
<MarshalAs(Unma nagedType.ByVal TStr, 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.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 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.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 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(L ayoutKind.Seque ntial, CharSet:=CharSe t.Ansi, Pack:=1)> _
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

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

--
Tom Shelton [MVP]
Nov 20 '05 #6
Tom
Didn't help. I used

System.Text.ASC IIEncoding.ASCI I.GetBytes("Get 1Equp")

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

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

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.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 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(L ayoutKind.Seque ntial, CharSet:=CharSe t.Ansi, Pack:=1)> _
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

If for some reason that doesn't work, then you may want to change the type
to byte and then use the System.Text.Enc oding 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
7996
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., etc. The intent is to finish by assigning the list to a string that I would write to disk: recstr = string.join(recd,'')
3
2660
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 anything helpfull in internet for 4 months ;-( . Please someone who is more expirenced help me. I am trying to use the Terminal Server APIs There is as an API function WTSEnumerateSessions, which returns a WTS_SESSION_INFO custom structure n times....
3
1497
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? I've got a more basic question. The reason I'm doing all this is so I can pass a string buffer to an unmanaged method in a dll. It's assuming 1 byte/char. Does that mean that my fixed length should be half the size the buffer is supposed...
3
2332
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 the Win32 API. I figured how to use strings... No problem there, there was a couple undred examples on the net. I have yet to find how to do the same with an array of integers for example... Here's my test code :
4
23813
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 specifiers" error. Thank you, Scott
1
1057
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 ignore that if there's a better idea. I simply would like to create a technique to parse the bar code that will be easily portable. I could keep a set of constants that would contain the start position and length of each field within the bar...
6
4243
by: Eric | last post by:
.... my eternal gratitude!!! :p Here is the problem. A sample of my original VB6 code : '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public Type INFO name(1 To 3) As String * 41 address As String * 41 End Typ ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
1
9413
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 form data in a fixed length structure that is written direct to disk. I need to load the existing files, into a fixed length structure to initialize the form data within the project. I am running into problems with statements like the...
0
946
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 { public System.UInt16 ln; public int in; public System.UInt32 tn;
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8730
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7321
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1607
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.