[Mono-list] marshal a struct

Geoff Norton gnorton at novell.com
Thu Oct 2 19:54:21 EDT 2008


On Fri, 2008-10-03 at 01:27 +0200, Norbert Lazzeri wrote:
> i would like to check what chars are inside the array but since 
> monodevelop currently doesnt support debugging, it could be a bit 
> difficult to produce a significant testcase.
> 

A significant test case is simple, provide the C#, .c and monodevelop
project files that exhibit the problem.

> i found out that monodevelop stops execution while trying to write the 
> next char. it simply stops without error or log message.
> 

These statements are contradictory.

> i dont believe filing a bug like this makes much sense, i wouldnt even 
> know how to name the issue :p
> 

How about "Monodevelop truncates output returned from a p/invoke
structure?"

-g

> i will continue investigating the problem, however any further 
> suggestions are very wellcome.
> 
> best regards,
> norbert
> 
> Geoff Norton schrieb:
> > Norbert,
> >
> >   I suggest you file a bug on http://bugzilla.novell.com/ and attach a
> > testcase which shows the problem.
> >
> > -g
> >
> > On Thu, 2008-10-02 at 21:54 +0200, Norbert Lazzeri wrote:
> >   
> >> hi,
> >>
> >> im playing around a bit with c# and dllimports.
> >>
> >> im working on debian sid with mono and monodevelop from the debian repos
> >> the shared-library is written in C++ using code::blocks compiled with g++
> >>
> >> specifically im trying to pass a pointer of my struct to the unmanaged 
> >> function and write some stuff into it. i used this article as help:
> >> http://www.vsj.co.uk/articles/display.asp?id=501 
> >> <http://dontknow.me/at/?http://www.vsj.co.uk/articles/display.asp%3Fid=501>
> >>
> >> my unmanaged function:
> >> /****************************************/
> >> struct blub {
> >>     int size;
> >>     char data[100];
> >>     bool flag;
> >> };
> >> ...
> >> extern "C" {
> >> int getStruct(int structSize, blub * str) {
> >>
> >>             if (structSize != sizeof(blub))
> >>                 return -1;
> >>            
> >>
> >>             str->size = 234;
> >>             strcpy(str->data, "das ist ein text");
> >>             str->flag = false;
> >>
> >>             return 0;
> >>     }
> >> };
> >> /****************************************/
> >> 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);
> >>        
> >>     }   
> >> }
> >> /****************************************/
> >> now this really seems to work, the only problem is the output of 
> >> monodevelop.
> >>
> >> executing the program within monodevelop gives me:
> >>
> >> "234
> >> False
> >> << das ist ein text"
> >>
> >> so the everything thats written to stdout after my string, is truncated.
> >>
> >> if i execute the program from CLI via "mono myprogram.exe" everything 
> >> works fine and i got:
> >>
> >> "234
> >> False
> >> << das ist ein text >>
> >> 0"
> >>
> >> i dont know whats causing the difference between the outputs, I also 
> >> tried to Console.out.Flush().
> >> i dont think thats a monodevelop-issue. so something must be wrong with 
> >> my code but i have no idea what..
> >>
> >> cheers,
> >> Norbert
> >> _______________________________________________
> >> Mono-list maillist  -  Mono-list at lists.ximian.com
> >> http://lists.ximian.com/mailman/listinfo/mono-list
> >>     
> >
> >   



More information about the Mono-list mailing list