如何实现XP自动关机??(1分)

试试这个:<br><br>void CshutdownDlg::Shutdown()<br>{<br> OSVERSIONINFO osv;<br> osv.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);<br> ::GetVersionEx(&amp;osv); //获得操作系统信息<br> //如果操作系统是 WINNT,那么调整当前进程得优先权<br> if(osv.dwPlatformId==VER_PLATFORM_WIN32_NT)<br> {<br> HANDLE hProcess,hToken;<br> TOKEN_PRIVILEGES Privileges;<br> LUID luid;<br> hProcess = ::GetCurrentProcess();<br> ::OpenProcessToken(hProcess,TOKEN_ADJUST_PRIVILEGES,&amp;hToken);<br> Privileges.PrivilegeCount = 1;<br> ::LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&amp;luid);<br> Privileges.Privileges[0].Luid = luid;<br> Privileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;<br> ::AdjustTokenPrivileges(hToken,FALSE,&amp;Privileges,NULL,NULL,NULL);<br> ::ExitWindowsEx(EWX_SHUTDOWN|EWX_FORCE|EWX_POWEROFF,0);<br> }<br> else<br> ::ExitWindowsEx(EWX_SHUTDOWN,0);<br>}
 
以上程序在vc++、win2000pro/server的环境下调试通过
 
1.常量定义<br>const //[EXIT CODE]<br>&nbsp; EI_SYSEXIT_LOGOFF &nbsp; &nbsp; &nbsp;=$00;<br>&nbsp; EI_SYSEXIT_SHUTDOWN &nbsp; &nbsp;=$01;<br>&nbsp; EI_SYSEXIT_REBOOT &nbsp; &nbsp; &nbsp;=$02;<br>&nbsp; EI_SYSEXIT_FORCE &nbsp; &nbsp; &nbsp; =$04;<br>&nbsp; EI_SYSEXIT_POWEROFF &nbsp; &nbsp;=$08;<br>&nbsp; EI_SYSEXIT_FORCEIFHUNG =$10;<br><br>2.函数<br>//---------[退出系统]---------------------------------------------<br>FUNCTION FkSystemExit(vType:INTEGER;vForce:BOOL):BOOL;<br>VAR<br>&nbsp; hToken: THANDLE;<br>&nbsp; &nbsp; hProc: THANDLE;<br>&nbsp; &nbsp; iLUID: INT64;<br>&nbsp; &nbsp; mPriv, mNewPriv: TOKEN_PRIVILEGES;<br>&nbsp; &nbsp; dLength: DWORD;<br>&nbsp; &nbsp; dExit:DWORD;<br>BEGIN<br>&nbsp; IF vForce THEN dExit:=vType+EWX_FORCE ELSE dExit:=vType;<br>&nbsp; //[WIN9X]<br>&nbsp; IF Win32Platform=VER_PLATFORM_WIN32_WINDOWS THEN<br>&nbsp; &nbsp; BEGIN Result:=ExitWindowsEx(dExit,0); END;<br>&nbsp; //[WINNT]<br>&nbsp; IF Win32Platform=VER_PLATFORM_WIN32_NT &nbsp;THEN<br>&nbsp; &nbsp; BEGIN<br>&nbsp; &nbsp; hProc:=GetCurrentProcess() ;<br>&nbsp; &nbsp; OpenProcessToken(hProc,TOKEN_ADJUST_PRIVILEGES +TOKEN_QUERY,hToken);<br>&nbsp; &nbsp; LookupPrivilegeValue('','SeShutdownPrivilege',iLUID);<br>&nbsp; &nbsp; mPriv.PrivilegeCount:=1;<br>&nbsp; &nbsp; mPriv.Privileges[0].Attributes:=SE_PRIVILEGE_ENABLED;<br>&nbsp; &nbsp; mPriv.Privileges[0].Luid :=iLUID;<br>&nbsp; &nbsp; AdjustTokenPrivileges(hToken,False,mPriv,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (4+(12*mPriv.PrivilegeCount)),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mNewPriv,dLength);<br>&nbsp; &nbsp; Result:=ExitWindowsEx(dExit,0);<br>&nbsp; &nbsp; END;<br>&nbsp; //[END;]<br>END;<br>//-------------------------------------------------------------------------------<br>3.使用方法(请确定使用的是 POWEROFF不是 SHUTDOWN)<br>&nbsp; FkSystemExit(EI_SYSEXIT_POWEROFF,TRUE);<br><br>[:)]在XPpro+ Delphi 7通过。<br>如果此函数无效可能有以下问题<br>1。主板兼容性问题,并不是说能用P4就一定兼容。<br>2。Delphi版本问题,具体原因不清楚,但确实存在。换个版本试一下。<br><br>
 
//Windows NT/2000/XP 获得关机权限<br>procedure AdjustToken;<br>var<br>&nbsp; hdlProcessHandle : Cardinal;<br>&nbsp; hdlTokenHandle : Cardinal;<br>&nbsp; tmpLuid : Int64;<br>&nbsp; tkp : TOKEN_PRIVILEGES;<br>&nbsp; tkpNewButIgnored : TOKEN_PRIVILEGES;<br>&nbsp; lBufferNeeded : Cardinal;<br>&nbsp; Privilege : array[0..0] of _LUID_AND_ATTRIBUTES;<br>begin<br>&nbsp; &nbsp; &nbsp; &nbsp; hdlProcessHandle := GetCurrentProcess;<br>&nbsp; &nbsp; &nbsp; &nbsp; OpenProcessToken(hdlProcessHandle,(TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY),hdlTokenHandle);<br>&nbsp; &nbsp; &nbsp; &nbsp; LookupPrivilegeValue('', 'SeShutdownPrivilege', tmpLuid);<br>&nbsp; &nbsp; &nbsp; &nbsp; Privilege[0].Luid := tmpLuid;<br>&nbsp; &nbsp; &nbsp; &nbsp; Privilege[0].Attributes := SE_PRIVILEGE_ENABLED;<br>&nbsp; &nbsp; &nbsp; &nbsp; tkp.PrivilegeCount := 1;<br>&nbsp; &nbsp; &nbsp; &nbsp; tkp.Privileges[0] := Privilege[0];<br>&nbsp; &nbsp; &nbsp; &nbsp; AdjustTokenPrivileges(hdlTokenHandle,False,tkp,Sizeof(tkpNewButIgnored),tkpNewButIgnored,lBufferNeeded);<br>end;<br><br>procedure TfrmMain.TimerTOutTimer(Sender: TObject);<br>begin<br>&nbsp; AdjustToken;<br>&nbsp; ExitWindowsEx(EWX_SHUTDOWN+EWX_FORCE+EWX_POWEROFF,0);<br>end;<br><br>以上程序在Delphi5环境下编译,在Windows95以上操作系统通过
 
顶部