How to convert a C++ Struct with Union into C#? -


guys having difficulties on retrieving struct member values after calling function in dll. tried convert c++ codes c# i’m not sure if correct or not. please me understand mistakes here (if there is) , how correct.

my problem here can’t correctly retrieved values of inner structs (union) after called receivemessage function dll. example m_objmsg.msgdata.startreq.msgid 0. when try use c++ .exe program, msgid has correct value. (not 0)

c++ code:

extern int receivemessage(session, int, msg*);    typedef struct   {     char subsid[15];     int level;     char options[12];   } conxreq;    typedef struct {   char msgid[25]; } startreq;   typedef struct   {     long length;     short type;     union     {       conxreq oconxreq;       startreq ostartreq;     } data;    } msg;     ///////////////////////////////////////////////////// msg omsg; int rc=receivemessage(session, 0, &omsg);  switch(rc) {   case 0:      switch(omsg.type)      {        case 0: // conxreq          …          break;        case 1: // startreq          …          break;    …   } 

and here attempt convert c#:

[dllimport("mydll.dll",   callingconvention = callingconvention.cdecl,   charset = charset.ansi)]   protected static extern int32 receivemessage(intptr session,   int32 ntimeout,   [marshalas(unmanagedtype.struct)] ref msg ptrmsg);     [structlayout(layoutkind.sequential, charset = charset.ansi)]   public struct conxreq   {                  [marshalas(unmanagedtype.byvaltstr, sizeconst = 15)]      public string subsid;       public int32 level;       [marshalas(unmanagedtype.byvaltstr, sizeconst = 12)]      public string options;   }    [structlayout(layoutkind.sequential, charset = charset.ansi)]           public struct startreq   {                  [marshalas(unmanagedtype.byvaltstr, sizeconst = 25)]      public string msgid;   }     [structlayout(layoutkind.sequential, charset = charset.ansi)]   protected struct msg   {     public int length;     public int16 type;     public data msgdata;   }    structlayout(layoutkind.explicit, charset = charset.ansi)]   public struct data   {     [fieldoffset(0)]     public conxreq oconxreq;      [fieldoffset(0)]     public startreq ostartreq;   }     msg m_objmsg = new msg();   m_objmsg.msgdata = new data();   m_objmsg.msgdata.oconxreq = new conxreq();   m_objmsg.msgdata.ostartreq = new startreq();    int rc = receivemessage(m_session, ntimeout, ref m_objmsg);     switch condition 

and if add struct inside union c++ , c#... i've got error stating "... incorrectly align" or "...overlapped..."

c++

conxnack oconxnack;  typedef struct   {     int reason;  } conxnack;   [structlayout(layoutkind.sequential)]         public struct conxnack {                 public int nreason; }  [fieldoffset(0)] public conxnack oconxnack; 

thank in advance time , help...

akash right, have here: http://social.msdn.microsoft.com/forums/en/csharplanguage/thread/60150e7b-665a-49a2-8e2e-2097986142f3

another option create 2 structs , use appropriate cast once know type is.

hth

mario


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -