473,407 Members | 2,312 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,407 software developers and data experts.

How to: Add a fixed length string member in a struct

Hi folks! :O)

I found this posted by Matthias on february 2001 :
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
string blabla;

or this posted by Matthias on december 2003 :
[MarshalAs(UnmanagedType.ByValArray, SizeConst=123)]
public char[] chars;
in both scenario the OP needed to work with APIs. What I want to do is to
dump/read data from a customly formated file.

in VB 6, we could declare something like the following :
'***
Type MyType
Code As String * 3
Name As String * 25
End Type
'***
and then write/read the given Type directly into a file.
Would this be possible in C# using one of the above way (and a BinaryWriter
i guess) ?

--
Best Regards
Yanick Lefebvre
Nov 16 '05 #1
4 2781
Zoury,

The binary writer won't do much for you here, since it won't know how to
lay out the strings. You could write it directly to a file yourself through
the API, taking advantage of the marshaling infrastructure, but to be
honest, it is a bit overkill for something like this.

You are better of creating the structure, and having a specific class
what will format the output correctly to the file, and read the items from
the file (or rather, make it a stream, or a TextReader that you read from).

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Zoury" <ya*************@hotmail.com> wrote in message
news:uI*************@TK2MSFTNGP10.phx.gbl...
Hi folks! :O)

I found this posted by Matthias on february 2001 :
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
string blabla;

or this posted by Matthias on december 2003 :
[MarshalAs(UnmanagedType.ByValArray, SizeConst=123)]
public char[] chars;
in both scenario the OP needed to work with APIs. What I want to do is to
dump/read data from a customly formated file.

in VB 6, we could declare something like the following :
'***
Type MyType
Code As String * 3
Name As String * 25
End Type
'***
and then write/read the given Type directly into a file.
Would this be possible in C# using one of the above way (and a
BinaryWriter
i guess) ?

--
Best Regards
Yanick Lefebvre

Nov 16 '05 #2
Hey Nicholas!
The binary writer won't do much for you
here, since it won't know how to lay out the strings.
I thought that LayoutKind.Sequential would have worked perfectly for that..
:O/

You are better of creating the structure, and having a specific class
what will format the output correctly to the file, and read the items from
the file (or rather, make it a stream, or a TextReader that you read

from).

Ok then, I'll give it a try.. I was a bit concerned about the execution
speed of such an approach. I'll test it against the APIs way for fun.

Thanks you very much for your help as always.

--
Best Regards
Yanick Lefebvre
Nov 16 '05 #3
Zoury,

The LayoutKind.Sequential is used to indicate how the CLR should map the
structure to unmanaged memory when marshaling that structure to unmanaged
memory.

You aren't doing anything like that, so the attribute is unecessary (and
it will be the default behavior anyways if you don't specify otherwise).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Zoury" <ya*************@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hey Nicholas!
The binary writer won't do much for you
here, since it won't know how to lay out the strings.


I thought that LayoutKind.Sequential would have worked perfectly for
that..
:O/

You are better of creating the structure, and having a specific class
what will format the output correctly to the file, and read the items
from
the file (or rather, make it a stream, or a TextReader that you read

from).

Ok then, I'll give it a try.. I was a bit concerned about the execution
speed of such an approach. I'll test it against the APIs way for fun.

Thanks you very much for your help as always.

--
Best Regards
Yanick Lefebvre

Nov 16 '05 #4
> The LayoutKind.Sequential is used to indicate how the CLR should map
the
structure to unmanaged memory when marshaling that structure to unmanaged
memory.


OK! I thought it meant that the CLR would keep the structure sequentially in
managed memory. :O/

thanks for the precision.

--
Best Regards
Yanick Lefebvre
Nov 16 '05 #5

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.,...
10
by: Adam Warner | last post by:
Hi all, With this structure that records the length of an array of pointers as its first member: struct array { ptrdiff_t length; void *ptr; };
1
by: Brian Reed | last post by:
How would I go about creating a struct that has a member with a fixed length character array? I am trying to call a DLL that requires a struct to be passed in that contains multiple fixed length...
3
by: Dr. Len | last post by:
Hi NG, is it possible for struct to have a fixed-length array of primitive types as member variable as in C++, such that the array doesn't need to be allocated explicitly? Eg.: --------...
1
by: Tajmiester | last post by:
Hi, And thanks for any help. I am having trouble declaring a struct containing strings that can be Serialized using the following functions. It works, but the strings wont store the right number...
2
by: AlanL | last post by:
I am calling a Fortran DLL that has a declaration like: character * 260 variablename. I do not have the Fortran code. A path to a file is passed to the DLL. C# does not have a fixed character...
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?...
4
by: taskswap | last post by:
I'm converting an application that relies heavily on a binary network protocol. Within this protocol are a lot of byte arrays of character data, like: public unsafe struct MsgAddEntry {...
0
by: Ken Varn | last post by:
I have a managed C++ assembly in which I need to interact with some 'C' APIs that take fixed size 'C' data blocks. I need to wrap these data blocks into a managed object. It seems like a lot of...
2
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
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,...
0
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...

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.