windows 有没有关闭计算机的C++函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/846576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a C++ function to turn off the computer?
提问by setzamora
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++?
有没有关闭计算机的C++函数?因为我怀疑有一个(至少在标准库中),我可以从 C++ 调用的 windows 函数是什么?
Basically, what is the code to turn off a windows xp computer in c++?
基本上,在 C++ 中关闭 windows xp 计算机的代码是什么?
回答by John Boker
On windows you can use the ExitWindows function described here:
在 Windows 上,您可以使用此处描述的 ExitWindows 函数:
http://msdn.microsoft.com/en-us/library/aa376868(VS.85).aspx
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
http://msdn.microsoft.com/en-us/library/aa376871(VS.85).aspx
回答by paxdiablo
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.
它是 Win32 API 的一部分,而不是标准 C++,但这是因为 C++ 没有提供直接执行此操作的方法。
回答by setzamora
You can shutdown by utilizing the system() function.
您可以使用 system() 函数关闭。
for Windows
适用于 Windows
system("shutdown -s");
for Linux
适用于 Linux
system("poweroff");
or
或者
system("init 0");
回答by Scott Wisniewski
You can do this in Windows, by calling the ExitWindowsEx
function.
您可以在 Windows 中通过调用该ExitWindowsEx
函数来执行此操作。
回答by Muhammad Muneeb
yes! for Windows XP:
是的!对于 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
对于 Windows 7
system("C:\WINDOWS\System32\shutdown /s");
For Linux
对于 Linux
system("shutdown -P now");