473,378 Members | 1,119 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,378 software developers and data experts.

Initializing a struct with spaces

Hi,

My C# code is calling VB6 code, which expects all (fixed-length) strings to
be padded with spaces. The strings are contained with a struct, something
like this (attributes to simulate fixed-length in C# omitted for brevity):

struct MyStruct
{
int someValue;
string myString;
string anotherString;
}

When I create a new myStruct, it's memory space is initialized with binary
0's (per the C# spec). I would like to find a way to overwrite the
myStruct's entire memory space with spaces ' '.

Also, when I assign a particular string to a structure member like this:

myStruct.myString = "123";

the memory buffer for myString ends up getting null terminated. Is there a
work-around for that?

I have considered using char[] instead of string, but while working with
MyStruct I don't know how large the fixed-string size for any particular
structure member is.

Thanks,

Eric

Nov 15 '05 #1
7 1862


Why not simply write a utility function that takes a string and returns a
fully spaced string, and use that return value in your struct? Or you could
use properties that properly pad strings assigned to them.

"Eric Johannsen" <no*********@johannsen.us> wrote in message
news:g2******************@newssvr25.news.prodigy.c om...
Hi,

My C# code is calling VB6 code, which expects all (fixed-length) strings to be padded with spaces. The strings are contained with a struct, something
like this (attributes to simulate fixed-length in C# omitted for brevity):

struct MyStruct
{
int someValue;
string myString;
string anotherString;
}

When I create a new myStruct, it's memory space is initialized with binary
0's (per the C# spec). I would like to find a way to overwrite the
myStruct's entire memory space with spaces ' '.

Also, when I assign a particular string to a structure member like this:

myStruct.myString = "123";

the memory buffer for myString ends up getting null terminated. Is there a work-around for that?

I have considered using char[] instead of string, but while working with
MyStruct I don't know how large the fixed-string size for any particular
structure member is.

Thanks,

Eric

Nov 15 '05 #2
you can use new string('\0', 255) but why do you need to marshal to vb6, won't interop do it for you?

--
Michael Culley
"Eric Johannsen" <no*********@johannsen.us> wrote in message news:g2******************@newssvr25.news.prodigy.c om...
Hi,

My C# code is calling VB6 code, which expects all (fixed-length) strings to
be padded with spaces. The strings are contained with a struct, something
like this (attributes to simulate fixed-length in C# omitted for brevity):

struct MyStruct
{
int someValue;
string myString;
string anotherString;
}

When I create a new myStruct, it's memory space is initialized with binary
0's (per the C# spec). I would like to find a way to overwrite the
myStruct's entire memory space with spaces ' '.

Also, when I assign a particular string to a structure member like this:

myStruct.myString = "123";

the memory buffer for myString ends up getting null terminated. Is there a
work-around for that?

I have considered using char[] instead of string, but while working with
MyStruct I don't know how large the fixed-string size for any particular
structure member is.

Thanks,

Eric

Nov 15 '05 #3
> Why not simply write a utility function that takes a string and returns a
fully spaced string, and use that return value in your struct? Or you could use properties that properly pad strings assigned to them.


The trouble is, I don't know how long the individual string member of the
struct is at runtime. The code is going into a base class, and the derived
class could be using any one of about 20 structs to pass data to VB6. Each
struct has many strings (10 on average).

Eric

Nov 15 '05 #4
> you can use new string('\0', 255) but why do you need to marshal to vb6,
won't interop do it for you?

I don't know how long each string's storage allocation in the struct is at
runtime.

The VB6 code is designed to take a memory buffer that happens to be
formatted like the struct I'm passing in. That memory buffer expects the
values of the strings to be included at the appropriate place padded out
with spaces, and the VB6 code does not work properly if the padding is with
nulls instead.

Eric

Nov 15 '05 #5

"Eric Johannsen" <no*********@johannsen.us> wrote in message
news:Zl******************@newssvr25.news.prodigy.c om...
Why not simply write a utility function that takes a string and returns a fully spaced string, and use that return value in your struct? Or you could
use properties that properly pad strings assigned to them.


The trouble is, I don't know how long the individual string member of the
struct is at runtime. The code is going into a base class, and the

derived class could be using any one of about 20 structs to pass data to VB6. Each struct has many strings (10 on average).
Then you will have to rely on each derived class to perform the proper
string conversion, a static utility function will save you some time, but
that is still the responsibility of your derived classes. However, you could
scan the structs yourself using reflection and change the strings to the
proper size.
Have you tried using the MarshalAs attribute and setting the SizeConst
property? I don't know how that behaves with strings, but its worth looking
into. If it doesn't produce a space padded string, then you can scan the
structure yourself and pad the strings before passing it to the marshaller. Eric

Nov 15 '05 #6
I resolved the issue by:

- Using char [] instead of string in the struct's
- Adding an attribute to indicate how many chars the array is to take up
(unfortunately you don't seem to be able to get at MarshalAs, which also has
that information
- Ensuring that the char[] that is assigned to the struct has the exact same
length as the definition. If it's too short the interop call throws an
exception.

Thanks for all the input.

Eric

"Daniel O'Connell" <onyxkirx@--NOSPAM--comcast.net> wrote in message
news:#g**************@TK2MSFTNGP12.phx.gbl...

"Eric Johannsen" <no*********@johannsen.us> wrote in message
news:Zl******************@newssvr25.news.prodigy.c om...
Why not simply write a utility function that takes a string and
returns
a fully spaced string, and use that return value in your struct? Or you could
use properties that properly pad strings assigned to them.


The trouble is, I don't know how long the individual string member of the struct is at runtime. The code is going into a base class, and the

derived
class could be using any one of about 20 structs to pass data to VB6.

Each
struct has many strings (10 on average).

Then you will have to rely on each derived class to perform the proper
string conversion, a static utility function will save you some time, but
that is still the responsibility of your derived classes. However, you

could scan the structs yourself using reflection and change the strings to the
proper size.
Have you tried using the MarshalAs attribute and setting the SizeConst
property? I don't know how that behaves with strings, but its worth looking into. If it doesn't produce a space padded string, then you can scan the
structure yourself and pad the strings before passing it to the

marshaller.
Eric



Nov 15 '05 #7

"Eric Johannsen" <no*********@johannsen.us> wrote in message
news:yX******************@newssvr25.news.prodigy.c om...
I resolved the issue by:

- Using char [] instead of string in the struct's
- Adding an attribute to indicate how many chars the array is to take up
(unfortunately you don't seem to be able to get at MarshalAs, which also has that information
- Ensuring that the char[] that is assigned to the struct has the exact same length as the definition. If it's too short the interop call throws an
exception.
A string should have been possible, but if char[] did what you wanted, thats
good. Thanks for all the input.

Eric

"Daniel O'Connell" <onyxkirx@--NOSPAM--comcast.net> wrote in message
news:#g**************@TK2MSFTNGP12.phx.gbl...

"Eric Johannsen" <no*********@johannsen.us> wrote in message
news:Zl******************@newssvr25.news.prodigy.c om...
> Why not simply write a utility function that takes a string and returns
a
> fully spaced string, and use that return value in your struct? Or you could
> use properties that properly pad strings assigned to them.

The trouble is, I don't know how long the individual string member of the struct is at runtime. The code is going into a base class, and the

derived
class could be using any one of about 20 structs to pass data to VB6.

Each
struct has many strings (10 on average).

Then you will have to rely on each derived class to perform the proper
string conversion, a static utility function will save you some time, but that is still the responsibility of your derived classes. However, you

could
scan the structs yourself using reflection and change the strings to the
proper size.
Have you tried using the MarshalAs attribute and setting the SizeConst
property? I don't know how that behaves with strings, but its worth

looking
into. If it doesn't produce a space padded string, then you can scan the
structure yourself and pad the strings before passing it to the

marshaller. Eric


Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Nollie | last post by:
Say you have a struct: struct MYSTRUCT { int x; int y; int w; int h; };
3
by: Joe | last post by:
Hi, I have been struggling with this issue for a couple of days and would like to know if some can give me a pointer. I want to initialize a struct with default values and depending on the...
10
by: Bart Goeman | last post by:
Hi, I have a question about how to put redundant information in data structures, initialized at compile time. This is often necessary for performance reasons and can't be done at run time (data...
4
by: ccdrbrg | last post by:
I am trying to initialize an arrary of pointers to structs with constants. Sample code: struct mystruct { char *text; int number; };
7
by: Paminu | last post by:
In the following code I am trying to initialize a pointer that is located in a struct. #include <stdlib.h> #include <stdio.h> #define KIDS 4 typedef struct test { void *content;
5
by: ScottM | last post by:
I expect this has been covered somewhere, but I can't find it. Given: class A { public: A() {...does stuff...} }; struct S { A a; };
12
by: Mik0b0 | last post by:
Hallo. Let's say, there is a structure struct struct10{ int a1; int a2; int a3; int a4; }count={ {10,20,30,40}, {50,60,70,80}
4
by: benn686 | last post by:
I have a structure that contains a union that Id like to initialize at compile time... something like: //global declare and initialize fullStructType var1 = { unionMember.union1.field1...
13
by: WaterWalk | last post by:
Hello. When I consult the ISO C++ standard, I notice that in paragraph 3.6.2.1, the standard states: "Objects with static storage duration shall be zero-initialized before any other...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.