天天看点

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# 关机 代码

}