7

I am currently working on a C# .NET Add-In for Microsoft Outlook. The goal of the Add-In is, to capture the search input from the Outlook Instant Search, and show in a Custom Pane my own search results.

It works pretty well, and with subclassing the Outlook Window with a Native Window, I get the search string, and it already passes that into my panel.

The problem is now, that when you close the Add-In (via "File->Options->Add-Ins->COM Add-Ins", but not with the X in the pane), the Add-In gets terminated instantly and I can't call searchboxWindow.ReleaseHandle() beforehand to restore my WndProc chain. Outlook simply crashes without any visible errors.

protected override void WndProc(ref Message m)
{
    base.WndProc(ref m);

    switch ((uint)m.Msg)
    {
        case WindowMessages.WM_DESTROY:
        case WindowMessages.WM_QUIT:
        case WindowMessages.WM_NCDESTROY:
            this.ReleaseHandle();
            return;

        case WindowMessages.WM_KEYUP:
        case WindowMessages.WM_LBUTTONDOWN:
        case WindowMessages.WM_RBUTTONDOWN:
            OnKeyUp();
            break;

        case WindowMessages.WM_EXITSIZEMOVE:
            OnResize();
            break;
    }
}

I already tried to listen to a few Window Messages that should be called when the Add-In gets closed, but these messages only appear when I close the Outlook in a normal way.

Also, the events in the main Add-In source file like AppDomain.CurrentDomain.ProcessExit, this.Shutdown, or ((Outlook.ApplicationEvents_10_Event)this.Application).Quit don't get called.

What event can I listen on that (reliably) gets fired when the Add-In is terminated? Are there some? If not, what alternatives to solve my problem do I have?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Florian Schöffl
  • 499
  • 3
  • 12
  • Did you try Dipose(), and attaching a method to the Diposed event to make sure it actually did get disposed? – Mr. B Aug 11 '15 at 15:19
  • I implemented the IDisposable interface and called the ReleaseHandle() in there, but it had no effect? Is that what you mean? – Florian Schöffl Aug 11 '15 at 15:24
  • 3
    Use your add-in's Shutdown event to un-subclass the window. The auto-generated ThisAddIn_Shutdown() event handler has scary language but it is still raised in this specific scenario. – Hans Passant Aug 11 '15 at 16:37
  • "Starting in Outlook 2010, Outlook, [...] no longer calls the ThisAddin_Shutdown method when Outlook is shutting down." [source](https://msdn.microsoft.com/en-us/library/office/ee720183(v=office.14).aspx#OL2010AdditionalShutdownChanges_AddinShutdownChangesinOL2010Beta) - You sure that it is called in this case? I can't check right now – Florian Schöffl Aug 11 '15 at 16:40
  • 1
    This may be one of the rare situations where you might need to implement a finalizer. Take a look at this article https://limbioliong.wordpress.com/2011/07/03/invoking-finalizers-of-net-based-com-objects-in-an-unmanaged-app-part-1/ – Scott Chamberlain Aug 11 '15 at 21:14

3 Answers3

1

SOLVED: Thanks to Hans Passant

It realy seems that the ThisAddIn_Shutdown Event is triggered when the Add-In is manually disconnect through the COM Add-ins dialog box.

Florian Schöffl
  • 499
  • 3
  • 12
0

I don't think there is much you can do in the managed code. Unmamaged code would have worked fine; the COM system would ask you politely whether your dll can be unloaded by calling your implementation of DllCanUnload.

Wandering Fool
  • 2,170
  • 3
  • 18
  • 48
Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
0

Make sure you add a DWORD RequireShutdownNotification=1 in your addin registry, otherwise ThisAddIn_Shutdown() will not be called

Alex
  • 583
  • 5
  • 18