Is there a C++ function to turn off the computer?

北城以北 提交于 2019-11-30 16:06:09

问题


Is there a C++ function to turn off the computer? And since I doubt there is one (in the standard library, at least), what's the windows function that I can call from C++?

Basically, what is the code to turn off a windows xp computer in c++?


回答1:


On windows you can use the ExitWindows function described here:

http://msdn.microsoft.com/en-us/library/aa376868(VS.85).aspx

and here's a link to example code that does this:

http://msdn.microsoft.com/en-us/library/aa376871(VS.85).aspx




回答2:


Use the following, assuming you have the privileges):

ExitWindowsEx (EWX_POWEROFF | EWX_FORCEIFHUNG,
    SHTDN_REASON_MINOR_OTHER);

This will cause power off while giving applications a chance to shut down (if they take too long, they'll be terminated anyway).

It's part of the Win32 API rather than standard C++ but that's because C++ provides no way to do this directly.




回答3:


You can shutdown by utilizing the system() function.

for Windows

system("shutdown -s");

for Linux

system("poweroff");

or

system("init 0");



回答4:


You can do this in Windows, by calling the ExitWindowsEx function.




回答5:


yes! for Windows XP:

#include <stdio.h>
#include <stdlib.h>

int main()
{
   char ch;

   printf("Do you want to shutdown your computer now (y/n)\n");
   scanf("%c", &ch);

   if (ch == 'y' || ch == 'Y')
       system("C:\\WINDOWS\\System32\\shutdown -s");
       return 0;
}

For Windows 7

system("C:\\WINDOWS\\System32\\shutdown /s");

For Linux

system("shutdown -P now");


来源:https://stackoverflow.com/questions/846576/is-there-a-c-function-to-turn-off-the-computer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!