Solved

InitiateSystemShutdown on a Remote Computer

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

734 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