none
Outlook VSTO Error : 'Attempted to read or write protected memory'

    Question

  • Hello everyone. I am seeing an issue I've never seen before. Hopefully someone can help. I am using Outlook 2007 and have developed my add-in in VS2008. The exact error is :

    "2/22/2013 1:17:14 PM - MYREMINDERS_BEFOREREMINDERSHOW ERROR: EXCEPTION TYPE
    ------------------------------------------------------------------------------------------------------------------------------------------------------
    SYSTEM.ACCESSVIOLATIONEXCEPTION

    MESSAGE
    ------------------------------------------------------------------------------------------------------------------------------------------------------
    ATTEMPTED TO READ OR WRITE PROTECTED MEMORY. THIS IS OFTEN AN INDICATION THAT OTHER MEMORY IS CORRUPT.

    STACK TRACE
    ------------------------------------------------------------------------------------------------------------------------------------------------------
       AT VARIANTCLEAR(TAGVARIANT* PVARG)
       AT SYSTEM.RUNTIME.INTEROPSERVICES.CUSTOMMARSHALERS.ENUMERATORVIEWOFENUMVARIANT.MOVENEXT()
       AT OUTLOOKADDIN2.THISADDIN.MYREMINDERS_BEFOREREMINDERSHOW(BOOLEAN& CANCEL)


    2/22/2013 1:17:14 PM - MYREMINDERS_BEFOREREMINDERSHOW FINISH."

    The sub TestOutOfProcess() works on Application_Startup(), but throws the above error in myReminders_BeforeReminderShow(). I don't get it.

    Private WithEvents myReminders As Outlook.Reminders
    
    'Works in here
    Private Sub Application_Startup() Handles Application.Startup
            TestOutOfProcess()
    End Sub
    
    'Does not work in here
    Private Sub myReminders_BeforeReminderShow(ByRef Cancel As Boolean) Handles myReminders.BeforeReminderShow
            AppendLog("myReminders_BeforeReminderShow Begin.", Const_LogToScreen, Const_LogToFile)
    
            Try
                TestOutOfProcess()
            Catch ex As Exception
                AppendLog("myReminders_BeforeReminderShow Error: " & MyErrorMessage(ex).ToString, Const_LogToScreen, Const_LogToFile)
            Finally
                ' cancel the reminder
                Cancel = True
            End Try
    End Sub
    
    Public Sub TestOutOfProcess()
            Dim objReminder As Outlook.Reminder
            Dim objReminders As Outlook.Reminders
    
            objReminders = Application.Reminders
    
            For Each objReminder In objReminders
                If objReminder.Caption = taskSubject Then
                    MsgBox("This does not work sometimes.", MsgBoxStyle.Information, "Test")
                End If
            Next
    
    End Sub
    

    Thanks once again!
    Friday, February 22, 2013 7:52 PM

Answers

  • I understand now! Thank you. I actually moved this code to my addin startup event handler :

    Private ThisOutlookInstance As Outlook.Application = Nothing
    
    ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup

    Not sure how I got sidetracked with Application_StartUp(). In other words I had 2 startup handlers. The above is the right place for it right? I used the below instead. The Me keyword means the same thing as 'ThisAddin.Application' right? When I reference 'ThisAddIn.Application' it is not a shared member.

    ThisOutlookInstance = Me.Application
    Thank you Ken.
    • Marked as answer by ILJester Wednesday, February 27, 2013 2:38 PM
    Tuesday, February 26, 2013 8:03 PM

