数学建模社区-数学中国

标题: 用C++Builder写的一个关闭机器的函数! [打印本页]

作者: ilikenba    时间: 2004-9-24 21:07
标题: 用C++Builder写的一个关闭机器的函数!
<>bool  closecomputer(int kind)1 c% A1 Q1 {) Y+ z% a, K4 [, G
{' `# K" M$ t: P9 q$ l
    HANDLE hToken;
. @8 O! G' a0 @( P$ K    TOKEN_PRIVILEGES tkp; </P>6 n3 b7 Y6 R# e7 Y7 H
<>    // Get a token for this process.( b# H: g9 C. ~! M; S2 [
    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &amp;hToken))/ x0 ^# x$ J8 Z& E0 V0 c( X6 ~
    {2 e2 v7 G$ A( ?
      return false;
: Z; |! o1 Y5 N  T+ \% b5 v    }</P>
  ~3 R$ X0 O: K8 ?  ?: x; v" L" ~<>    // Get the LUID for the shutdown privilege.
3 U- B, h2 Z9 _    LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &amp;tkp.Privileges[0].Luid); </P>
: k( {7 d, ?$ F% H3 j( P6 T3 X<>    tkp.PrivilegeCount = 1;  // one privilege to set
; |) l" r$ g6 C. P  ]! b3 B    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;</P>
7 e7 }; t% V. A<>    // Get the shutdown privilege for this process.
/ L' [& s- V: X8 C0 y% y' ~    AdjustTokenPrivileges(hToken, FALSE, &amp;tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);</P>
. c8 {/ B5 y' n<>    if (GetLastError() != ERROR_SUCCESS)
8 v7 m* q* W# h7 X    {
$ L7 m+ A/ O+ q3 _; G- _      return false;
' F. @9 f3 D' u4 v3 O! l( @    }
! d$ i) k# w9 O$ E6 Y3 n/ o    switch(kind)# b; f* ?3 G2 I; |' e+ t" {5 A" s
    {case 1: ExitWindowsEx(EWX_FORCE|EWX_REBOOT,0); break; //重启
: M: }2 ?/ ~, M% a5 T     case 2: ExitWindowsEx(EWX_FORCE|EWX_SHUTDOWN|EWX_POWEROFF,0); break; //关机
( T6 L% ?  I) J7 X' ?     case 3: ExitWindowsEx(EWX_FORCE|EWX_LOGOFF,0); break; //注销
4 X2 X2 P1 v2 n  X: S6 W     case 4: SetSystemPowerState(false, true); break; //休眠
9 N( T; T$ Z8 K$ P     case 5: SetSystemPowerState(true, true); break; //待机6 P6 b2 e4 [2 R% x
     default:  break;& h$ J# W+ K, R. V
    }
3 R5 o3 i$ E# M- Q5 k2 A  w8 \    return true;</P>
. C: d- V3 j+ M# @<>}</P>




欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5