Native Window: Release Handle On Close

701 Views Asked by At

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?

3

There are 3 best solutions below

0
On BEST ANSWER

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.

1
On

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.

0
On

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