Exemple #1
0
        private static void HookUpDefWindowProc(IntPtr hwnd)
        {
            SecurityHelper.DemandUnmanagedCode();

#if LOGGING
            LogFinishHWND(hwnd, "Core HookUpDWP");
#endif

            IntPtr result = IntPtr.Zero;

            // We've already cleaned up, return immediately.
            if (hwnd == IntPtr.Zero)
            {
                return;
            }

            IntPtr defWindowProc = GetDefWindowProcAddress(hwnd);

            if (defWindowProc != IntPtr.Zero)
            {
                try
                {
                    result = UnsafeNativeMethods.SetWindowLong(new HandleRef(null, hwnd), NativeMethods.GWL_WNDPROC, defWindowProc);
                }
                catch (System.ComponentModel.Win32Exception e)
                {
                    // We failed to change the window proc.  Now what?

                    if (e.NativeErrorCode != 1400) // ERROR_INVALID_WINDOW_HANDLE
                    {
                        // For debugging purposes, throw an exception so we can debug
                        // this and know if it's possible to call SetWindowLong on
                        // the wrong thread.
                        throw;
                    }
                }
                if (result != IntPtr.Zero)
                {
                    UnsafeNativeMethods.PostMessage(new HandleRef(null, hwnd), WindowMessage.WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
                }
            }
        }