首页 > 其他 > 详细

C# 实现快速闪电关机、快速重启

时间:2014-05-21 17:17:48      阅读:655      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
using System;
using System.Runtime.InteropServices;

namespace FastReboot
{
    static class Program
    {
        private delegate uint ZwShutdownSystem(int ShutdownAction);//编译
        private delegate uint RtlAdjustPrivilege(int Privilege, bool Enable, bool CurrentThread, ref int Enabled);

        [DllImport("kernel32.dll")]
        private extern static IntPtr LoadLibrary(String path);
        [DllImport("kernel32.dll")]
        private extern static IntPtr GetProcAddress(IntPtr lib, String funcName);
        [DllImport("kernel32.dll")]
        private extern static bool FreeLibrary(IntPtr lib);

        //将要执行的函数转换为委托
        private static Delegate Invoke(String APIName, Type t, IntPtr hLib)
        {
            IntPtr api = GetProcAddress(hLib, APIName);
            return (Delegate)Marshal.GetDelegateForFunctionPointer(api, t);
        }
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            IntPtr hLib = LoadLibrary("ntdll.dll");
            RtlAdjustPrivilege rtla = (RtlAdjustPrivilege)Invoke("RtlAdjustPrivilege", typeof(RtlAdjustPrivilege), hLib);
            ZwShutdownSystem shutdown = (ZwShutdownSystem)Invoke("ZwShutdownSystem", typeof(ZwShutdownSystem), hLib);

            int en = 0;
            uint ret = rtla(0x13, true, false, ref en);//SE_SHUTDOWN_PRIVILEGE = 0x13;     //关机权限
            ret = shutdown(1); // POWEROFF = 0x2 // 关机 // REBOOT = 0x1 // 重启
        }
    }
}
bubuko.com,布布扣

 

用C#实现闪电关机、重启。XP、2003、win8 x64测试正常。win7没有测试环境。未作测试。

C# 实现快速闪电关机、快速重启,布布扣,bubuko.com

C# 实现快速闪电关机、快速重启

原文:http://www.cnblogs.com/leeairw/p/3740430.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!