How to shutdown your computer using C++?

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2007
Posts: 981
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 210
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: How to shutdown your computer using C++?

 
0
  #11
Mar 20th, 2009
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.
  1. if( ! ExitWindowsEx(...))
  2. {
  3. // maybe lastError will tell something useful ...
  4. DWORD lastError = GetLastError();
  5. }

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).
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 326
Reputation: NicAx64 will become famous soon enough NicAx64 will become famous soon enough 
Solved Threads: 19
NicAx64's Avatar
NicAx64 NicAx64 is offline Offline
Posting Whiz

Re: How to shutdown your computer using C++?

 
0
  #12
Mar 20th, 2009
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.
Last edited by NicAx64; Mar 20th, 2009 at 11:32 am.
Nothing like a kernel pannic !
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,273
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 543
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator

Re: How to shutdown your computer using C++?

 
-7
  #13
Mar 20th, 2009
Btw that command wont work on vista i dont think
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 625
Reputation: OmniX is an unknown quantity at this point 
Solved Threads: 10
OmniX's Avatar
OmniX OmniX is offline Offline
Practically a Master Poster

Re: How to shutdown your computer using C++?

 
0
  #14
Mar 20th, 2009
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
"You never stop learning." - OmniX
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 176
Reputation: dubeyprateek is an unknown quantity at this point 
Solved Threads: 22
dubeyprateek's Avatar
dubeyprateek dubeyprateek is offline Offline
Junior Poster

Re: How to shutdown your computer using C++?

 
0
  #15
Mar 20th, 2009
how about trying this

  1. #include <Windows.h>
  2.  
  3. BOOL MySystemShutdown()
  4. {
  5. HANDLE hToken;
  6. TOKEN_PRIVILEGES tkp;
  7.  
  8. // Get a token for this process.
  9.  
  10. if (!OpenProcessToken(GetCurrentProcess(),
  11. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
  12. return( FALSE );
  13.  
  14. // Get the LUID for the shutdown privilege.
  15.  
  16. LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
  17. &tkp.Privileges[0].Luid);
  18.  
  19. tkp.PrivilegeCount = 1; // one privilege to set
  20. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  21.  
  22. // Get the shutdown privilege for this process.
  23.  
  24. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
  25. (PTOKEN_PRIVILEGES)NULL, 0);
  26.  
  27. if (GetLastError() != ERROR_SUCCESS)
  28. return FALSE;
  29.  
  30. // Shut down the system and force all applications to close.
  31.  
  32. if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE,
  33. SHTDN_REASON_MAJOR_OPERATINGSYSTEM |
  34. SHTDN_REASON_MINOR_UPGRADE |
  35. SHTDN_REASON_FLAG_PLANNED))
  36. return FALSE;
  37.  
  38. return TRUE;
  39. }
  40. int _tmain(int argc, _TCHAR* argv[])
  41. {
  42. MySystemShutdown();
  43. return 0;
  44. }
I know I am. Therefore I am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 625
Reputation: OmniX is an unknown quantity at this point 
Solved Threads: 10
OmniX's Avatar
OmniX OmniX is offline Offline
Practically a Master Poster

Re: How to shutdown your computer using C++?

 
0
  #16
Mar 20th, 2009
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?
  1. using System.Diagnostics;
  2.  
  3. ProcessStartInfo startinfo = new ProcessStartInfo("shutdown.exe","-r");
  4. Process.Start(startinfo);
Last edited by OmniX; Mar 20th, 2009 at 11:56 am.
"You never stop learning." - OmniX
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 176
Reputation: dubeyprateek is an unknown quantity at this point 
Solved Threads: 22
dubeyprateek's Avatar
dubeyprateek dubeyprateek is offline Offline
Junior Poster

Re: How to shutdown your computer using C++?

 
0
  #17
Mar 20th, 2009
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.
I know I am. Therefore I am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 625
Reputation: OmniX is an unknown quantity at this point 
Solved Threads: 10
OmniX's Avatar
OmniX OmniX is offline Offline
Practically a Master Poster

Re: How to shutdown your computer using C++?

 
0
  #18
Mar 20th, 2009
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?
"You never stop learning." - OmniX
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 176
Reputation: dubeyprateek is an unknown quantity at this point 
Solved Threads: 22
dubeyprateek's Avatar
dubeyprateek dubeyprateek is offline Offline
Junior Poster

Re: How to shutdown your computer using C++?

 
0
  #19
Mar 20th, 2009
Shutdown.exe uses InitiateSystemShutdownEx.

I have used this code on Vista and it works for me.
I know I am. Therefore I am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 981
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 210
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: How to shutdown your computer using C++?

 
0
  #20
Mar 20th, 2009
Originally Posted by jbennet View Post
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?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 3786 | Replies: 38
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC