?
Solved

InitiateSystemShutdown on a Remote Computer

Posted on 1997-11-21
3
Medium Priority
?
566 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
[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
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

719 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