Click here to Skip to main content
11,481,404 members (61,729 online)
Click here to Skip to main content

Control Windows System

, 25 Jun 2007 CPOL 22.8K 421 29
Rate this:
Please Sign up or sign in to vote.
Control your system by shutting down, rebooting, log-off, hibernate
Screenshot - Demo_Img1.jpg


Sometimes, you need to control your system from your application software. For example, you may lock the computer, log-off, reboot, shutdown, etc.

Here I am going to discuss how these things can be done in .NET.

Note: You have to use un-managed code to perform this application.

Using the Code

You have to first add the System.Runtime.InteropServices using directive into your project.

In order to lock the system, you need to call the LockWorkStation() method which is in user32.dll.

First, you need to import user32.dll before calling.

Here is the code for importing user32.dll. Define the function as shown below:

public static extern void LockWorkStation();

Stand By and Hibernation

Call SetSuspendState method of Application class.

SetSuspendState method accepts 3 arguments.

The first argument power sets either standby or hibernate, the second argument if set to true, forces the OS to suspend all its applications. The third parameter, if set to true, disables wake events.

Below is the code for the application to stand by:

Application.SetSuspendState(PowerState.Suspend true, true);

For the application to Hibernate, use the following code:

Application.SetSuspendState(PowerState.Hibernate, true, true);

Log-off the Application

In order to log off, you need to first import user32.dll and define the function which you need to call to log off.

public static extern int ExitWindowsEx(int uFlags, int dwReason);

Call the ExitWindowsEx function with 2 arguments as 0, Use the following line to log off:

ExitWindowsEx(0, 0);

Rebooting the System

We use the same function ExitWindowsEx but now with different parameters:

ExitWindowsEx(2, 0);

Shutdown System

Call ExitWindowsEx with 1 and 0 as arguments for shutting down the system:

ExitWindowsEx(1, 0);

Points of Interest

I felt happy writing this code to control my system from my .NET application.


  • 25th June, 2007: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
United States United States

Around 10 years of professional software development experience in analysis, design, development, testing and implementation of enterprise web applications for healthcare domain with good exposure to object-oriented design, software architectures, design patterns, test-driven development and agile practices.

In Brief

Analyse and create High Level , Detailed Design documents.
Use UML Modelling and create Use Cases , Class Diagram , Component Model , Deployment Diagram, Sequence Diagram in HLD.

Area of Working : Dedicated to Microsoft .NET Technologies
Experience with : C# , J2EE , J2ME, Windows Phone 8, Windows Store App
Proficient in: C# , XML , XHTML, XML, HTML5, Javascript, Jquery, CSS, SQL, LINQ, EF

Software Development

Database: Microsoft SQL Server, FoxPro
Development Frameworks: Microsoft .NET 1.1, 2.0, 3.5, 4.5
UI: Windows Forms, Windows Presentation Foundation, ASP.NET Web Forms and ASP.NET MVC3, MVC4
Coding: WinForm , Web Development, Windows Phone, WinRT Programming, WCF, WebAPI

Healthcare Domain Experience

CCD, CCR, QRDA, HIE, HL7 V3, Healthcare Interoperability


B.E (Computer Science)

CodeProject Contest So Far:

1. Windows Azure Developer Contest - HealthReunion - A Windows Azure based healthcare product , link -

2. DnB Developer Contest - DNB Business Lookup and Analytics , link -

3. Intel Ultrabook Contest - Journey from development, code signing to publishing my App to Intel AppUp , link -

4. Intel App Innovation Contest 2013 - eHealthCare

5. Grand Prize Winner of CodeProject HTML5 &CSS3 Article Contest 2014

6. Grand Prize Winner of CodeProject Android Article Contest 2014

7. Grand Prize Winner of IOT on Azure Contest 2015

Comments and Discussions

Generalone thing Pin
kradlle12-Dec-07 3:23
memberkradlle12-Dec-07 3:23 
Questionone litle thing... Pin
kradlle12-Dec-07 3:23
memberkradlle12-Dec-07 3:23 
GeneralGood for a start Pin
The_Mega_ZZTer25-Jun-07 17:46
memberThe_Mega_ZZTer25-Jun-07 17:46 
GeneralRe: Good for a start Pin
Ranjan.D26-Jun-07 5:13
memberRanjan.D26-Jun-07 5:13 
GeneralRe: Good for a start Pin
MussiMan16-Jul-07 2:27
memberMussiMan16-Jul-07 2:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150520.1 | Last Updated 25 Jun 2007
Article Copyright 2007 by Ranjan.D
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid