943,600 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 14192
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Mar 20th, 2009
0

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

Quote ...
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.
C++ Syntax (Toggle Plain Text)
  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).
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Mar 20th, 2009
0

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

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.
Reputation Points: 86
Solved Threads: 43
Posting Pro
NicAx64 is offline Offline
532 posts
since Mar 2009
Mar 20th, 2009
-7

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

Btw that command wont work on vista i dont think
Moderator
Featured Poster
Reputation Points: 1764
Solved Threads: 574
Moderator
jbennet is offline Offline
16,485 posts
since Apr 2005
Mar 20th, 2009
0

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

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
Reputation Points: 31
Solved Threads: 10
Practically a Master Poster
OmniX is offline Offline
652 posts
since Dec 2007
Mar 20th, 2009
0

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

how about trying this

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 39
Solved Threads: 24
Junior Poster
dubeyprateek is offline Offline
176 posts
since Mar 2006
Mar 20th, 2009
0

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

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?
cpp Syntax (Toggle Plain Text)
  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.
Reputation Points: 31
Solved Threads: 10
Practically a Master Poster
OmniX is offline Offline
652 posts
since Dec 2007
Mar 20th, 2009
0

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

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.
Reputation Points: 39
Solved Threads: 24
Junior Poster
dubeyprateek is offline Offline
176 posts
since Mar 2006
Mar 20th, 2009
0

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

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?
Reputation Points: 31
Solved Threads: 10
Practically a Master Poster
OmniX is offline Offline
652 posts
since Dec 2007
Mar 20th, 2009
0

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

Shutdown.exe uses InitiateSystemShutdownEx.

I have used this code on Vista and it works for me.
Reputation Points: 39
Solved Threads: 24
Junior Poster
dubeyprateek is offline Offline
176 posts
since Mar 2006
Mar 20th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by jbennet ...
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?
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Improvements to Quicksort using the Median of 3 partition and Insertion sort
Next Thread in C++ Forum Timeline: no matching function call to for_each





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC