Beispiel #1
0
		public static extern bool ExitWindowsEx(ExitWindowsType uFlags, ShutdownReason dwReason);
Beispiel #2
0
        //        public enum TOKEN_INFORMATION_CLASS
        //        {
        //            TokenUser = 1,
        //            TokenGroups,
        //            TokenPrivileges,
        //            TokenOwner,
        //            TokenPrimaryGroup,
        //            TokenDefaultDacl,
        //            TokenSource,
        //            TokenType,
        //            TokenImpersonationLevel,
        //            TokenStatistics,
        //            TokenRestrictedSids,
        //            TokenSessionId,
        //            TokenGroupsAndPrivileges,
        //            TokenSessionReference,
        //            TokenSandBoxInert,
        //            TokenAuditPolicy,
        //            TokenOrigin
        //        }
        //
        //        const Int32 ANYSIZE_ARRAY				= 1;
        //        const string SE_SHUTDOWN_NAME			= "SeShutdownPrivilege";
        //        const string SE_RESTORE_NAME			= "SeRestorePrivilege";
        //        const uint SE_PRIVILEGE_ENABLED			= 0x00000002;
        //        const uint STANDARD_RIGHTS_REQUIRED		= 0x000F0000;
        //        const uint STANDARD_RIGHTS_READ			= 0x00020000;
        //        const uint TOKEN_ASSIGN_PRIMARY			= 0x0001;
        //        const uint TOKEN_DUPLICATE				= 0x0002;
        //        const uint TOKEN_IMPERSONATE			= 0x0004;
        //        const uint TOKEN_QUERY					= 0x0008;
        //        const uint TOKEN_QUERY_SOURCE			= 0x0010;
        //        const uint TOKEN_ADJUST_PRIVILEGES		= 0x0020;
        //        const uint TOKEN_ADJUST_GROUPS			= 0x0040;
        //        const uint TOKEN_ADJUST_DEFAULT			= 0x0080;
        //        const uint TOKEN_ADJUST_SESSIONID		= 0x0100;
        //        const uint TOKEN_READ					= (STANDARD_RIGHTS_READ | TOKEN_QUERY);
        //        const uint TOKEN_ALL_ACCESS				= (STANDARD_RIGHTS_REQUIRED | TOKEN_ASSIGN_PRIMARY |
        //                                                    TOKEN_DUPLICATE | TOKEN_IMPERSONATE | TOKEN_QUERY |
        //                                                    TOKEN_QUERY_SOURCE | TOKEN_ADJUST_PRIVILEGES |
        //                                                    TOKEN_ADJUST_GROUPS | TOKEN_ADJUST_DEFAULT |
        //                                                    TOKEN_ADJUST_SESSIONID);
        //        
        //
        //        [System.Runtime.InteropServices.DllImport("advapi32.dll", SetLastError=true, CharSet=System.Runtime.InteropServices.CharSet.Auto)]
        //        [return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]
        //        static extern bool LookupPrivilegeValue(string lpSystemName, string lpName,
        //            out LUID lpLuid);
        //        
        ////		[System.Runtime.InteropServices.DllImport("advapi32.dll", SetLastError=true)]
        ////		[return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]
        ////		static extern bool AdjustTokenPrivileges(IntPtr TokenHandle,
        ////			[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]bool DisableAllPrivileges,
        ////			ref TOKEN_PRIVILEGES NewState,
        ////			UInt32 BufferLength,
        ////			ref TOKEN_PRIVILEGES PreviousState,
        ////			IntPtr ReturnLength);
        //        
        //        [System.Runtime.InteropServices.DllImport("advapi32.dll", SetLastError=true)]
        //        [return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]
        //        static extern bool AdjustTokenPrivileges(IntPtr TokenHandle,
        //            [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]bool DisableAllPrivileges,
        //            ref TOKEN_PRIVILEGES NewState,
        //            UInt32 BufferLength,
        //            IntPtr PreviousState,
        //            IntPtr ReturnLength);
        //
        //        [System.Runtime.InteropServices.DllImport("advapi32.dll", SetLastError=true)]
        //        [return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]
        //        static extern bool OpenProcessToken(IntPtr ProcessHandle,
        //            UInt32 DesiredAccess, out IntPtr TokenHandle);
        //        
        //        struct LUID
        //        {
        //            public uint LowPart;
        //            public uint HighPart;
        //        }
        //        
        //        struct LUID_AND_ATTRIBUTES
        //        {
        //            public LUID Luid;
        //            public uint Attributes;
        //        }
        //
        //        [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
        //        struct TOKEN_PRIVILEGES
        //        {
        //            public UInt32 PrivilegeCount;
        //            [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=ANYSIZE_ARRAY)]
        //            public LUID_AND_ATTRIBUTES [] Privileges;
        //        }
        public bool ShutDownWindows(ExitWindowsType KindOfExit)
        {
            #region dead code?
            //			//TOKEN_PRIVILEGES previousTokenPrivileges;
            //			TOKEN_PRIVILEGES nullTokenPrivileges = new TOKEN_PRIVILEGES();
            //			TOKEN_PRIVILEGES newTokenPrivleges = new TOKEN_PRIVILEGES();
            //			IntPtr returnLength = IntPtr.Zero;
            //			//UInt32 bufferLength;
            //			IntPtr tokenHandle;
            //			IntPtr processHandle;
            //			LUID shutdownLUID;
            //
            //			processHandle = System.Diagnostics.Process.GetCurrentProcess().Handle;
            //
            //			if (!OpenProcessToken(processHandle, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, out tokenHandle))
            //			{
            //				return false;
            //			}
            //
            //			if (!LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, out shutdownLUID))
            //			{
            //				return false;
            //			}
            //
            //			newTokenPrivleges.PrivilegeCount = 1;
            //			newTokenPrivleges.Privileges = new LUID_AND_ATTRIBUTES[1];
            //			newTokenPrivleges.Privileges[0].Luid = shutdownLUID;
            //			newTokenPrivleges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
            //
            //			//int tokenPrivlegesSize = System.Runtime.InteropServices.Marshal.SizeOf(newTokenPrivleges);
            //
            //			IntPtr ptrToZero = IntPtr.Zero;
            //			//System.Runtime.InteropServices.Marshal.PtrToStructure(IntPtr.Zero, nullTokenPrivileges);
            //			if (!AdjustTokenPrivileges(tokenHandle, false, ref newTokenPrivleges, 0,
            //				IntPtr.Zero, IntPtr.Zero))
            //			{
            //				return false;
            //			}
            #endregion

            if (!TokenAdjuster.SetPrivilege("SeShutdownPrivilege"))
                return false;
            if (!ExitWindowsEx(KindOfExit, ShutdownReason.MajorOther & ShutdownReason.MinorOther))
                return false;

            //AdjustTokenPrivileges(tokenHandle, false, ref previousTokenPrivileges, 0, ref nullTokenPrivileges, IntPtr.Zero);
            return true;
        }