[Mono-list] marshal a struct

Norbert Lazzeri elnogge at gmx.de
Thu Oct 2 15:54:51 EDT 2008


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:

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;
    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("<< " + new String(str.data) + " >>");
        Console.Out.WriteLine(+ res);
now this really seems to work, the only problem is the output of 

executing the program within monodevelop gives me:

<< 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:

<< das ist ein text >>

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..


More information about the Mono-list mailing list