我需要在关机/注销时执行代码。当任何这些操作发生时,我得到一个代码为0xe0434352的应用程序错误。我做错了什么?
这是我的代码:
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));
}
答案 0 :(得分:0)
没关系,需要管理员权限。