[MonoDevelop] [Mono-list] marshal a struct
Chris Howie
cdhowie at gmail.com
Thu Oct 2 22:27:15 EDT 2008
On Thu, Oct 2, 2008 at 3:54 PM, Norbert Lazzeri <elnogge at gmx.de> wrote:
> im playing around a bit with c# and dllimports.
>
> my unmanaged function:
> /****************************************/
> struct blub {
> int size;
> char data[100];
> bool flag;
> };
> /****************************************/
> c# - calling code
> /****************************************/
> [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
> public struct INFO
> {
> public int size;
> [MarshalAs(UnmanagedType.ByValArray,SizeConst=100)]
> public char [] data;
> public bool flag;
> }
>
> class MainClass
> {
>
> [DllImport ("libmitcode", CharSet = CharSet.Ansi)]
> private static extern int getStruct(int size, ref INFO str);
>
> public static void Main(string[] args)
> {
> INFO str = new INFO();
> int res = getStruct(Marshal.SizeOf(str), ref str);
>
> Console.Out.WriteLine(str.size);
> Console.Out.WriteLine(str.flag);
> Console.Out.WriteLine("<< " + new String(str.data) + " >>");
> Console.Out.WriteLine(+ res);
>
> }
> }
> /****************************************/
A couple of issues:
1. The C type char and C# type char have different sizes. The
marshaller is probably going to truncate going from C# to C, and
coming back it should be filling the array with invalid (that is, not
unicode) data. Either store a byte[] on the C# class and use
System.Text.Encoding.ASCII to convert, or use a string, or something.
Somehow the String constructor or the marshaller is figuring this out
by itself but I would not rely on this behavior, unless you can find
where it is documented.
2. The String(char[]) constructor may not be smart enough to
understand that a null ASCII character means end-of-string. The
terminal may be OK ignoring a null character, but the MD output pane
may not, and that's why you see it truncated there. (This is still
arguably an MD bug, but you are correct that there are issues with
your code as well.)
--
Chris Howie
http://www.chrishowie.com
http://en.wikipedia.org/wiki/User:Crazycomputers
More information about the Monodevelop-list
mailing list