win-pvdrivers

changeset 111:f565bdc4f522

fix bugs in shutdown - thanks to Nico Prenzel
author James Harper <james.harper@bendigoit.com.au>
date Fri Jan 11 23:47:45 2008 +1100 (2008-01-11)
parents ae2b8983f881
children 57b0d44f10f5
files ShutdownMon/Program.cs
line diff
     1.1 --- a/ShutdownMon/Program.cs	Fri Jan 11 23:46:10 2008 +1100
     1.2 +++ b/ShutdownMon/Program.cs	Fri Jan 11 23:47:45 2008 +1100
     1.3 @@ -15,6 +15,66 @@ namespace ShutdownMon
     1.4  {
     1.5      class Program : ServiceBase
     1.6      {
     1.7 +        /* 
     1.8 +        * Shutdown definition see here: http://msdn2.microsoft.com/en-us/library/aa376868.aspx
     1.9 +        */
    1.10 +
    1.11 +        //public enum ExitWindows : uint
    1.12 +        internal enum ExitWindows : uint
    1.13 +        {
    1.14 +            // ONE of the following five:
    1.15 +            LogOff = 0x00,
    1.16 +            ShutDown = 0x01,
    1.17 +            Reboot = 0x02,
    1.18 +            PowerOff = 0x08,
    1.19 +            RestartApps = 0x40,
    1.20 +            // plus AT MOST ONE of the following two:
    1.21 +            Force = 0x04,
    1.22 +            ForceIfHung = 0x10,
    1.23 +        }
    1.24 +
    1.25 +        internal enum ShutdownReason : uint
    1.26 +        {
    1.27 +            MajorApplication = 0x00040000,
    1.28 +            MajorHardware = 0x00010000,
    1.29 +            MajorLegacyApi = 0x00070000,
    1.30 +            MajorOperatingSystem = 0x00020000,
    1.31 +            MajorOther = 0x00000000,
    1.32 +            MajorPower = 0x00060000,
    1.33 +            MajorSoftware = 0x00030000,
    1.34 +            MajorSystem = 0x00050000,
    1.35 +
    1.36 +            MinorBlueScreen = 0x0000000F,
    1.37 +            MinorCordUnplugged = 0x0000000b,
    1.38 +            MinorDisk = 0x00000007,
    1.39 +            MinorEnvironment = 0x0000000c,
    1.40 +            MinorHardwareDriver = 0x0000000d,
    1.41 +            MinorHotfix = 0x00000011,
    1.42 +            MinorHung = 0x00000005,
    1.43 +            MinorInstallation = 0x00000002,
    1.44 +            MinorMaintenance = 0x00000001,
    1.45 +            MinorMMC = 0x00000019,
    1.46 +            MinorNetworkConnectivity = 0x00000014,
    1.47 +            MinorNetworkCard = 0x00000009,
    1.48 +            MinorOther = 0x00000000,
    1.49 +            MinorOtherDriver = 0x0000000e,
    1.50 +            MinorPowerSupply = 0x0000000a,
    1.51 +            MinorProcessor = 0x00000008,
    1.52 +            MinorReconfig = 0x00000004,
    1.53 +            MinorSecurity = 0x00000013,
    1.54 +            MinorSecurityFix = 0x00000012,
    1.55 +            MinorSecurityFixUninstall = 0x00000018,
    1.56 +            MinorServicePack = 0x00000010,
    1.57 +            MinorServicePackUninstall = 0x00000016,
    1.58 +            MinorTermSrv = 0x00000020,
    1.59 +            MinorUnstable = 0x00000006,
    1.60 +            MinorUpgrade = 0x00000003,
    1.61 +            MinorWMI = 0x00000015,
    1.62 +
    1.63 +            FlagUserDefined = 0x40000000,
    1.64 +            FlagPlanned = 0x80000000
    1.65 +        }
    1.66 +
    1.67          const UInt32 READ_CONTROL = 0x00020000;
    1.68          const UInt32 STANDARD_RIGHTS_READ = READ_CONTROL;
    1.69          const UInt32 FILE_READ_DATA = 0x0001;
    1.70 @@ -56,20 +116,16 @@ namespace ShutdownMon
    1.71          internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
    1.72  
    1.73          [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    1.74 -        internal static extern bool ExitWindowsEx(int flg, int rea);
    1.75 -
    1.76 +        
    1.77 +        internal static extern bool ExitWindowsEx(ExitWindows uFlags, ShutdownReason dwReason);
    1.78 +        
    1.79          internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
    1.80          internal const int TOKEN_QUERY = 0x00000008;
    1.81          internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
    1.82          internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
    1.83 -        internal const int EWX_LOGOFF = 0x00000000;
    1.84 -        internal const int EWX_SHUTDOWN = 0x00000001;
    1.85 -        internal const int EWX_REBOOT = 0x00000002;
    1.86 -        internal const int EWX_FORCE = 0x00000004;
    1.87 -        internal const int EWX_POWEROFF = 0x00000008;
    1.88 -        internal const int EWX_FORCEIFHUNG = 0x00000010;
    1.89 +
    1.90  
    1.91 -        private static void DoExitWin(int flg)
    1.92 +        private static void DoExitWin(ExitWindows flg)
    1.93          {
    1.94              bool ok;
    1.95              TokPriv1Luid tp;
    1.96 @@ -82,7 +138,7 @@ namespace ShutdownMon
    1.97              ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
    1.98              ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero,
    1.99              IntPtr.Zero);
   1.100 -            ok = ExitWindowsEx(flg, 0);
   1.101 +            ok = ExitWindowsEx(flg, ShutdownReason.MajorOther & ShutdownReason.MinorOther);
   1.102          }
   1.103  
   1.104          const string MyServiceName = "XenShutdownMon";
   1.105 @@ -241,12 +297,14 @@ namespace ShutdownMon
   1.106                      case "":
   1.107                          break;
   1.108                      case "reboot":
   1.109 -                        DoExitWin(EWX_REBOOT | EWX_FORCE);
   1.110 +                        //DoExitWin(EWX_REBOOT | EWX_FORCE);
   1.111 +                        DoExitWin(ExitWindows.Reboot | ExitWindows.ForceIfHung);
   1.112                          break;
   1.113                      case "poweroff":
   1.114                      case "halt":
   1.115                      default:
   1.116 -                        DoExitWin(EWX_POWEROFF | EWX_FORCE);
   1.117 +                        //DoExitWin(EWX_POWEROFF | EWX_FORCE);
   1.118 +                        DoExitWin(ExitWindows.PowerOff | ExitWindows.ForceIfHung);
   1.119                          break;
   1.120                  }
   1.121              }