private void RestoreWndProc()
        {
            var currentProc = Win32Native.GetWindowLong(new HandleRef(this, hwnd), Win32Native.WindowLongType.WndProc);

            if (currentProc == newWndProcPtr)
            {
                // Restore back default WndProc only if the previous callback is owned by this message filter hook
                Win32Native.SetWindowLong(new HandleRef(this, hwnd), Win32Native.WindowLongType.WndProc, defaultWndProc);
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="Win32.MessageFilterHook" /> class.
        /// </summary>
        /// <param name="hwnd">The HWND.</param>
        private MessageFilterHook(IntPtr hwnd)
        {
            this.currentFilters = new List <IMessageFilter>();
            this.hwnd           = hwnd;

            // Retrieve the previous WndProc associated with this window handle
            defaultWndProc = Win32Native.GetWindowLong(new HandleRef(this, hwnd), Win32Native.WindowLongType.WndProc);

            // Create a pointer to the new WndProc
            newWndProc    = WndProc;
            newWndProcPtr = Marshal.GetFunctionPointerForDelegate(newWndProc);

            // Set our own private wndproc in order to catch NCDestroy message
            Win32Native.SetWindowLong(new HandleRef(this, hwnd), Win32Native.WindowLongType.WndProc, newWndProcPtr);
        }