.net - Attempted to read or write protected memory -


i having problem mapi class below.(original source http://www.codeproject.com/kb/ip/sendfiletonet.aspx)

when users try send emails using sendmailpopup-method email program opens correctly after mail window closes program crashes message: attempted read or write protected memory. indication other memory corrupt.

i suspect that bug caused cleanup method , managed reduce frequency of crashes using int64 store pointers instead of int used in original version. .net unsafe programming knowlege rather limited can me find out causing rest of crashes

update: apparently cleanup method not causing exceptions because program crashes when commented out, possible cause mapi32.ddl sendmail method. wrong pointers passed parameters.

users using systems 64-bit processors , 32-bit winxp.

update:

the solutions provided in thread did lower frequency of crashes did not solve problem entirely. solution worked 100 % write small console application using c++ made mapi calls. our .net application interfaced mapi firing console application , passing data using arguments.

 public class mapi     {         public bool addrecipientto(string email)         {            return addrecipient(email, howto.mapi_to);         }          public bool addrecipientcc(string email)         {            return addrecipient(email, howto.mapi_cc);         }          public bool addrecipientbcc(string email)         {           return addrecipient(email, howto.mapi_bcc);         }          public void addattachment(string strattachmentfilename)         {             m_attachments.add(strattachmentfilename);         }          public int sendmailpopup(string strsubject, string strbody)         {             return sendmail(strsubject, strbody, mapi_logon_ui | mapi_dialog);         }          public int sendmaildirect(string strsubject, string strbody)         {             return sendmail(strsubject, strbody, mapi_logon_ui);         }           [dllimport("mapi32.dll")]         static extern int mapisendmail(intptr sess, intptr hwnd, mapimessage message, int flg, int rsv);          int sendmail(string strsubject, string strbody, int how)         {             mapimessage msg = new mapimessage();             msg.subject = strsubject;             msg.notetext = strbody;              msg.recips = getrecipients(out msg.recipcount);             msg.files = getattachments(out msg.filecount);              m_lasterror = mapisendmail(new intptr(0l), new intptr(0l), msg, how, 0);             if (m_lasterror > 1)                 messagebox.show("mapisendmail failed! " + getlasterror(), "mapisendmail");              cleanup(ref msg);             return m_lasterror;         }          bool addrecipient(string email, howto howto)         {            if (!string.isnullorempty(email))             {             mapirecipdesc recipient = new mapirecipdesc();             recipient.recipclass = (int)howto;             recipient.name = email;             m_recipients.add(recipient);             return true;             }             else             {             return false;             }         }          intptr getrecipients(out int recipcount)         {             recipcount = 0;             if (m_recipients.count == 0)                 return intptr.zero;              int size = marshal.sizeof(typeof(mapirecipdesc));             intptr intptr = marshal.allochglobal(m_recipients.count * size);              int ptr = (int)intptr;             foreach (mapirecipdesc mapidesc in m_recipients)             {                 marshal.structuretoptr(mapidesc, (intptr)ptr, false);                 ptr += size;             }              recipcount = m_recipients.count;             return intptr;         }          intptr getattachments(out int filecount)         {             filecount = 0;             if (m_attachments == null)                 return intptr.zero;              if ((m_attachments.count <= 0) || (m_attachments.count > maxattachments))                 return intptr.zero;              int size = marshal.sizeof(typeof(mapifiledesc));             intptr intptr = marshal.allochglobal(m_attachments.count * size);              mapifiledesc mapifiledesc = new mapifiledesc();             mapifiledesc.position = -1;             int ptr = (int)intptr;              foreach (string strattachment in m_attachments)             {                 mapifiledesc.name = path.getfilename(strattachment);                 mapifiledesc.path = strattachment;                 marshal.structuretoptr(mapifiledesc, (intptr)ptr, false);                 ptr += size;             }              filecount = m_attachments.count;             return intptr;         }          void cleanup(ref mapimessage msg)         {             try             {                 int size = marshal.sizeof(typeof(mapirecipdesc));                 int64 ptr = 0;                  if (msg.recips != intptr.zero)                 {                     ptr = msg.recips.toint64();                     (int = 0; < msg.recipcount; i++)                     {                         marshal.destroystructure((intptr)ptr, typeof(mapirecipdesc));                         ptr += size;                     }                     marshal.freehglobal(msg.recips);                 }                  if (msg.files != intptr.zero)                 {                     size = marshal.sizeof(typeof(mapifiledesc));                      ptr = msg.files.toint64();                     (int = 0; < msg.filecount; i++)                     {                         marshal.destroystructure((intptr)ptr, typeof(mapifiledesc));                         ptr += size;                     }                     marshal.freehglobal(msg.files);                 }                 m_recipients.clear();                 m_attachments.clear();             }             catch (exception e)             {                 smtpsender errorsender = new smtpsender();                 errorsender.sendautomaticerror(e.stacktrace + e.message, "virhe mapi sähköpostin lähetyksessä" + mysession.projectname + " käyttäjä:" + mysession.loginname);             }           }          public string getlasterror()         {             if (m_lasterror <= 26)                 return errors[m_lasterror];             return "mapi error [" + m_lasterror.tostring() + "]";         }          readonly string[] errors = new string[] {         "ok [0]", "user abort [1]", "yleinen virhe sähköpostin lähettämisessä [2]", "mapi login failure [3]",         "disk full [4]", "insufficient memory [5]", "access denied [6]", "-unknown- [7]",         "too many sessions [8]", "too many files specified [9]", "too many recipients specified [10]", "a specified attachment not found [11]",         "attachment open failure [12]", "attachment write failure [13]", "unknown recipient [14]", "bad recipient type [15]",         "no messages [16]", "invalid message [17]", "text large [18]", "invalid session [19]",         "type not supported [20]", "a recipient specified ambiguously [21]", "message in use [22]", "network failure [23]",         "invalid edit fields [24]", "asiakkaalle ei ole määritetty sähköpostiosoitetta.", "not supported [26]"          };           list<mapirecipdesc> m_recipients = new list<mapirecipdesc>();         list<string> m_attachments = new list<string>();         int m_lasterror = 0;          const int mapi_logon_ui = 0x00000001;         const int mapi_dialog = 0x00000008;         const int maxattachments = 20;          enum howto { mapi_orig = 0, mapi_to, mapi_cc, mapi_bcc };     }      [structlayout(layoutkind.sequential, charset = charset.ansi)]     public class mapimessage     {         public int reserved;         public string subject;         public string notetext;         public string messagetype;         public string datereceived;         public string conversationid;         public int flags;         public intptr originator;         public int recipcount;         public intptr recips;         public int filecount;         public intptr files;     }      [structlayout(layoutkind.sequential, charset = charset.ansi)]     public class mapifiledesc     {         public int reserved;         public int flags;         public int position;         public string path;         public string name;         public intptr type;     }      [structlayout(layoutkind.sequential, charset = charset.ansi)]     public class mapirecipdesc     {         public int reserved;         public int recipclass;         public string name;         public string address;         public int eidsize;         public intptr entryid;     } 

below stacktrace of exception. partly in finnish, can still see method names it.

kohteessa system.windows.forms.unsafenativemethods.dispatchmessagea(msg& msg) kohteessa system.windows.forms.application.componentmanager.system.windows.forms.unsafenativemethods.imsocomponentmanager.fpushmessageloop(int32 dwcomponentid, int32 reason, int32 pvloopdata) kohteessa system.windows.forms.application.threadcontext.runmessageloopinner(int32 reason, applicationcontext context) kohteessa system.windows.forms.application.threadcontext.runmessageloop(int32 reason, applicationcontext context) kohteessa system.windows.forms.application.run(form mainform)

you didn't use int64 consistantly (getrecipients , getattachments well). suspect therin lies problem, didn't go on in detail. below changes required. notice used different method of incrementing intptr little less error prone.

altered use joerg's suggestion increase buffer size of file attachment path.

public class mapi {     public bool addrecipientto(string email)     {         return addrecipient(email, howto.mapi_to);     }      public bool addrecipientcc(string email)     {         return addrecipient(email, howto.mapi_cc);     }      public bool addrecipientbcc(string email)     {         return addrecipient(email, howto.mapi_bcc);     }      public void addattachment(string strattachmentfilename)     {         m_attachments.add(strattachmentfilename);     }      public int sendmailpopup(string strsubject, string strbody)     {         return sendmail(strsubject, strbody, mapi_logon_ui | mapi_dialog);     }      public int sendmaildirect(string strsubject, string strbody)     {         return sendmail(strsubject, strbody, mapi_logon_ui);     }      int sendmail(string strsubject, string strbody, int how)     {         mapimessage msg = new mapimessage();         msg.subject = strsubject;         msg.notetext = strbody;          msg.recips = getrecipients(out msg.recipcount);         msg.files = getattachments(out msg.filecount);          m_lasterror = mapisendmail(new intptr(0l), new intptr(0l), msg, how, 0);         if (m_lasterror > 1)             messagebox.show("mapisendmail failed! " + getlasterror(), "mapisendmail");          cleanup(ref msg);         return m_lasterror;     }      bool addrecipient(string email, howto howto)     {         if (!string.isnullorempty(email))         {             mapirecipdesc recipient = new mapirecipdesc();             recipient.recipclass = (int)howto;             recipient.name = email;             m_recipients.add(recipient);             return true;         }         else         {             return false;         }     }      intptr getrecipients(out int recipcount)     {         recipcount = 0;         if (m_recipients.count == 0)             return intptr.zero;          int size = marshal.sizeof(typeof(mapirecipdesc));         intptr blockptr = marshal.allochglobal(m_recipients.count * size);         intptr currentptr = blockptr;          foreach (mapirecipdesc mapidesc in m_recipients)         {             marshal.structuretoptr(mapidesc, currentptr, false);             currentptr = (intptr)((long)currentptr + size);         }          recipcount = m_recipients.count;         return blockptr;     }      intptr getattachments(out int filecount)     {         filecount = 0;         if (m_attachments == null)             return intptr.zero;          if ((m_attachments.count <= 0) || (m_attachments.count > maxattachments))             return intptr.zero;          int size = marshal.sizeof(typeof(mapifiledesc));         intptr blockptr = marshal.allochglobal(m_attachments.count * size);         intptr currentptr = blockptr;          mapifiledesc mapifiledesc = new mapifiledesc();         mapifiledesc.position = -1;          foreach (string strattachment in m_attachments)         {             mapifiledesc.name = path.getfilename(strattachment);             mapifiledesc.path = marshal.allochglobal(max_path);             copystringansi(mapifiledesc.path, strattachment);             marshal.structuretoptr(mapifiledesc, currentptr, false);             currentptr = (intptr)((long)currentptr + size);         }          filecount = m_attachments.count;         return blockptr;     }      void cleanup(ref mapimessage msg)     {         try         {             if (msg.recips != intptr.zero)             {                 intptr currentptr = msg.recips;                 int size = marshal.sizeof(typeof(mapirecipdesc));                  (int = 0; < msg.recipcount; i++)                 {                     marshal.destroystructure(currentptr, typeof(mapirecipdesc));                     currentptr = (intptr)((long)currentptr + size);                 }                 marshal.freehglobal(msg.recips);             }              if (msg.files != intptr.zero)             {                 intptr currentptr = msg.files;                 int size = marshal.sizeof(typeof(mapifiledesc));                  (int = 0; < msg.filecount; i++)                 {                     marshal.destroystructure(currentptr, typeof(mapifiledesc));                     currentptr = (intptr)((long)currentptr + size);                 }                 marshal.freehglobal(msg.files);             }             m_recipients.clear();             m_attachments.clear();         }         catch (exception e)         {             smtpsender errorsender = new smtpsender();             errorsender.sendautomaticerror(e.stacktrace + e.message, "virhe mapi sähköpostin lähetyksessä" + mysession.projectname + " käyttäjä:" + mysession.loginname);         }     }      public string getlasterror()     {         if (m_lasterror <= 26)             return errors[m_lasterror];         return "mapi error [" + m_lasterror.tostring() + "]";     }      readonly string[] errors = new string[] {           "ok [0]", "user abort [1]", "yleinen virhe sähköpostin lähettämisessä [2]", "mapi login failure [3]",           "disk full [4]", "insufficient memory [5]", "access denied [6]", "-unknown- [7]",           "too many sessions [8]", "too many files specified [9]", "too many recipients specified [10]", "a specified attachment not found [11]",           "attachment open failure [12]", "attachment write failure [13]", "unknown recipient [14]", "bad recipient type [15]",           "no messages [16]", "invalid message [17]", "text large [18]", "invalid session [19]",           "type not supported [20]", "a recipient specified ambiguously [21]", "message in use [22]", "network failure [23]",           "invalid edit fields [24]", "asiakkaalle ei ole määritetty sähköpostiosoitetta.", "not supported [26]"            };       list<mapirecipdesc> m_recipients = new list<mapirecipdesc>();     list<string> m_attachments = new list<string>();     int m_lasterror = 0;      const int mapi_logon_ui = 0x00000001;     const int mapi_dialog = 0x00000008;     const int maxattachments = 20;      const int max_path = 256;      enum howto { mapi_orig = 0, mapi_to, mapi_cc, mapi_bcc };      [dllimport("mapi32.dll")]     static extern int mapisendmail(intptr sess, intptr hwnd, mapimessage message, int flg, int rsv);      [dllimport("kernel32.dll", entrypoint = "rtlmovememory", charset = charset.ansi)]     static extern void rtlmovestringansi(intptr pdst, string psrc, intptr sizetcb);      private void copystringansi(intptr intptr, string str)     {         int length = (str.length + 1) * marshal.systemmaxdbcscharsize;         rtlmovestringansi(intptr, str, (intptr)length);     }      [structlayout(layoutkind.sequential, charset = charset.ansi)]     class mapimessage     {         public int reserved;         public string subject;         public string notetext;         public string messagetype;         public string datereceived;         public string conversationid;         public int flags;         public intptr originator;         public int recipcount;         public intptr recips;         public int filecount;         public intptr files;     }      [structlayout(layoutkind.sequential, charset = charset.ansi)]     class mapifiledesc     {         public int reserved;         public int flags;         public int position;         public intptr path;         public string name;         public intptr type;     }      [structlayout(layoutkind.sequential, charset = charset.ansi)]     class mapirecipdesc     {         public int reserved;         public int recipclass;         public string name;         public string address;         public int eidsize;         public intptr entryid;     } } 

Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -