none
Attempting to call into managed code without transitioning out first RRS feed

  • Question

  • Hi,

    I am writing an add-in for outlook 2003 in c#.

    code was running fine but today it started displaying the following message:

    "Attempting to call into managed code without transitioning out first.  Do not attempt to run managed code inside low-level native extensibility points, such as the vectored exception handler, since doing so can cause corruption and data loss."

    my code is written below:

    using System;
    using System.Windows.Forms;
    using Microsoft.VisualStudio.Tools.Applications.Runtime;
    using Microsoft.Win32;
    using System.Collections.Generic;
    using Outlook = Microsoft.Office.Interop.Outlook;
    
    namespace Crosselerator
    {
        public partial class ThisAddIn
        {
            AddInWebServices.PortalServciesSoapClient ws = new AddInWebServices.PortalServciesSoapClient();        
            Outlook.Inspectors inspectors;
            int _companyID = 0;
    
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                inspectors = this.Application.Inspectors;
                inspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);
    
                inspectors = this.Application.Inspectors;
                inspectors.Application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
            }
            void Inspectors_NewInspector(Outlook.Inspector Inspector)
            {
                ReadWriteReg _readWriteReg = new ReadWriteReg();
                _companyID = Convert.ToInt16(_readWriteReg.Read());
    
                if (_companyID == 0)
                {
                    ActivationForm _activationForm = new ActivationForm();
                    _activationForm.ShowDialog();
                }
            }
    
            void Application_ItemSend(object Item, ref bool Cancel)
            {
                try
                {
                    Outlook.Application application = Globals.ThisAddIn.Application;
                    Outlook.Inspector inspector = application.ActiveInspector();
                    Outlook.MailItem myMailItem = (Outlook.MailItem)inspector.CurrentItem;
                    Outlook.Recipients _recepientEmail = myMailItem.Recipients;
    
                    string _marketingMsg = string.Empty;
                    string _msgReceiver = myMailItem.To;
                    string _msgBoby = myMailItem.Body;
                    //string _temp = _msgReceiver.Trim('\'');
                    //_msgReceiver = _temp;
    
    
                    var _getMessages = ws.GetMessages(_companyID, _msgReceiver, DateTime.Now.ToString("yyyy-MM-dd"), DateTime.Now.ToString("yyyy-MM-dd"));
                    if (_getMessages.Count > 0)
                    {
                        foreach (Outlook.Recipient recepientEmail in _recepientEmail)
                        {
                            _msgReceiver = recepientEmail.Name;
    
                            for (int i = 0; i < _getMessages.Count; i++)
                            {
                                string _url = "http://SERVERIPADDRESS/EmailReadStatus/updateStatus.aspx?advID=" + _getMessages[i].AddID + "&emailID=" + recepientEmail.Name + "&companyID=" + Convert.ToInt32(_companyID);
                                _marketingMsg = _marketingMsg + "<br/><br/><img alt='' src=' " + _url + "'/><br/>" + _getMessages[i].Message;
    
                                var _result = ws.UpdateMessageStatus(Convert.ToInt16(_getMessages[i].AddID), _companyID, _msgReceiver);
                            }
                        }
                        myMailItem.To = _msgReceiver;
                        //myMailItem.Subject = "This text was added by using code";
                        myMailItem.HTMLBody = _msgBoby + _marketingMsg;
                        //MessageBox.Show("2 - Send item " + _marketingMsg);
                    }
                    else
                    {
                        myMailItem.To = _msgReceiver;
                        //myMailItem.Subject = "This text was added by using code";
                        myMailItem.HTMLBody = _msgBoby;
                        //MessageBox.Show("3 - Send item " + _msgBoby);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
            }
    
            private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
            {
            }
    
            #region VSTO generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisAddIn_Startup);
                this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
            }
            
            #endregion
        }
    }
    


    Regards, Shahzad

    Sunday, December 6, 2015 1:10 PM

All replies

  • >>>code was running fine but today it started displaying the following message:

    According to your description, could you figure out whether this issue happens randomly or frequently, which line code cause throwing this exception?

    Otherwise you could examine the stack trace for the thread that has activated this MDA. The thread is attempting to illegally call into managed code. The stack trace should reveal the application code using this extensibility point, the operating system code that provides this extensibility point, and the managed code that was interrupted by the extensibility point.

    For more information, click here to refer about reentrancy MDA

    Monday, December 7, 2015 6:50 AM