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
Try system().
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
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++
It is "shutdown" no space but even then didnt work, are you 100% sure it worked with yours?
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
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
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.
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
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).
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.
Btw that command wont work on vista i dont think
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 :(
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;
}
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);
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.
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?
Shutdown.exe uses InitiateSystemShutdownEx.
I have used this code on Vista and it works for me.
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?
There are some registry values in the windows that stops shutdown
check this out also. Windows is a flat system that harder to debug.
Btw that command wont work on vista i dont think
MSDN says if you have a windows version 2000 above , this will works.Read the MSDN. Anyway if you refering about that "shutdown" shell command then if it's not working in the windows shell then it will surely not working inside your program too.
That may be a registry error.
This may be a user priviledge problem ,
But there should be a reason , so we are going to debug it.
Oky what is the error code ? Then just open the Error Lookup tool
in VS and submit that value and check it.
There is a error but there should be a something reason. Hopefully
there can be slove exists for that problem. ( anyway not every problem have an answer).
Nothing in the MSDN documentation on ExitWindowsEx() indicates that. How did you came into that conclusion?
Agreed !
That way of thinking is bad for your debugging skills.
if( ! ExitWindowsEx(...))
{
// maybe lastError will tell something useful ...
DWORD lastError = GetLastError();
}
// Toggle a debug flag on this line //
and rebuild and press GO , then the debugger will stop in this
line. Then you can see what's going on.
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?
It is not a Vista problem. Vista uses same APIs to shutdown the system.
You certainly have required permissions and privileges if you can shutdown the system by Shutdown.exe
You just need to be more careful that you need to *enable* shutdown privilege before you call ExitWindowEx API. By default, this privilege is disabled for all users. The code on MSDN enable this privilege and calls the API.
2 prerequisites
a) You must have the shutdown privilege (It is not 'permission' it is called 'privilege')
b) You must have this privilege enabled.
Many times this privilege is present but not enabled. You would need additional programming effort to enable it.
Also, you can try turning off the UAC and running your application to see if that makes any difference to you.
Off the records::
If that doesnot work, open a support incident with Microsoft I will have a dedicated engineer work with you. If it is idenified as a Vista problem Microsoft will not charge you!
I know that to shutdown the system using the NET Framework, you must change your application so that it asks for an elevation of privileges on execution, or else it wont work on vista.
So i assume you must do the same in this case
try using system("shutdown -s -f -t 00");
Its not the best solution, but whether it works or not, it will be informative.
try using system("shutdown -s -f -t 00");
Its not the best solution, but whether it works or not, it will be informative.
Yes -- I just tested it on my Vista Home and it works perfectly.
I have seen it not work on some machines. In high school i tried it on the school PCs and it wouldn't work (I messed with my friends in that class all the time). So if OminiX can't get that to work his user account may not have the necessary permissions.
If it does work for OminiX then the other suggestions may not have worked because of something in the registry blocking shutdowns. If that is the issue, another (better) solution could be implemented.
I think the user will have to be logged in under an Administrative account for that to work. In school users probably don't have those permissions, and there is nothing you can do about it short of getting the instructor to let you log in as Administrator (and that's highly unlikely)
Yes that's true. That's pretty much what I was trying to say. If it doesn't work its most likely because you need admin privileges.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.