快速关机[原创]

// 以下代码实现在NT以上系统的快速关机,关机速度在2秒以内,不保存资料,不伤硬盘
typedef
enum _SHUTDOWN_ACTION {

     ShutdownNoReboot,      // 关机

     ShutdownReboot,         // 重启

     ShutdownPowerOff      // 这个没有试,有心的人试下

}SHUTDOWN_ACTION;

 

DWORD (__stdcall *NtShutdownSystem)(SHUTDOWN_ACTION);

 

VOID ShutDown()

{

     HANDLE hToken;
     // 得到关机权限 

     if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES, &hToken))

     {

         TOKEN_PRIVILEGES tkp;

 

         LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);

        

         tkp.PrivilegeCount = 1;

         tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

 

         AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, NULL, 0);

         CloseHandle(hToken);

     }

 

     // 加载未公开API,强行关机(快速)

     HMODULE mod;

     mod = LoadLibrary("ntdll.dll");

     NtShutdownSystem = (DWORD(__stdcall *)(SHUTDOWN_ACTION))GetProcAddress(mod, "NtShutdownSystem");

 

     NtShutdownSystem(ShutdownNoReboot);

}

posted @ 2006-11-03 18:02  至尊王者  阅读(841)  评论(1编辑  收藏  举报