天天看點

c# 關機 代碼

c# 關機 代碼

using  System;

c# 關機 代碼

using  System.Collections.Generic;

c# 關機 代碼

using  System.ComponentModel;

c# 關機 代碼

using  System.Data;

c# 關機 代碼

using  System.Drawing;

c# 關機 代碼

using  System.Text;

c# 關機 代碼

using  System.Windows.Forms;

c# 關機 代碼

using  System.Runtime.InteropServices; 

c# 關機 代碼
c# 關機 代碼

namespace  test

c# 關機 代碼
c# 關機 代碼

... {

c# 關機 代碼

    public partial class Form1 : Form

c# 關機 代碼
c# 關機 代碼

    ...{

c# 關機 代碼

        public Form1()

c# 關機 代碼
c# 關機 代碼

        ...{

c# 關機 代碼

            InitializeComponent();

c# 關機 代碼

        }

c# 關機 代碼
c# 關機 代碼

        [StructLayout(LayoutKind.Sequential, Pack = 1)]

c# 關機 代碼

        internal struct TokPriv1Luid

c# 關機 代碼
c# 關機 代碼

        ...{

c# 關機 代碼

            public int Count;

c# 關機 代碼

            public long Luid;

c# 關機 代碼

            public int Attr;

c# 關機 代碼

        }

c# 關機 代碼
c# 關機 代碼

        [DllImport("kernel32.dll", ExactSpelling = true)]

c# 關機 代碼

        internal static extern IntPtr GetCurrentProcess();

c# 關機 代碼
c# 關機 代碼

        [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]

c# 關機 代碼

        internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);

c# 關機 代碼
c# 關機 代碼

        [DllImport("advapi32.dll", SetLastError = true)]

c# 關機 代碼

        internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);

c# 關機 代碼
c# 關機 代碼

        [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]

c# 關機 代碼

        internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,

c# 關機 代碼

        ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);

c# 關機 代碼
c# 關機 代碼

        [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]

c# 關機 代碼

        internal static extern bool ExitWindowsEx(int flg, int rea);

c# 關機 代碼
c# 關機 代碼

        internal const int SE_PRIVILEGE_ENABLED = 0x00000002;

c# 關機 代碼

        internal const int TOKEN_QUERY = 0x00000008;

c# 關機 代碼

        internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;

c# 關機 代碼

        internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";

c# 關機 代碼

        internal const int EWX_LOGOFF = 0x00000000;

c# 關機 代碼

        internal const int EWX_SHUTDOWN = 0x00000001;

c# 關機 代碼

        internal const int EWX_REBOOT = 0x00000002;

c# 關機 代碼

        internal const int EWX_FORCE = 0x00000004;

c# 關機 代碼

        internal const int EWX_POWEROFF = 0x00000008;

c# 關機 代碼

        internal const int EWX_FORCEIFHUNG = 0x00000010;

c# 關機 代碼
c# 關機 代碼

        private static void DoExitWin(int flg)

c# 關機 代碼
c# 關機 代碼

        ...{

c# 關機 代碼

            bool ok;

c# 關機 代碼

            TokPriv1Luid tp;

c# 關機 代碼

            IntPtr hproc = GetCurrentProcess();

c# 關機 代碼

            IntPtr htok = IntPtr.Zero;

c# 關機 代碼

            ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);

c# 關機 代碼

            tp.Count = 1;

c# 關機 代碼

            tp.Luid = 0;

c# 關機 代碼

            tp.Attr = SE_PRIVILEGE_ENABLED;

c# 關機 代碼

            ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);

c# 關機 代碼

            ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);

c# 關機 代碼

            ok = ExitWindowsEx(flg, 0);

c# 關機 代碼

        } 

c# 關機 代碼
c# 關機 代碼
c# 關機 代碼
c# 關機 代碼

        private void button1_Click(object sender, EventArgs e)

c# 關機 代碼
c# 關機 代碼

        ...{

c# 關機 代碼

            if (radioButton1.Checked == true)

c# 關機 代碼
c# 關機 代碼

            ...{

c# 關機 代碼

                MessageBox.Show("哼不當我是好老公,給你個懲罰哼哼!");

c# 關機 代碼

                DoExitWin(EWX_SHUTDOWN); 

c# 關機 代碼

            }

c# 關機 代碼

            else

c# 關機 代碼
c# 關機 代碼

            ...{

c# 關機 代碼

                MessageBox.Show("老婆老婆我也愛你,親親寶貝!");

c# 關機 代碼

                //關閉程式

c# 關機 代碼

                Application.Exit(); 

c# 關機 代碼

            }

c# 關機 代碼

            //MessageBox.Show("2");

c# 關機 代碼

        }

c# 關機 代碼

    }

c# 關機 代碼

}