Solved

To shut down the machine

Posted on 2003-11-11
11
444 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
  • 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
repeatSeparator  java  challenge 13 58
deburging in oracle form 12 105
Sed question 2 101
How do I get Window Title of all opened process? 4 13
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

861 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