c#中关于拦截关机消息的问题!!

wisdomhxz 2006-10-27 12:33:39
private static int WM_QUERYENDSESSION = 0x11;
private static bool systemShutdown = false;
private static bool IsselfClose = false;
private IntPtr _lp;

protected override void WndProc(ref System.Windows.Forms.Message m)
{
if (m.Msg == WM_QUERYENDSESSION)
{
if (!IsselfClose)
{
systemShutdown = true;
_lp = m.LParam;
}
}
base.WndProc(ref m);
} //WndProc



private void FormSetting_FormClosing(object sender, FormClosingEventArgs e)
{
if (systemShutdown)
{
e.Cancel = true;
systemShutdown = false;
WriteLog wl = new WriteLog(_LogPath, _UserName,_lp.ToInt32());
wl.Show();
}
}


为什么一注销或关机的时候,我的WriteLog窗口可以跳出来,然后一闪,就关机/注销了~~~

我想拦截到关机或注销消息的时候,跳出这个窗口,同时取消关机或注销.

我这段程序应该怎么改~~~~?

跪求高手~~~
...全文
523 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
soft_df 2006-11-11
  • 打赏
  • 举报
回复
API可以解决
CallWindowProc、SetWindowLong。
xu_2007 2006-11-11
  • 打赏
  • 举报
回复
你也可以用直接修改注册表的方法来改变关闭程序等待时间的问题
xu_2007 2006-11-11
  • 打赏
  • 举报
回复
这跟超给兔子有什么关系啊
VirtualDesktop 2006-11-11
  • 打赏
  • 举报
回复
倒~那lz给自己结贴吧~
soft_df 2006-11-11
  • 打赏
  • 举报
回复
我有VB6.0写的。
wisdomhxz 2006-11-10
  • 打赏
  • 举报
回复
解决了...我都忘记我有这贴了....
上面的代码并没有问题....问题是超级兔子中有一个设定关闭程序等待时间的....

设成默认,就不会有问题了
windows19790408 2006-11-10
  • 打赏
  • 举报
回复
关注
wisdomhxz 2006-10-27
  • 打赏
  • 举报
回复
是用来提醒写工作日志的,很多人没有写的习惯,下班就关机走人.
用这个程序可以在关机的时候提醒人写日志


很郁闷怎么有时候行有时候不行......


xiaohutushen 2006-10-27
  • 打赏
  • 举报
回复
有什么用呢.人家直接关电源,呵呵

不过支持一下
wisdomhxz 2006-10-27
  • 打赏
  • 举报
回复
没人会吗?
wisdomhxz 2006-10-27
  • 打赏
  • 举报
回复
在线等~~~谁能帮我看看!
wisdomhxz 2006-10-27
  • 打赏
  • 举报
回复
楼上的不行,弹是弹出来了,可是弹出来后还是关机了~~~~
VirtualDesktop 2006-10-27
  • 打赏
  • 举报
回复
立即弹出一个MessageBox即可
wisdomhxz 2006-10-27
  • 打赏
  • 举报
回复
还是不行~~继续顶~
fernanado 2006-10-27
  • 打赏
  • 举报
回复
个人愚见:
可以考虑把这段代码
WriteLog wl = new WriteLog(_LogPath, _UserName,_lp.ToInt32());
wl.Show();
放在函数 WndProc 的 这句 base.WndProc(ref m); 之前,并将 Show() 改为 ShowDialog(),这样在你手动关掉窗口后再处理系统的关机任务。
C# 关机程序 收藏 1. using System; 2. using System.Runtime.InteropServices; 3. 4. class shoutdown{ 5. [StructLayout(LayoutKind.Sequential, Pack=1)] 6. internal struct TokPriv1Luid 7. { 8. public int Count; 9. public long Luid; 10. public int Attr; 11. } 12. 13. [DllImport("kernel32.dll", ExactSpelling=true) ] 14. internal static extern IntPtr GetCurrentProcess(); 15. 16. [DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ] 17. internal static extern bool OpenProcessToken( IntPtr h, int acc, ref IntPtr phtok ); 18. 19. [DllImport("advapi32.dll", SetLastError=true) ] 20. internal static extern bool LookupPrivilegeValue( string host, string name, ref long pluid ); 21. 22. [DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ] 23. internal static extern bool AdjustTokenPrivileges( IntPtr htok, bool disall, 24. ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen ); 25. 26. [DllImport("user32.dll", ExactSpelling=true, SetLastError=true) ] 27. internal static extern bool ExitWindowsEx( int flg, int rea ); 28. 29. internal const int SE_PRIVILEGE_ENABLED = 0x00000002; 30. internal const int TOKEN_QUERY = 0x00000008; 31. internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020; 32. internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege"; 33. internal const int EWX_LOGOFF = 0x00000000; 34. internal const int EWX_SHUTDOWN = 0x00000001; 35. internal const int EWX_REBOOT = 0x00000002; 36. internal const int EWX_FORCE = 0x00000004; 37. internal const int EWX_POWEROFF = 0x00000008; 38. internal const int EWX_FORCEIFHUNG = 0x00000010; 39. 40. private static void DoExitWin(int flg) 41. { 42. bool ok; 43. TokPriv1Luid tp; 44. IntPtr hproc = GetCurrentProcess(); 45. IntPtr htok = IntPtr.Zero; 46. ok = OpenProcessToken( hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok ); 47. tp.Count = 1; 48. tp.Luid = 0; 49. tp.Attr = SE_PRIVILEGE_ENABLED; 50. ok = LookupPrivilegeValue( null, SE_SHUTDOWN_NAME, ref tp.Luid ); 51. ok = AdjustTokenPrivileges( htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero ); 52. ok = ExitWindowsEx( flg, 0 ); 53. } 54. 55. public static void Main() 56. { 57. Console.WriteLine("正在关机……"); 58. // 修改 EWX_SHUTDOWN 或者 EWX_LOGOFF, EWX_REBOOT等实现不同得功能。 59. // 在XP下可以看到帮助信息,以得到不同得参数 60. // SHUTDOWN /? 61. DoExitWin(EWX_SHUTDOWN); 62. } 63. } 64. 65.

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