[Mono-list] Unterminated string

Neale.Ferguson@SoftwareAG-USA.com Neale.Ferguson@SoftwareAG-USA.com
Fri, 22 Oct 2004 15:59:08 -0400


I am trying to write a .Net interface to a system of ours. The system =
takes the following structure:

struct XXXid {
	short level;
	short size;
	char	node[8];
	char  user[8];
	int	no;
}

Both user & node a 8 byte character strings padded with blanks and not =
zero terminated. I wanted to code the equivalent as a C# structure. =
Initially I came up with:

private struct XXXId {
       [FieldOffset (0)]  public  short  level;
       [FieldOffset (2)]  public  short  size;
       [MarshalAs(UnmanagedType.ByValTStr, SizeConst=3D8)]
       [FieldOffset (4)]  public  string node;
       [MarshalAs(UnmanagedType.ByValArray, SizeConst=3D8)]
       [FieldOffset (12)] public  string user;
       [FieldOffset (20)] public  int    pid;
}

The code that would use this would go something like:

static public int XXXInit (string user)
{
       XXXId sessInit =3D new XXXId();

       sessInit.level =3D 2;
       sessInit.size  =3D (short) =
System.Runtime.InteropServices.Marshal.SizeOf(sessInit);
       sessInit.node  =3D sessInit.user  =3D =
Encoding.ASCII.GetBytes(user);
       return (lnk_set_id(sessInit));
}

Now this almost works except that the character strings are zero =
terminated (which is what you'd expect from using ByValTStr I guess). I =
am at a loss as to how I should achieve what I'm after. I thought about =
using ByValArray and defining the fields as byte[] and then using=20

sessInit.user  =3D Encoding.ASCII.GetBytes(user);

But that leaves me with addresses in those fields in the structure.

Any suggestions for this novice?

Neale