Skip to content

Commit

Permalink
pin the SubclassProc delegate
Browse files Browse the repository at this point in the history
fix crash after reading email item
  • Loading branch information
dinhngtu committed Sep 24, 2021
1 parent 9e1448f commit 568889d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyVersion("1.2.0.0")]
[assembly: AssemblyFileVersion("1.2.0.0")]

12 changes: 9 additions & 3 deletions ThisAddIn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public partial class ThisAddIn {
const int SW_MINIMIZE = 6;

IntPtr _hWnd = IntPtr.Zero, _scProc = IntPtr.Zero;
SubclassProc _scDelegate;
GCHandle _gch;

IntPtr MySubclassProc(IntPtr hWnd, uint uMsg, UIntPtr wParam, IntPtr lParam, UIntPtr uIdSubclass, UIntPtr dwRefData) {
Expand All @@ -47,21 +48,26 @@ private void ThisAddIn_Startup(object sender, System.EventArgs e) {
// before our module is unloaded to avoid crashes
((Outlook.ApplicationEvents_11_Event)Application).Quit += ThisAddIn_Quit;
if (((IOleWindow)Application.ActiveExplorer()).GetWindow(out _hWnd) == S_OK && _hWnd != IntPtr.Zero) {
_gch = GCHandle.Alloc(this);
_scProc = Marshal.GetFunctionPointerForDelegate((SubclassProc)MySubclassProc);
_scDelegate = new SubclassProc(MySubclassProc);
_gch = GCHandle.Alloc(_scProc);
_scProc = Marshal.GetFunctionPointerForDelegate(_scDelegate);
if (SetWindowSubclass(_hWnd, _scProc, UIntPtr.Zero, UIntPtr.Zero)) {
return;
}
_scProc = IntPtr.Zero;
_gch.Free();
_scDelegate = null;
}
_hWnd = IntPtr.Zero;
}

private void ThisAddIn_Quit() {
if (_hWnd != IntPtr.Zero) {
RemoveWindowSubclass(_hWnd, _scProc, UIntPtr.Zero);
_hWnd = IntPtr.Zero;
_scProc = IntPtr.Zero;
_gch.Free();
_scDelegate = null;
_hWnd = IntPtr.Zero;
}
}

Expand Down

0 comments on commit 568889d

Please sign in to comment.