Solved

To shut down the machine

Posted on 2003-11-11
11
446 Views
Last Modified: 2013-11-20
i want to shut down, log off, and restart the machine(running on windows) using the functions which i can implement inVC++

(iam able to only log off using ExitWindowsEx function)

plz help  in these pts

yateesh
0
Comment
Question by:yateesh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
11 Comments
 
LVL 9

Expert Comment

by:Priyesh
ID: 9728339
if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0))
      return FALSE;
0
 
LVL 32

Accepted Solution

by:
jhance earned 20 total points
ID: 9730440
Note that the code fragment above is simpleminded.  It will work ONLY on Win9x/ME.  It will fail on NT4/2000/XP due to lack of proper privilege by the calling process.

Please see the Microsoft example here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/shutting_down.asp

It shows you exactly how to do this.  Indeed it's more complex than the above sample but having the SE_SHUTDOWN_NAME privilege is critical for success.
0
 
LVL 5

Expert Comment

by:jrocnuck
ID: 9734003
     HANDLE                        hToken;
      TOKEN_PRIVILEGES      tkp;
      BOOL                        bOpenProcessTokenGood;
      
      bOpenProcessTokenGood = OpenProcessToken(GetCurrentProcess(), (TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY), &hToken );

      if( !bOpenProcessTokenGood )
      {      
            return;
      }
      
      //******************************************************************
      //* Get the LUID for the shutdown privilege.                       *
      //******************************************************************
      LookupPrivilegeValue( NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid );

      tkp.PrivilegeCount = 1;
      tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
      //******************************************************************
      // Set the shutdown privilege for this process.                    *
      //******************************************************************
      AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
      
      ExitWindowsEx( EWX_REBOOT, NULL );      
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Expert Comment

by:jhance
ID: 9734544
jrocnuck,

It seems you've been participating here on EE long enough to be aware of the rules...
0
 
LVL 5

Expert Comment

by:jrocnuck
ID: 9735138
which rule are you refering to?
0
 
LVL 5

Expert Comment

by:jrocnuck
ID: 9735171
or rules..
0
 
LVL 5

Expert Comment

by:jrocnuck
ID: 9735184
were you aware that using terms like "simipleminded" is not really appropriate according to the rules?
0
 
LVL 32

Expert Comment

by:jhance
ID: 9736344
Note that I said the APPROACH is simpleminded.  I did NOT indicate that the user was.  Take it up with the moderators.
0
 
LVL 5

Expert Comment

by:jrocnuck
ID: 9739790
same back at ya jack...

The term simpleminded should never be used period, it is arrogant and the wrong choice of words.  If you cared anything about the real reason this site exists, you wouldn't use words like that and say the kinds of things I've seen you say in other posts.

This site is about helping people, not belittling them or their methods or approaches and not about boosting your ego by appearing to be a genius.
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Include multiple hostnames in this command? 3 55
iSeries FTP Exit Program 8 169
dog bark java program 15 130
Not needed 13 133
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question