Search code examples
wpfexceptionwndproc

WPF and WndProc hangs with .NET exception


I need to execute a code when shutdown/logoff. When any of these action are made im getting an application error with code 0xe0434352. What im doing wrong?

This is my code:

    private void Window_SourceInitialized(object sender, EventArgs e)
    {
        IntPtr windowHandle = (new WindowInteropHelper(this)).Handle;
        HwndSource src = HwndSource.FromHwnd(windowHandle);
        src.AddHook(new HwndSourceHook(WndProc));
    }

    private IntPtr WndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        if (msg == 0x0011)
        {
            EventLog.WriteEntry("WPF1", "finally!");
            //my code
        }
        return IntPtr.Zero;
    }

    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        IntPtr windowHandle = (new WindowInteropHelper(this)).Handle;
        HwndSource src = HwndSource.FromHwnd(windowHandle);
        src.RemoveHook(new HwndSourceHook(this.WndProc));
    }

Exception error: http://pastebin.com/gaX0ekaP


Solution

  • Nevermind, admin priviledges were required.