Roaming Code

I intend to live forever. So far, so good. [Roaming Code]

c++ Shutdown Code

December 24, 2009

关机 代码 c++
by:FoolishQiang

#include   <stdio.h> 
#include   <windows.h>
int   main()
{
         //尝试用最简单的API函数关机
         if(ExitWindowsEx(EWX_REBOOT,   0))return   TRUE; 
         if(ExitWindowsEx(EWX_REBOOT   |   EWX_FORCE,   0))return   TRUE; 
         //因为上面的关机不成功,所以需要调整本进程的特权 
         HANDLE   hToken; 
         TOKEN_PRIVILEGES   tkp; 
         if   (!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES   |   TOKEN_QUERY,   &hToken))   return   FALSE; 
         //获取关机的特权SE_SHUTDOWN_NAME 
         LookupPrivilegeValue(NULL,   SE_SHUTDOWN_NAME,   &tkp.Privileges[0].Luid); 
         tkp.PrivilegeCount   =   1; 
         tkp.Privileges[0].Attributes   =   SE_PRIVILEGE_ENABLED; 
         AdjustTokenPrivileges(hToken,   FALSE,   &tkp,   0,   (PTOKEN_PRIVILEGES)NULL,   0); 
         //判断是NT内核还是9x内核,选择用不同的方式 
         if(!(GetVersion()   &   0×80000000)) 
            InitiateSystemShutdown(NULL,   NULL,   0,   TRUE,   TRUE); 
         else   if   (!ExitWindowsEx(EWX_POWEROFF,   0)) 
                   if(!ExitWindowsEx(EWX_POWEROFF|EWX_FORCE,   0)) 
                         return   FALSE;
         return   TRUE;
   }

Posted by oooo at 12:13 am | permalink

All comments are moderated. Your comments will not appear here unless approved by the blog owner. Thank you.

Add a comment








     

December 2009
M T W T F S S
    Jan »
 123456
78910111213
14151617181920
21222324252627
28293031  

About Me

Worrying works! 90% of the things I worry about never happen.

    

Recent Photo

Subscribe

Technorati
Bloglines

Message Board

Yeast Infection Treatment:

I think the things you covered through the post are quiet impressive, good job and great efforts. I found it very interesting and enjoyed reading all of it …keep it up, lovely job.

tinnitus treatment:

thanks You sure do know what your’e talking about. Man, this blog is just great! I cant wait to read more of what youv’e got to say. Im really happy that I came across this when I did because I was really starting

oooo:

cool~

support:

Congratulations, you’ve just completed the installation of this shoutbox.

support:

Hi! Your shoutbox is working fine!

Leave a message ▼