代码之家  ›  专栏  ›  技术社区  ›  Andrew Garrison

如何检测Windows是关闭还是重新启动

  •  20
  • Andrew Garrison  · 技术社区  · 17 年前

    我知道当Windows关闭时,它会发送一个 WM_QUERYENDSESSION

    考虑到MSDN的文档有这样的说法,我并不特别有希望 WM_QUERYENDSESSION

    4 回复  |  直到 17 年前
        1
  •  6
  •   Eric Petroelje    17 年前

        2
  •  6
  •   Danath    12 年前

    here :

    “HKCU\软件\Microsoft\Windows\当前版本\资源管理器\关闭

        3
  •  5
  •   unixman83    12 年前

    WM_ENDSESSION 并记录下来。现在记录时间。如果系统在合理的时间内(比如5分钟)恢复。那是重启,而不是关机。

    想法 真的

    API hooking ExitWindowsEx 以及相关功能,但我不推荐这种方法。如果你真的需要直接检测到这一点,请重新思考。

        4
  •  1
  •   conceptacid    10 年前

    Windows7的可能实验解决方案如下。(我不确定这是否适用于其他本地化,因此我称之为变通方法)

    using System.Diagnostics.Eventing.Reader;
    
    namespace MyApp
    {
    public class RestartDetector : IDisposable
    {
        public delegate void OnShutdownRequsted(bool restart);
        public OnShutdownRequsted onShutdownRequsted;
    
        private EventLogWatcher watcher = null;
    
        public RestartDetector()
        {
            try
            {
                EventLogQuery subscriptionQuery = new EventLogQuery(
                    "System", PathType.LogName, "*[System[Provider[@Name='USER32'] and (EventID=1074)]]");
    
                watcher = new EventLogWatcher(subscriptionQuery);
    
                // Make the watcher listen to the EventRecordWritten
                // events.  When this event happens, the callback method
                // (EventLogEventRead) is called.
                watcher.EventRecordWritten +=
                    new EventHandler<EventRecordWrittenEventArgs>(
                        EventLogEventRead);
    
                // Activate the subscription
                watcher.Enabled = true;
            }
            catch (EventLogReadingException e)
            {
            }
        }
    
        public void EventLogEventRead(object obj, EventRecordWrittenEventArgs arg)
        {
            bool restart = false;
            try
            {
                // Make sure there was no error reading the event.
                if (arg.EventRecord != null)
                {
                    String[] xPathRefs = new String[1];
                    xPathRefs[0] = "Event/EventData/Data";
                    IEnumerable<String> xPathEnum = xPathRefs;
    
                    EventLogPropertySelector logPropertyContext = new EventLogPropertySelector(xPathEnum);
                    IList<object> logEventProps = ((EventLogRecord)arg.EventRecord).GetPropertyValues(logPropertyContext);
    
                    string[] eventData = (string[])logEventProps[0];
    
                    foreach (string attribute in eventData)
                    {
                        if (attribute.Contains("restart")) { restart = true; break; }
                    }
                }
            }
            catch (Exception e)
            {
            }
            finally
            {
                if (onShutdownRequsted != null) { onShutdownRequsted(restart); }
            }   
        }
    
        public void Dispose()
        {
            // Stop listening to events
            if (watcher != null)
            {
                watcher.Enabled = false;
                watcher.Dispose();
            }
        }
    }
    }
    

    - <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
    - <System>
      <Provider Name="USER32" /> 
      <EventID Qualifiers="32768">1074</EventID> 
      <Level>4</Level> 
      <Task>0</Task> 
      <Keywords>0x80000000000000</Keywords> 
      <TimeCreated SystemTime="2015-12-15T11:10:43.000000000Z" /> 
      <EventRecordID>90416</EventRecordID> 
      <Channel>System</Channel> 
      <Computer>WIN7PC</Computer> 
      <Security UserID="S-1-5-21-1257383181-1549154685-2724014583-1000" /> 
      </System>
    - <EventData>
      <Data>C:\Windows\system32\winlogon.exe (WIN7PC)</Data> 
      <Data>WIN7PC</Data> 
      <Data>No title for this reason could be found</Data> 
      <Data>0x500ff</Data> 
      <Data>restart</Data> 
      <Data /> 
      <Data>WIN7PC\WIN7PCUser</Data> 
     <Binary>FF00050000000000000000000000000000000000000000000000000000000000</Binary> 
      </EventData>
      </Event>