有没有 C++ 关闭电脑的功能?

发布于 2024-07-18 15:46:22 字数 120 浏览 4 评论 0原文

C++ 有没有关闭计算机的函数? 因为我怀疑是否有一个(至少在标准库中),所以我可以从 C++ 调用的 Windows 函数是什么?

基本上,用 C++ 关闭 Windows XP 计算机的代码是什么?

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++?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

英雄似剑 2024-07-25 15:46:22

在 Windows 上,您可以使用此处描述的 ExitWindows 函数:

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

下面是执行此操作的示例代码的链接:

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

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

葬シ愛 2024-07-25 15:46:22

使用以下命令,假设您有权限):

ExitWindowsEx (EWX_POWEROFF | EWX_FORCEIFHUNG,
    SHTDN_REASON_MINOR_OTHER);

这将导致电源关闭,同时给应用程序一个关闭的机会(如果它们花费太长时间,它们无论如何都会被终止)。

它是 Win32 API 的一部分,而不是标准 C++ 的一部分,但这是因为 C++ 没有提供直接执行此操作的方法。

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.

无语# 2024-07-25 15:46:22

您可以使用 system() 函数关闭。

对于 Windows

system("shutdown -s");

对于 Linux

system("poweroff");

system("init 0");

You can shutdown by utilizing the system() function.

for Windows

system("shutdown -s");

for Linux

system("poweroff");

or

system("init 0");
凯凯我们等你回来 2024-07-25 15:46:22

您可以在 Windows 中通过调用 来执行此操作ExitWindowsEx 函数。

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

分开我的手 2024-07-25 15:46:22

是的!
对于 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;
}

对于 Windows 7

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

对于 Linux

system("shutdown -P now");

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");
青巷忧颜 2024-07-25 15:46:22

[ FOR WINDOWS ]

其他解决方案都不适合我,我想关闭我的自定义窗口,如果没有资源管理器,此代码根本无法工作。

重要的注意事项是超时时间,因此这是Windows的真正解决方案:

GenericFunction(void, Shutdown)()
{
    WinExec("shutdown -s -t 0", SW_HIDE);
    Sleep(500); // Works without this but it's safer to use sleep
    KillProcessTree("winlogon"); // Internal process killer you can use pskill64
    // WinExec("pskill64 winlogon -t -nobanner /accepteula", SW_HIDE);
    exit(-10); // Planned Shutdown Code
}

[ FOR WINDOWS ]

None of other solutions worked for me, I wanted to shutdown my customized windows and without explorer this codes simply don't work.

The important note is timeout time, so here's the real solution for windows :

GenericFunction(void, Shutdown)()
{
    WinExec("shutdown -s -t 0", SW_HIDE);
    Sleep(500); // Works without this but it's safer to use sleep
    KillProcessTree("winlogon"); // Internal process killer you can use pskill64
    // WinExec("pskill64 winlogon -t -nobanner /accepteula", SW_HIDE);
    exit(-10); // Planned Shutdown Code
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文