Search code examples
c#pinvoke

Equivalent to the structure in C#


What is a C#-equivalent to the following structure from C:

struct  netbuf{
unsigned int    maxlen;
unsigned int    len;
char            *buf;
};

I have translated it as:

public struct netbuf
{
    public uint maxlen;
    public uint len;
    public string buf;
};

but it seems to not be correct.

I have a legacy c code:

datagramm.addr.maxlen   = 0;
datagramm.addr.len      = 0;
datagramm.addr.buf      = (char*) 0;
datagramm.opt.maxlen    = 0;
datagramm.opt.len       = 0;
datagramm.opt.buf       = (char*) 0;

datagramm.udata.len   = sizeof(xliconf);
datagramm.udata.buf   = (char*)&xliconf;

xliconf.ccb_h.source = (uint8)ctrl_ed;
rval = xli_sndudata(ctrl_ed,&datagramm);

where declaration of xli_sndudata from header:

int xli_sndudata( int , struct t_unitdata *);

and

struct t_unitdata{
    struct netbuf   addr;     
    struct netbuf   opt;  
    struct netbuf   udata;   
};

Struct netbuf is above. I need to translate that code in C#.


Solution

  • [StructLayout(LayoutKind.Sequential, Pack = 0)]
    public struct netbuf
    {
        public ushort maxlen;
        public ushort len;
        public IntPtr buf;
    };