打印

[求助] 如何设置自动关机?

0
shutdown.exe 有时候不灵,好像用户切换出来时就不行,我就遇到过,后来是自己写个程序实现的,可能对你有用,有兴趣可以看看:
#include <stdio.h>
#include <time.h>
#include <windows.h>

#define EWX_FORCEIFHUNG 16

int main()
{
        int day,hour,minute;
        char reboot=0;
        char buf[256];

        SYSTEMTIME CurTime;

        HANDLE hToken;
        TOKEN_PRIVILEGES tkp;

        if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &hToken))
    {
        LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
        tkp.PrivilegeCount = 1;
        tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
        AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
    }
        else
        {
                return 0;
        }

        printf("Day Of Week:");
        scanf("%d",&day);
        printf("Hour:");
        scanf("%d",&hour);
        printf("Minute:");
        scanf("%d",&minute);
        gets(buf);
        printf("Reboot?");
        scanf("%c",&reboot);
        if(reboot == 'y')
        {
                printf("reboot at %d %02d:%02d\n",day,hour,minute);
        }
        else
        {
                printf("shutdown at %d %02d:%02d\n",day,hour,minute);
        }
       
        while(1)
        {
                GetLocalTime(&CurTime);
                if(day == 0)
                {
                        CurTime.wDayOfWeek = day;
                }
                if((CurTime.wDayOfWeek == day)&&(CurTime.wHour == hour)&&(CurTime.wMinute == minute))
                {
                        printf("Reached!\n");
                        if(reboot == 'y')
                        {
                                ExitWindowsEx(EWX_FORCE | EWX_REBOOT | EWX_FORCEIFHUNG, 0);
                        }
                        else
                        {
                                ExitWindowsEx(EWX_FORCE | EWX_SHUTDOWN | EWX_POWEROFF | EWX_FORCEIFHUNG, 0);
                        }
                       
                        return 0;
                }
                Sleep(1000);
        }
        return 0;
}

TOP

当前时区 GMT+8, 现在时间是 2025-3-13 17:00