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
Ancient Dragon
Retired & Loving It
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
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
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
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
Btw that command wont work on vista i dont think
jbennet
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
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
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
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