Solved

InitiateSystemShutdown on a Remote Computer

Posted on 1997-11-21
3
534 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

17 Experts available now in Live!

Get 1:1 Help Now