注销与关机的代码
范例里只有关闭显示器的
xiongmao1313 答
代码来自http://www.ecranesoft.com/bbs/showtopic-8765.aspx
////关机函数/
ExitWindowsEx = ::User32.api("ExitWindowsEx","int(int uFlags,pointer dwReserved)")
SetSystemPowerState = ::Kernel32.api("SetSystemPowerState","int(int fSuspend,int fForce)")
win.exit= function( mode ){
if(! setprivilege("SeShutdownPrivilege",true) )
return false;
var result;
select(mode){
case 0 {
thread.stop();
}
case 1 {
//关机
result = ExitWindowsEx(0x1/*_EWX_SHUTDOWN*/| 0x00000008/*_EWX_POWEROFF*/ |0x4/*_EWX_FORCE*/ );
ApeConfig.ExitApplication();
}
case 2 {
//重启
result = ExitWindowsEx(0x2/*_EWX_REBOOT*/|0x4/*_EWX_FORCE*/ );
ApeConfig.ExitApplication();
}
case 3 {
//注销
result = ExitWindowsEx(0x0/*_EWX_LOGOFF*/|0x4/*_EWX_FORCE*/);
}
case 4 {
//待机
result = SetSystemPowerState( 1, 1);
}
else {
//休眠
result = SetSystemPowerState( 0, 0 );
}
}
return result ;
}
后边直接调用就可以
win.exit(1);//关机
win.exit(2);//重启
crane 答
封装到库里了,下载SVN最新版本即可。
有库的机制,一切都可以变得简单。
import win.util |
要先提权才能关机的,可以查看win.util的源码
希望大家注意一下这个函数封装到库的过程,其实非常简单。
利用库,可以封装简化复杂的代码,让一切变的简单,熟练掌握这个机制,会有很大的帮助。
浙公网安备 33010602011771号