Solved

InitiateSystemShutdown on a Remote Computer

Posted on 1997-11-21
3
527 Views
Last Modified: 2013-11-20
How do I programatically call InitiateSystemShutdown to shut down/reboot a remote computer/all computers on a network ? I know how to do it locally by assigning the Process Token the SE_SHUTDOWN_NAME privilege, but I don't know how to do it remotely (SE_REMOTE_SHUTDOWN_NAME).

Thanx,
/Stefan
0
Comment
Question by:stefanr
3 Comments
 
LVL 32

Accepted Solution

by:
jhance earned 200 total points
ID: 1310443
HANDLE hToken;              // handle to process token
TOKEN_PRIVILEGES tkp;       // pointer to token structure  
BOOL fResult;               // system shutdown flag  

// Get the current process token handle so we can get shutdown // privilege.  
if (!OpenProcessToken(GetCurrentProcess(),
    TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
    ErrorHandler("OpenProcessToken failed.");  

// Get the LUID for shutdown privilege.  
LookupPrivilegeValue(NULL, SE_REMOTE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);  
tkp.PrivilegeCount = 1;  // one privilege to set    
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;  

// Get shutdown privilege for this process.  
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,     (PTOKEN_PRIVILEGES) NULL, 0);

 // Cannot test the return value of AdjustTokenPrivileges.  
if (GetLastError() != ERROR_SUCCESS)
    ErrorHandler("AdjustTokenPrivileges enable failed.");  

// Display the shutdown dialog box and start the time-out countdown.  
fResult = InitiateSystemShutdown(
    lpszComputer,                                  // shut down remote computer
    "Click on the main window and press \
     the Escape key to cancel shutdown.",  // message to user
    20,                                    // time-out period
    FALSE,                                 // ask user to close apps
    TRUE);                                 // reboot after shutdown  
if (!fResult) {
     ErrorHandler("InitiateSystemShutdown failed.");
}  

// Disable shutdown privilege.  tkp.Privileges[0].Attributes = 0;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0);
if (GetLastError() != ERROR_SUCCESS) {
 ErrorHandler("AdjustTokenPrivileges disable failed.");
}  
}
0
 
LVL 3

Author Comment

by:stefanr
ID: 1310444
Thank you for the answer. I tried that in my first attempt to do it, but it didn't work. However I discovered the answer myself later, and it was so simple. Just make two calls to enable both the SE_SHUTDOWN_NAME and SE_REMOTE_SHUTDOWN_NAME privileges, and it just works fine (as long as I am member of the Administrator group).

May the Source be with you !

/Stefan
0
 

Expert Comment

by:haider
ID: 4568909
stefanr:
I m doing the same thing but no success and I m getting error message access denied, Will you please give me code which you modify.

Thanks!
haider_haidee@hotmail.com
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now