数学建模社区-数学中国
标题:
用C++Builder写的一个关闭机器的函数!
[打印本页]
作者:
ilikenba
时间:
2004-9-24 21:07
标题:
用C++Builder写的一个关闭机器的函数!
<
>bool closecomputer(int kind)
8 j& M, O5 [2 _) V
{
% [$ g( w% g g! O5 B9 E1 l
HANDLE hToken;
+ S# t7 b) M f6 P% w" H- O
TOKEN_PRIVILEGES tkp; </P>
) l: j) t: K! O' |1 t
<
> // Get a token for this process.
# m' k8 s9 L/ I2 N/ W
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
* P- U7 B2 W& f1 V- }& F; ]& H# S
{
, z% V J9 {6 \
return false;
$ T# c/ Y6 c E' y7 a- ?8 q
}</P>
( b0 o5 f$ E& J
<
> // Get the LUID for the shutdown privilege.
& A! S; M0 _' v) b/ o ?
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid); </P>
# c, o m7 K0 K! w% M
<
> tkp.PrivilegeCount = 1; // one privilege to set
4 `" r' D* V) T
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;</P>
- Y4 c$ l* U) U+ B1 ]3 |
<
> // Get the shutdown privilege for this process.
7 ^% Q) Y6 L$ s# v" _! z
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);</P>
: v% G7 ]5 x$ X; Z& U
<
> if (GetLastError() != ERROR_SUCCESS)
; ?1 W4 T, ` j+ K. l/ S5 @) i
{
' _5 [/ |% o2 M
return false;
, {: k& ?1 p. S# C& A2 W V: l
}
. V( x8 v! |' r( o7 X
switch(kind)
+ s4 _! m- E l* N B
{case 1: ExitWindowsEx(EWX_FORCE|EWX_REBOOT,0); break; //重启
4 B5 {* y9 U$ r4 X3 e
case 2: ExitWindowsEx(EWX_FORCE|EWX_SHUTDOWN|EWX_POWEROFF,0); break; //关机
5 S+ A# \' K4 y3 d; B |
case 3: ExitWindowsEx(EWX_FORCE|EWX_LOGOFF,0); break; //注销
! t @! r0 ]) r4 o
case 4: SetSystemPowerState(false, true); break; //休眠
; L& v$ l# R- l' P
case 5: SetSystemPowerState(true, true); break; //待机
! a0 z! {( x! E4 h/ q6 [6 _! q& Q! s( U
default: break;
[+ h6 a/ A) L
}
& u: e. C+ w3 B" T5 s
return true;</P>
; L0 p1 w! t/ D7 [1 N/ T9 H
<
>}</P>
欢迎光临 数学建模社区-数学中国 (http://www.madio.net/)
Powered by Discuz! X2.5