Initializing a struct with spaces

  • Thread starter Thread starter Eric Johannsen
  • Start date Start date
E

Eric Johannsen

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
 
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.
 
you can use new string('\0', 255) but why do you need to marshal to vb6, won't interop do it for you?
 
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
 
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
 
Eric Johannsen said:
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.
 
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
 
Eric Johannsen said:
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.
 
Back
Top