Thursday, March 29, 2012

Just shut it down... shut it all down! Okay, well maybe just PInvoke your way to Remote Machine Shutdown/Restart with this code sample

Microsoft Developer Network - Samples - Shut down a computer using .NET (CSShutdownComputer)

"The application demonstrates how to shut down a computer using .net.

Introduction

The application demonstrates how to shut down a computer using .net. I’ve pinvoked the native API’s to get this going. Following are the API’s implemented…

1. ExitWindowsEx

2. InitiateShutdown

3. InitiateSystemShutdownEx

4. AbortSystemShutdown

Running the Sample

I’ve provided links which points towards the documentation in the application itself. Select any of the four radios to activate corresponding API. Fill in the options as per the documentation. Also note that I haven’t provided much validation since I would like the user to invoke the API as would like to, no restrictions. Read the documentation carefully.

Also note that remote shutdown will only work if the remote computer has remote shutdown enabled. Usesecpol.msc to enable this feature for the remote computer.

InitiateShutdown and InitiateSystemShutdownEx will end up showing a message box before shutting down the computer. Once the timeout expires the system automatically shutdowns. If you want to prevent this shutdown invoke “AbortSystemShutdown”.

This is how the application looks like…

image001

SNAGHTML87eb2b..."

I've been using these PInvoke calls for a number of years now, but I still learned some things from this sample. More importantly I really like the WinForm sample driver, with the Documentation and PInvoke information links. That's a really great touch...

 

Related Past Post XRef:
Daddy, tell me a story of how VB, WP7, Azure and my photos can be combined in the cloud to create a video story...
All-In-One Code Framework Sample Browser v4 (aka Metro version) now Visual Studio Extension'd (Don't leave your Visual Studio without it...)
The All-In-One Code Framework Browser v4 goes major Metro
All-In-One Code Framework is now on MSDN Code Gallery, has a new sample browser and is cooler than ever
All-In-One Code Framework, your one stop shop for Windows code samples, examples and guidelines
All-In-One Knowledge Base Articles (aka another way to see all the coolness that’s in the All-In-One sample code base)
Three languages, tons of technologies, continual additions, all source: The Microsoft All-In-One Code Framework (think “An Official Boat Load of Code Samples!”) (Added bonus “How can I on earth write a managed shell extension” with .Net 4 sample)

No comments: