Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the use of C # system message capture, how to distinguish the message is off or restart?


What I have tried:

//系统关机事件捕获, 并发送数据给串口
        private const int WM_QUERYENDSESSION = 0x0011;
        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            try
            {
                switch (m.Msg)
                {
                    case WM_QUERYENDSESSION: //暂时屏蔽关机拦截
                        m.Result = (IntPtr)1;
                        break;
                }
                base.WndProc(ref m);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
Posted
Updated 23-Apr-17 23:29pm

According to the documentation - WM_QUERYENDSESSION message (Windows)[^] it is not possible...
Quote:
If this parameter is 0, the system is shutting down or restarting (it is not possible to determine which event is occurring).


There are some suggestions here - c++ - How to detect whether Windows is shutting down or restarting[^] on how you might take a reasonable "guess" at what is happening - see the solution from Danath.

I really can't see why you would want to know the difference though
 
Share this answer
 
v2
Comments
Stroylong 24-Apr-17 5:32am    
Is there any way to know whether the system is shut down or restart it? Thank you!
CHill60 24-Apr-17 5:35am    
No, there is not. The documentation is quite clear on that
Stroylong 24-Apr-17 5:43am    
Is there any other solution? In addition to such a solution!
CHill60 24-Apr-17 5:50am    
I've updated my solution
Stroylong 24-Apr-17 5:59am    
Because i need to inform the microcontroller to start and close, and after some of the event handling! Thank you very much for your answer!
See WM_QUERYENDSESSION message (Windows)[^]. Additional information is passed in the lParam member (LParam for System.Windows.Forms.Message):
Quote:
lParam

This parameter can be one or more of the following values. If this parameter is 0, the system is shutting down or restarting (it is not possible to determine which event is occurring).
So you can not detect a restart.
 
Share this answer
 
Comments
Stroylong 24-Apr-17 5:33am    
Is there any solution to know whether the system is shut down or restart? Thank you!
Jochen Arndt 24-Apr-17 5:42am    
I don't know a solution and I guess that there is none.
Stroylong 24-Apr-17 5:48am    
Thank you for your answer!
Jochen Arndt 24-Apr-17 5:51am    
You are welcome.

All I found with a quick search is this:
http://stackoverflow.com/questions/981306/how-to-detect-whether-windows-is-shutting-down-or-restarting

So you might read the event log. But that is a rather complex task and requires handling of special cases.
Stroylong 24-Apr-17 6:08am    
Thank you,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900