简体   繁体   English

如何使用cpp程序强制关闭计算机

[英]how to force shutdown computer using cpp program

i have been working on a small project.我一直在做一个小项目。 i am making a timer for my little brother's PC so that he will not be able to use computer more than set time by me in a day.我正在为我弟弟的电脑做一个计时器,这样他一天使用电脑的时间就不会超过我设定的时间。 The problem I am facing in my code is I have tried bunch of system commands to shut down computer automatically but when it runs the command it asks to wether close the running applications.我在代码中面临的问题是我尝试了一堆系统命令来自动关闭计算机,但是当它运行命令时,它要求关闭正在运行的应用程序。 but I want to force shut down all running apps.但我想强制关闭所有正在运行的应用程序。 below is the code i am trying but it is not quite working out下面是我正在尝试的代码,但它不是很有效

system("C:\\Windows\\System32\\shutdown /s /t 0");

now this statement, when it is executed, asks for closing applications which are running or cancel shut down process现在该语句在执行时要求关闭正在运行的应用程序或取消关闭过程

ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE,
    SHTDN_REASON_MAJOR_OPERATINGSYSTEM |
    SHTDN_REASON_MINOR_UPGRADE |
    SHTDN_REASON_FLAG_PLANNED);

i got this above function from Microsoft docs but it doesn't work in my computer even it doesn't show any error in this statement.我从 Microsoft 文档中得到了高于 function 的信息,但它在我的计算机中不起作用,即使它在此语句中没有显示任何错误。 but even after executing it doesn't work.但即使在执行后它也不起作用。 moreover visual studio offered its own suggestion of using a new API which is given below此外,Visual Studio 提出了使用新的 API 的建议,如下所示

InitiateSystemShutdownEx(NULL, NULL, 0, true, false, SHTDN_REASON_FLAG_USER_DEFINED);

this also doesn't work even if gets execute.即使被执行,这也不起作用。 i am using windows 11. and below i am giving the whole code where i reached yet我正在使用 windows 11。下面我给出了我到达的整个代码

#include <iostream>
#include <stdlib.h>
#include<Windows.h>
#include<fstream>
#pragma warning(disable : 4996)
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "advapi32.lib")
using namespace std;


void timer()
{
    int hours=0;
    int minutes=0;
    int seconds=0;
    ofstream tim;
    while (seconds != 5)
    {
        tim.open("time.txt", ios::out | ios::trunc);
        seconds++;
        Sleep(1000);
        if (seconds == 60)
        {
            minutes++;
            seconds = 0;
        }
        if (minutes == 60)
        {
            hours++;
            minutes = 0;
        }
        tim << hours << endl << minutes << endl << seconds;
        tim.close();
    }
}

int main()
{
    timer();
    ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE,
        SHTDN_REASON_MAJOR_OPERATINGSYSTEM |
        SHTDN_REASON_MINOR_UPGRADE |
        SHTDN_REASON_FLAG_PLANNED);
    cout << endl << endl;
    return 0;
}

so please just any type of help would be appreciated.所以请任何类型的帮助将不胜感激。

You need to add the /f flag to force a shutdown.您需要添加/f标志来强制关机。

Here's the actual logoff function I created many years ago when faced with a similar problem.这是我多年前在遇到类似问题时创建的实际注销 function。 There's a lot of extra code there to support debugging without logging myself out, the actual work is in the call to ExitWindowsEx .那里有很多额外的代码来支持调试而无需注销自己,实际工作是调用ExitWindowsEx

void CTimeLimitApp::Logoff()
{
    static bool bOff = false;
    if (bOff)
        return;
    bOff = true;
#if _DEBUG
    AfxMessageBox("Logoff!", MB_OK, 0);
    PostQuitMessage(0);
#else
    Sleep(1000);
    ExitWindowsEx(EWX_LOGOFF | EWX_FORCE, 0);
#endif
}

You may need to run the program as Administrator if you want to do a full shutdown.如果要完全关闭,您可能需要以管理员身份运行程序。

yet this solution worked for me and will also work for others hopefully but there is also better way explain by @CodyGray in comments...i prefer looking it out too....that is well explained and very useful但是这个解决方案对我有用,也希望对其他人有用,但@CodyGray 在评论中也有更好的解释方法......我也更喜欢看出来......这很好解释并且非常有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM