数学建模社区-数学中国

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

作者: ilikenba    时间: 2004-9-24 21:07
标题: 用C++Builder写的一个关闭机器的函数!
<>bool  closecomputer(int kind)/ G! J8 n+ E6 s9 d' j& z7 ?
{
! Q7 f; y( ~! ~+ p' N    HANDLE hToken;/ U" t" z+ c7 M0 W
    TOKEN_PRIVILEGES tkp; </P>
+ Y: }7 k$ e% t) x# y. b<>    // Get a token for this process.
3 p* ~+ Z0 d  V8 D# B+ \+ F    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &amp;hToken))$ B$ F3 g, q: ?' F+ H; X
    {
4 w4 R! _: d8 f" v- ~      return false;
+ `- V% I. b, _; ]& U  s    }</P>
1 w3 q2 b, q+ H' P: ^2 k  O<>    // Get the LUID for the shutdown privilege.5 l/ Z5 m& l' t7 e* N+ |- u
    LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &amp;tkp.Privileges[0].Luid); </P>9 k& u* d' N  w  {! v/ o, k4 m! C
<>    tkp.PrivilegeCount = 1;  // one privilege to set
: Z- q! p* o1 x/ t# m( x6 x    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;</P>8 U! E* N: d, E# }# S8 x9 z/ T4 n
<>    // Get the shutdown privilege for this process.) M: ^$ u0 q) B$ o
    AdjustTokenPrivileges(hToken, FALSE, &amp;tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);</P>
3 Z8 X0 i: Y* B2 K3 G<>    if (GetLastError() != ERROR_SUCCESS)
+ M' a' [" |: b8 X; s    {
: w$ e3 s! r) r  L. @) W4 D; o      return false;) ~# K2 [8 [! K; d+ N5 v
    }
& B! g" U3 U+ ~    switch(kind)$ @* c' k5 h8 n, n
    {case 1: ExitWindowsEx(EWX_FORCE|EWX_REBOOT,0); break; //重启8 A* p! S: L3 {( I1 f2 n
     case 2: ExitWindowsEx(EWX_FORCE|EWX_SHUTDOWN|EWX_POWEROFF,0); break; //关机, f/ S4 N. |; j, l
     case 3: ExitWindowsEx(EWX_FORCE|EWX_LOGOFF,0); break; //注销# x9 p& h1 n8 X3 `2 z7 v4 k, i" _
     case 4: SetSystemPowerState(false, true); break; //休眠' e6 D. d9 \4 K$ {; A: c
     case 5: SetSystemPowerState(true, true); break; //待机& I, G2 }% D0 h  ^+ P
     default:  break;- ~  i, ^+ j( X: U4 c% {& u/ B! Y1 ?
    }
1 b+ g  c3 D6 K/ ?    return true;</P>
% v, r( B# h1 Q' h" {6 [. O/ S1 ~<>}</P>




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