| | |
How to shutdown your computer using C++?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2007
Posts: 980
Reputation:
Solved Threads: 210
•
•
•
•
So what looks to be the problem?
To get a bit more clue about what is going wrong, check which error code GetLastError() returns immediately after the ExitWindowsEx() call ... I.e.
C++ Syntax (Toggle Plain Text)
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).
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
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
"You never stop learning." - OmniX
how about trying this
C++ Syntax (Toggle Plain Text)
#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; }
I know I am. Therefore I am.
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?
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?
cpp Syntax (Toggle Plain Text)
using System.Diagnostics; ProcessStartInfo startinfo = new ProcessStartInfo("shutdown.exe","-r"); Process.Start(startinfo);
Last edited by OmniX; Mar 20th, 2009 at 11:56 am.
"You never stop learning." - OmniX
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.
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.
I know I am. Therefore I am.
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?
I really think this is a vista problem? Anyone else using vista?
"You never stop learning." - OmniX
![]() |
Similar Threads
- Remote Computer Shutdown (Windows NT / 2000 / XP)
- AUTO SHUTDOWN Windows XP Pro? (Windows NT / 2000 / XP)
- shutdown.exe (Question) (C#)
- Creating Desktop Shutdown Icon (Windows NT / 2000 / XP)
- Installing a new card (PCI), computer does not do anything when card is in (Monitors, Displays and Video Cards)
- Shutdown Trouble in Windows 98 (Windows 95 / 98 / Me)
- Forgot Password Cant Get Into Computer (Windows NT / 2000 / XP)
- Computer freezes on logoff (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: Improvements to Quicksort using the Median of 3 partition and Insertion sort
- Next Thread: no matching function call to for_each
Views: 3769 | Replies: 38
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll dynamic encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort stream string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






