Solved

InitiateSystemShutdown on a Remote Computer

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

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
Generic progress indicator 6 117
sumNumber challenge 16 139
while loop over for loop 7 115
Path of Workbook 3 86
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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…
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

839 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