954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to shutdown your computer using C++?

I know the dos command is "shutdown" then with your parameters of "-s -t xxx" etc.

Anyways so "How to shutdown your computer using C++?"

Thanks, Regards X

OmniX
Practically a Master Poster
656 posts since Dec 2007
Reputation Points: 31
Solved Threads: 10
 

Try system().

nucleon
Posting Pro in Training
478 posts since Oct 2008
Reputation Points: 163
Solved Threads: 91
 
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Im a newb to C++, what paramters need to be used with the functions and do they require an include file? possible small example?

Thanks, Regards X

OmniX
Practically a Master Poster
656 posts since Dec 2007
Reputation Points: 31
Solved Threads: 10
 

Something like this

#include <iostream>
using namespace std;

int main ()
{

system("shut down -s -t 10");
return 0; 

}


Shuts it down in ten seconds this is using dev c++

power_computer
Junior Poster
148 posts since Feb 2009
Reputation Points: 30
Solved Threads: 3
 

It is "shutdown" no space but even then didnt work, are you 100% sure it worked with yours?

OmniX
Practically a Master Poster
656 posts since Dec 2007
Reputation Points: 31
Solved Threads: 10
 

Firstly, don't use sysrtem. use the function given to you by AncientDragon.

Secondly for an example, read the page it has a link to one.
Thirdly, that page tells you what parameters are needed, what they could be and what they do. Also it tells you what headers you need etc.

Please read information when someone points you to it, they don't do it for fun. They do it to try and help.

Chris

Freaky_Chris
Master Poster
702 posts since Apr 2008
Reputation Points: 325
Solved Threads: 118
 

Ok thankyou Chris for that abusive and useless post. (If I didnt need help I wouldnt have posted)

If someone can give me an example using ExitWindowsEx(), like power_computer tried to give me one using system("").

My attemps have included:

ExitWindowsEx(EWX_POWEROFF, SHTDN_REASON_MAJOR_OTHER);
 // AND
system("shutdown -s -t 180");


So any ideas?

Thanks, Regards X
Thanks

OmniX
Practically a Master Poster
656 posts since Dec 2007
Reputation Points: 31
Solved Threads: 10
 

Delete line 3 (system(...) because its not needed. Otherwise, your line #1 looks ok to me, as long as you are running as administrator account.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Ya they were just my attemps nothing works, this is my whole code below.
I am assuming that my ExitWindowsEx line is fine? (Whoops my internet is lagging didnt see ur second line)
Am I missing include files? Variables? Etc? I have vista also.
So what looks to be the problem?

// Include Files
#include <iomanip>
#include <iostream>
#include <stdio.h>
#include <time.h>
#include <windows.h>
using namespace std;

int main () {
    // Shutdown
    cout << "Shutdown Complete!" << endl;  
ExitWindowsEx(EWX_POWEROFF, SHTDN_REASON_MAJOR_OTHER);
    //system("shutdown.exe -s -t 180");

    // Return Carriage
    return 0;
}


Thanks, Regards X

OmniX
Practically a Master Poster
656 posts since Dec 2007
Reputation Points: 31
Solved Threads: 10
 
So what looks to be the problem?

There might be many reasons, e.g. any application you have open may also abort the shutdown (since you are not forcing it), maybe your system does not support the power-off feature or your program lacks the shutdown privilege etc.

To get a bit more clue about what is going wrong, check which error code GetLastError() returns immediately after the ExitWindowsEx() call ... I.e.

if( ! ExitWindowsEx(...))
{
    // maybe lastError will tell something useful ...
    DWORD lastError = GetLastError();
}


Note that even if ExitWindowsEx() returns non-zero, it does not necessarily mean that the shutdown will eventually occur. (Read the MSDN documentation carefully and (try to) understand it.)

You might also try the How to Shut Down the System sample function (it shows how to enable the shutdown privilege among other things).

mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
 

tip here : ( if you on the VSC++ 6.0)
You need to implement the error handling code , and it is a must.

However this is a debugging tip
Just hit the @err in the watch window to see what's the GetLastError.

NicAx64
Posting Pro
536 posts since Mar 2009
Reputation Points: 86
Solved Threads: 44
 

Btw that command wont work on vista i dont think

jbennet
Moderator
Moderator
18,523 posts since Apr 2005
Reputation Points: 1,826
Solved Threads: 601
 

Tried to implement your code mitrmkar but still no avial.
I am using codeblocks and it worked for several other previous projects but havent used it in 6 months.

Thanks NicAx64, ill keep that noted.

Didnt think this would be such a complex project :(

OmniX
Practically a Master Poster
656 posts since Dec 2007
Reputation Points: 31
Solved Threads: 10
 

how about trying this

#include <Windows.h>

BOOL MySystemShutdown()
{
	HANDLE hToken; 
	TOKEN_PRIVILEGES tkp; 

	// Get a token for this process. 

	if (!OpenProcessToken(GetCurrentProcess(), 
		TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) 
		return( FALSE ); 

	// 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); 

	if (GetLastError() != ERROR_SUCCESS) 
		return FALSE; 

	// Shut down the system and force all applications to close. 

	if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 
		SHTDN_REASON_MAJOR_OPERATINGSYSTEM |
		SHTDN_REASON_MINOR_UPGRADE |
		SHTDN_REASON_FLAG_PLANNED)) 
		return FALSE; 

	return TRUE;
}
int _tmain(int argc, _TCHAR* argv[])
{
	MySystemShutdown();
	return 0;
}
dubeyprateek
Junior Poster
176 posts since Mar 2006
Reputation Points: 39
Solved Threads: 24
 

jbennet - The ExitWindowsEx() wont work in vista?

dubeyprateek - You just thrown the MSDN example into a function so it runs? Ill check if this works but it sorta cheating but I guess this is something to work off if it does.

But begs the question if it works, why didnt my code work?

Ill let you know how it goes... brb

Well wont even compile correctly I didnt notice before
but each time I complie both files there is a big red bar
near the ExitWindowsEx() Parameters.

This means not including the correct files?

Note: I found this on another thread, it useful?

using System.Diagnostics;

ProcessStartInfo startinfo = new ProcessStartInfo("shutdown.exe","-r");
Process.Start(startinfo);
OmniX
Practically a Master Poster
656 posts since Dec 2007
Reputation Points: 31
Solved Threads: 10
 

1) Your code lacks the required privilage. You would need Shutdown Privilage enabled for the process which initiates the shutdown.
2) On server OS not everyone has shutdown privilage, you need to make sure that you are Admin if you are using server version.
3) This MSDN example is complete.

dubeyprateek
Junior Poster
176 posts since Mar 2006
Reputation Points: 39
Solved Threads: 24
 

Posted what just happened but then shouldnt I not be able to run shutdown.exe if i didnt have the required access?

I really think this is a vista problem? Anyone else using vista?

OmniX
Practically a Master Poster
656 posts since Dec 2007
Reputation Points: 31
Solved Threads: 10
 

Shutdown.exe uses InitiateSystemShutdownEx.

I have used this code on Vista and it works for me.

dubeyprateek
Junior Poster
176 posts since Mar 2006
Reputation Points: 39
Solved Threads: 24
 
Btw that command wont work on vista i dont think


Nothing in the MSDN documentation on ExitWindowsEx() indicates that. How did you came into that conclusion?

mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You