All replies

  • Where does your code intantiate myReminders?

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "ILJester" <=?utf-8?B?SUxKZXN0ZXI=?=> wrote in message news:890353b7-a1c7-447a-961a-4d48af3209e3...

    Hello everyone. I am seeing an issue I've never seen before. Hopefully someone can help. I am using Outlook 2007 and have developed my add-in in VS2008. The exact error is :

    "2/22/2013 1:17:14 PM - MYREMINDERS_BEFOREREMINDERSHOW ERROR: EXCEPTION TYPE
    ------------------------------------------------------------------------------------------------------------------------------------------------------
    SYSTEM.ACCESSVIOLATIONEXCEPTION

    MESSAGE
    ------------------------------------------------------------------------------------------------------------------------------------------------------
    ATTEMPTED TO READ OR WRITE PROTECTED MEMORY. THIS IS OFTEN AN INDICATION THAT OTHER MEMORY IS CORRUPT.

    STACK TRACE
    ------------------------------------------------------------------------------------------------------------------------------------------------------
       AT VARIANTCLEAR(TAGVARIANT* PVARG)
       AT SYSTEM.RUNTIME.INTEROPSERVICES.CUSTOMMARSHALERS.ENUMERATORVIEWOFENUMVARIANT.MOVENEXT()
       AT OUTLOOKADDIN2.THISADDIN.MYREMINDERS_BEFOREREMINDERSHOW(BOOLEAN& CANCEL)


    2/22/2013 1:17:14 PM - MYREMINDERS_BEFOREREMINDERSHOW FINISH."

    The sub TestOutOfProcess() works on Application_Startup(), but throws the above error in myReminders_BeforeReminderShow(). I don't get it.

    Private WithEvents myReminders As Outlook.Reminders
    
    'Works in here
    Private Sub Application_Startup() Handles Application.Startup
            TestOutOfProcess()
    End Sub
    
    'Does not work in here
    Private Sub myReminders_BeforeReminderShow(ByRef Cancel As Boolean) Handles myReminders.BeforeReminderShow
            AppendLog("myReminders_BeforeReminderShow Begin.", Const_LogToScreen, Const_LogToFile)
    
            Try
                TestOutOfProcess()
            Catch ex As Exception
                AppendLog("myReminders_BeforeReminderShow Error: " & MyErrorMessage(ex).ToString, Const_LogToScreen, Const_LogToFile)
            Finally
                ' cancel the reminder
                Cancel = True
            End Try
    End Sub
    
    Public Sub TestOutOfProcess()
            Dim objReminder As Outlook.Reminder
            Dim objReminders As Outlook.Reminders
    
            objReminders = Application.Reminders
    
            For Each objReminder In objReminders
                If objReminder.Caption = taskSubject Then
                    MsgBox("This does not work sometimes.", 
    MsgBoxStyle.Information, "Test")
                End If
            Next
    
    End Sub
    

    Thanks once again!

    Ken Slovak MVP - Outlook
    Monday, February 25, 2013 7:20 PM
    Moderator
  • If I understand you correctly... the :

    Private WithEvents myReminders As Outlook.Reminders

    is module level. And it is instantiated within the following sub. And that sub is originally called from Application_Startup():

    Public Sub StartRemindingMe()
    
            Dim olApp As Outlook.Application
            Dim olNS As Outlook.NameSpace
            Dim tsk As Outlook.TaskItem
            Dim tasksFolder As Outlook.MAPIFolder
            Dim tasks As Outlook.Items
            Dim matchingTasks As Outlook.Items
            Dim i As Long
            Dim task As Outlook.TaskItem
            ' only start watching reminders when I say so
            olApp = New Outlook.Application
            myReminders = olApp.Reminders
            ' delete any existing tasks
            olNS = olApp.GetNamespace("MAPI")
            tasksFolder = olNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks)
            tasks = tasksFolder.Items
            matchingTasks = tasks.Restrict("[Subject] = '" & taskSubject & "'")
            For i = matchingTasks.Count To 1 Step -1
                task = matchingTasks.Item(i)
                If task.Subject = taskSubject Then
                    With task
                        .MarkComplete()
                        .Delete()
                    End With
                End If
            Next i
    
            Dim today As System.DateTime
    
            today = System.DateTime.Now
    
            ' create initial task
            tsk = Application.CreateItem(Outlook.OlItemType.olTaskItem)
            With tsk
                .Subject = taskSubject
                .StartDate = today
                .ReminderSet = True
                .ReminderTime = today.AddMinutes(MINSBETWEENSCANS)
                .Save()
            End With
        End Sub
    Thanks.
    Monday, February 25, 2013 7:39 PM
  • You didn't show that initialization in the code you showed for the startup handler.
     
    Never create a new Outlook.Application object within an Outlook COM addin:
     
        olApp = New Outlook.Application
    Use the Application object passed to you in the startup event handler.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "ILJester" <=?utf-8?B?SUxKZXN0ZXI=?=> wrote in message news:fb51b282-f73a-4a1a-960e-76708217e822...

    If I understand you correctly... the :

    Private WithEvents myReminders As Outlook.Reminders

    is module level. And it is instantiated within the following sub. And that sub is originally called from Application_Startup():

    Public Sub StartRemindingMe()
    
            Dim olApp As Outlook.Application
            Dim olNS As Outlook.NameSpace
            Dim tsk As Outlook.TaskItem
            Dim tasksFolder As Outlook.MAPIFolder
            Dim tasks As Outlook.Items
            Dim matchingTasks As Outlook.Items
            Dim i As Long
            Dim task As Outlook.TaskItem
            ' only start watching reminders when I say so
            olApp = New Outlook.Application
            myReminders = olApp.Reminders
            ' delete any existing tasks
            olNS = olApp.GetNamespace("MAPI")
            tasksFolder = olNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks)
            tasks = tasksFolder.Items
            matchingTasks = tasks.Restrict("[Subject] = '" & taskSubject & "'")
            For i = matchingTasks.Count To 1 Step -1
                task = matchingTasks.Item(i)
                If task.Subject = taskSubject Then
                    With task
                        .MarkComplete()
                        .Delete()
                    End With
                End If
            Next i
    
            Dim today As System.DateTime
    
            today = System.DateTime.Now
    
            ' create initial task
            tsk = Application.CreateItem(Outlook.OlItemType.olTaskItem)
            With tsk
                .Subject = taskSubject
                .StartDate = today
                .ReminderSet = True
                .ReminderTime = today.AddMinutes(MINSBETWEENSCANS)
                .Save()
            End With
        End Sub
    Thanks.

    Ken Slovak MVP - Outlook
    Monday, February 25, 2013 9:02 PM
    Moderator
  • Thank you Ken. I understand and did not notice the creation of the seperate application object. But I am confused by what you mean as "Use the Application object passed to you in the startup event handler.". I do not see the Application object being passed to me from the startup event handler. My startup event handler is below :

    Private Sub Application_Startup() Handles Application.Startup
            StartRemindingMe()
    End Sub

    Is this the right handler you are referring too? Or are you referring to the addin startup handler? Can you illustrate or elaborate please.

    Thank you.

    Tuesday, February 26, 2013 2:19 PM
  • Public olApp As Outlook.Application = Nothing
    Private Sub Application_Startup() Handles Application.Startup
        olApp = ThisAddin.Application
        StartRemindingMe()
    End Sub
    VSTO does provide a trusted instance of the Outlook.Application object for you. In the snippet above I'm setting a global Application object in the startup handler. That trusted Application object is what should be used to always derive all your Outlook objects.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "ILJester" <=?utf-8?B?SUxKZXN0ZXI=?=> wrote in message news:60ae6b1c-200c-417f-b3e6-ff23fa1154b3...

    Thank you Ken. I understand and did not notice the creation of the seperate application object. But I am confused by what you mean as "Use the Application object passed to you in the startup event handler.". I do not see the Application object being passed to me from the startup event handler. My startup event handler is below :

    Private Sub Application_Startup() Handles Application.Startup
            StartRemindingMe()
    End Sub

    Is this the right handler you are referring too? Or are you referring to the addin startup handler? Can you illustrate or elaborate please.

    Thank you.


    Ken Slovak MVP - Outlook
    Tuesday, February 26, 2013 4:11 PM
    Moderator
  • I understand now! Thank you. I actually moved this code to my addin startup event handler :

    Private ThisOutlookInstance As Outlook.Application = Nothing
    
    ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup

    Not sure how I got sidetracked with Application_StartUp(). In other words I had 2 startup handlers. The above is the right place for it right? I used the below instead. The Me keyword means the same thing as 'ThisAddin.Application' right? When I reference 'ThisAddIn.Application' it is not a shared member.

    ThisOutlookInstance = Me.Application
    Thank you Ken.
    • Marked as answer by ILJester Wednesday, February 27, 2013 2:38 PM
    Tuesday, February 26, 2013 8:03 PM
  • Private Sub ThisAddIn_Startup() Handles Me.Startup

    is the correct startup handler. That's the one VSTO provides for you.

    VSTO handles the Extensibility events OnConnection() and OnStartupComplete() and uses those events to initialize itself. It then provides you with the VSTO startup event handler, ThisAddIn_Startup().

    Me.Application is OK. I'm sorry about the ThisAddIn.Application, that's what I get for writing it off the top of my head. The equivalent of Me.Application would be Globals.ThisAddIn.Application. I forgot the Globals part.


    Ken Slovak MVP - Outlook

    Tuesday, February 26, 2013 9:17 PM
    Moderator
  • Thank you Ken. I appreciate all your help.
    Wednesday, February 27, 2013 2:37 PM