在C#程序中如何判断系统是注销、重启还是关机?
及系统注销、关机、重启的消息ID分别是多少?

解决方案 »

  1.   

    从网上查找得知重载窗口的WndProc方法能够区分是系统关机导致程序关闭还是用户点了程序的“关闭”按钮使程序关闭,但我还需要在这里区分系统是注销、重启还是关机,这几个消息的ID我搜了好长时间了,哪位高手知道?
      

  2.   

    系统在进行如上处理时会向当前所有窗口句柄发送特定字符的消息,重写WndProc方法,在得到这些特定Windows消息时就执行相关处理既可。
      

  3.   

    int WM_QUERYENDSESSION = 0x0011;
    上面是关机消息,其它几个自己找找拉~~
      

  4.   

    using System;
    using System.Text;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    namespace TimerComputerShutdown
    {
    public enum RestartOptions {
    LogOff = 0,
    PowerOff = 8,
    Reboot = 2,
    ShutDown = 1,
    Suspend = -1,
    Hibernate = -2,
    }
    [StructLayout(LayoutKind.Sequential, Pack=1)]
    internal struct LUID {
    public int LowPart;
    public int HighPart;
    }
    [StructLayout(LayoutKind.Sequential, Pack=1)]
    internal struct LUID_AND_ATTRIBUTES {
    public LUID pLuid;
    public int Attributes;
    }
    [StructLayout(LayoutKind.Sequential, Pack=1)]
    internal struct TOKEN_PRIVILEGES {
    public int PrivilegeCount ;
    public LUID_AND_ATTRIBUTES Privileges ;
    }
    public class WindowsController {
    private const int TOKEN_ADJUST_PRIVILEGES = 0x20;
    private const int TOKEN_QUERY = 0x8;
    private const int SE_PRIVILEGE_ENABLED = 0x2;
    private const int FORMAT_MESSAGE_FROM_SYSTEM = 0x1000;
    private const int EWX_FORCE = 4; [ DllImport( "kernel32.dll", EntryPoint="LoadLibraryA", CharSet=CharSet.Ansi )]
    private static extern IntPtr LoadLibrary(string lpLibFileName);
    [ DllImport( "kernel32.dll", EntryPoint="FreeLibrary", CharSet=CharSet.Ansi )]
    private static extern int FreeLibrary(IntPtr hLibModule);
    [ DllImport( "kernel32.dll", EntryPoint="GetProcAddress", CharSet=CharSet.Ansi )]
    private static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);
    [ DllImport( "powrprof.dll", EntryPoint="SetSuspendState", CharSet=CharSet.Ansi )]
    private static extern int SetSuspendState(int Hibernate, int ForceCritical, int DisableWakeEvent);
    [ DllImport( "advapi32.dll", EntryPoint="OpenProcessToken", CharSet=CharSet.Ansi )]
    private static extern int OpenProcessToken(IntPtr ProcessHandle, int DesiredAccess, ref IntPtr TokenHandle);
    [ DllImport( "advapi32.dll", EntryPoint="LookupPrivilegeValueA", CharSet=CharSet.Ansi )]
    private static extern int LookupPrivilegeValue(string lpSystemName, string lpName, ref LUID lpLuid);
    [ DllImport( "advapi32.dll", EntryPoint="AdjustTokenPrivileges", CharSet=CharSet.Ansi )]
    private static extern int AdjustTokenPrivileges(IntPtr TokenHandle, int DisableAllPrivileges, ref TOKEN_PRIVILEGES NewState, int BufferLength, ref TOKEN_PRIVILEGES PreviousState, ref int ReturnLength);
    [ DllImport( "user32.dll", EntryPoint="ExitWindowsEx", CharSet=CharSet.Ansi )]
    private static extern int ExitWindowsEx(int uFlags, int dwReserved);
    [ DllImport( "user32.dll", EntryPoint="FormatMessageA", CharSet=CharSet.Ansi )]
    private static extern int FormatMessage(int dwFlags, IntPtr lpSource, int dwMessageId, int dwLanguageId, StringBuilder lpBuffer, int nSize, int Arguments);
    public static void ExitWindows(RestartOptions how , bool force ) {
    switch(how) {
    case RestartOptions.Suspend:
    { SuspendSystem(false, force);
    break;
    }
    case RestartOptions.Hibernate:
    { SuspendSystem(true, force);
    break;
    }
    default:
    {
    ExitWindows((int)how, force);
    break;
    }
    }
    }
    protected static void ExitWindows(int how , bool force ) {
    EnableToken("SeShutdownPrivilege");
    if (force)how = how | EWX_FORCE;
    if (ExitWindowsEx(how, 0) == 0)
    throw new PrivilegeException(FormatError(Marshal.GetLastWin32Error()));
    }
    protected static void EnableToken(string privilege ) {
    if (!CheckEntryPoint("advapi32.dll", "AdjustTokenPrivileges"))
    return;
    IntPtr tokenHandle = IntPtr.Zero;
    LUID privilegeLUID = new LUID();
    TOKEN_PRIVILEGES newPrivileges = new TOKEN_PRIVILEGES();
    TOKEN_PRIVILEGES tokenPrivileges ;
    if (OpenProcessToken(Process.GetCurrentProcess().Handle, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref tokenHandle) == 0)
    throw new PrivilegeException(FormatError(Marshal.GetLastWin32Error()));
    if (LookupPrivilegeValue("", privilege, ref privilegeLUID) == 0)
    throw new PrivilegeException(FormatError(Marshal.GetLastWin32Error()));
    tokenPrivileges.PrivilegeCount = 1;
    tokenPrivileges.Privileges.Attributes = SE_PRIVILEGE_ENABLED;
    tokenPrivileges.Privileges.pLuid = privilegeLUID;
    int size = 4;
    if (AdjustTokenPrivileges(tokenHandle, 0, ref tokenPrivileges, 4 + (12 * tokenPrivileges.PrivilegeCount), ref newPrivileges, ref size) == 0)
    throw new PrivilegeException(FormatError(Marshal.GetLastWin32Error()));
    }
    protected static void SuspendSystem(bool hibernate , bool force  )
    {
    if (!CheckEntryPoint("powrprof.dll", "SetSuspendState"))
    throw new PlatformNotSupportedException("The SetSuspendState method is not supported on this system!");
    SetSuspendState((int)(hibernate ? 1 : 0), (int)(force ? 1 : 0), 0);
    }
    protected static bool CheckEntryPoint(string library , string method ) {
    IntPtr  libPtr = LoadLibrary(library);
    if (!libPtr.Equals(IntPtr.Zero)) {
    if (!GetProcAddress(libPtr, method).Equals(IntPtr.Zero)) {
    FreeLibrary(libPtr);
    return true;
    }
    FreeLibrary(libPtr);
    }
    FreeLibrary(libPtr);
    return false;
    }
    protected static string FormatError(int number ) {
    StringBuilder buffer =new StringBuilder(255);
    FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, IntPtr.Zero, number, 0, buffer, buffer.Capacity, 0);
    return buffer.ToString();
    }
    }
    public class PrivilegeException : Exception {
    public PrivilegeException () : base() {}
    public PrivilegeException (string message ) :base(message) {}
    }
    }
      

  5.   

    如果是VS2005的话有这几个事件:
    Microsoft.Win32.SystemEvents.SessionEnded;//当用户注销或关闭系统时发生。
    Microsoft.Win32.SystemEvents.SessionEnding; //当用户试图注销或关闭系统时发生。
    Microsoft.Win32.SystemEvents.SessionSwitch;//更改当前登录的用户时发生
      

  6.   

    hoho我上次做了个智能关机的程序