Click here to Skip to main content
15,884,099 members
Articles / Programming Languages / C++
Article

How we can Shutdown a system OR Restart or Log Off or Hibernate or ....

Rate me:
Please Sign up or sign in to vote.
2.27/5 (11 votes)
2 Jun 2005CPOL 51.4K   1.7K   22   12
This program is a good example for shutdown or restart or stand by or Log off and Hibernate

Sample Image - Turn_Off_Computer.gif

Introduction

How we can Shutdown a system OR Restart or Log Off or Hibernate or ....

This program is a good example for this Task.

I define Fuction as a Library , and then voice it into my program(Name  my program is :TurnOffWindows ).

according to :

Merge Library TurnOffComputer to program.

// TurnOffWindowsDlg.h : header file
//

#if !defined(AFX_TURNOFFWINDOWSDLG_H__1AA55971_5022_4C8B_9700_D0A6791694F2__INCLUDED_)
#define AFX_TURNOFFWINDOWSDLG_H__1AA55971_5022_4C8B_9700_D0A6791694F2__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

//***********

#include "TurnOffComputer.h"

//*************

 use from this Library into my program.

void CTurnOffWindowsDlg::OnButtonRun() 
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);

CTurnOffComputer MyTurnoff;

MyTurnoff.Active(m_radio);


}

 

Notice :

m_radio is a integer value .

if  value m_radio is 0 ,So your computer will Shutdown .
Too :
<PRE>    1-->Restrat
    2-->Log Off
    3-->Stand by
    4-->Hibernate

For give more information you can download files it in this first section.

License

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


Written By
Engineer ArdakanKavosh
Iran (Islamic Republic of) Iran (Islamic Republic of)
I'm from Iran, and Live in Ardakan City.
I'm Computer engineering (HardWare-Software).
I'm Programmer . (VC++ , C++ , Assembly , Basic , C# , Web Programing [ ASP,Php ])

Comments and Discussions

 
GeneralUnable to extract Pin
Anil K P4-Dec-07 22:39
Anil K P4-Dec-07 22:39 
Generalrestart Pin
jeevirajan3-Feb-07 2:26
jeevirajan3-Feb-07 2:26 
GeneralRe: restart Pin
Behzad Bahjat Manesh4-Jun-07 5:56
Behzad Bahjat Manesh4-Jun-07 5:56 
GeneralSource Code Is not Available Pin
MohammadAmiry3-Jun-05 3:58
MohammadAmiry3-Jun-05 3:58 
GeneralRe: Source Code Is not Available Pin
Behzad Bahjat Manesh3-Jun-05 23:07
Behzad Bahjat Manesh3-Jun-05 23:07 
GeneralRe: Source Code Is not Available Pin
MohammadAmiry12-Jun-05 23:28
MohammadAmiry12-Jun-05 23:28 
GeneralRe: Source Code Is not Available Pin
Douglas R. Keesler24-Jun-05 16:37
Douglas R. Keesler24-Jun-05 16:37 

Look in your MSDN Platform SDK help files in Visual Studio... Search for "SHUTTING DOWN" ... this is just basically a copy/paste of that code... In case you can't find it, here it is:

Shutting Down
You can use the ExitWindowsEx function to shut down the system. Shutting down
flushes file buffers to disk and brings the system to a condition in which it 
is safe to turn off the computer. 
                    
Windows NT: The following example enables the SE_SHUTDOWN_NAME privilege and 
then shuts down the system. 
             
HANDLE hToken; 
TOKEN_PRIVILEGES tkp; 
 
// Get a token for this process. 
 
if (!OpenProcessToken(GetCurrentProcess(), 
        TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) 
    error("OpenProcessToken"); 
 
// Get the LUID for the shutdown privilege. 
 
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, 
        &tkp.Privileges[0].Luid); 
 
tkp.PrivilegeCount = 1;  // one privilege to set    
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; 
 
// Get the 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) 
    error("AdjustTokenPrivileges"); 
 
// Shut down the system and force all applications to close. 
 
if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0)) 
    error("ExitWindowsEx"); 
 
For more information about setting security privileges, seePrivileges. 

Further, I'm not sure this justifies a separate class - it's such a small snippet of code. Practically, in a project, it's primary use would be to intitiate a restart if system settings were changed which required restart to take effect. Even then, I would either use it as a standard function, or add it (as a simple function) to a DLL containing shell/system functions.




In business, if two people always agree, one of them is unnecessary.

GeneralSome comments Pin
Geert van Horrik2-Jun-05 21:57
Geert van Horrik2-Jun-05 21:57 
GeneralRe: Some comments Pin
.rich.w2-Jun-05 22:56
.rich.w2-Jun-05 22:56 
GeneralRe: Some comments Pin
Geert van Horrik2-Jun-05 23:38
Geert van Horrik2-Jun-05 23:38 
GeneralRe: Some comments Pin
Behzad Bahjat Manesh3-Jun-05 23:32
Behzad Bahjat Manesh3-Jun-05 23:32 

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

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