搜索
查看: 867|回复: 2
打印 上一主题 下一主题

做了个自动关机程序!

[复制链接]
跳转到指定楼层
楼主
发表于 2016-8-24 11:02:22 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <winbase.h>
#include <Winuser.h>
#include <time.h>
#include <process.h>
#include <wincon.h>
#include <string.h>

#define BOOL int
#define TRUE 1
#define FALSE 0
int running;

BOOL MySystemShutdown();
void ThreadProc(void *param);
int SetConsoleSize(int a,int b);
BOOL access();

int shour,smin;
int main()
{
    SetConsoleSize(20,6);
    shour=16;smin=21;
    time_t a;
    struct tm *t;
    BOOL r;
    running=1;
    _beginthread( ThreadProc,0,NULL);
    printf("Shutdown at %d:%d\n",shour,smin);
    while(running)
    {
        time(&a);
        t=localtime(&a);
        if(((*t).tm_hour==shour) && (smin-(*t).tm_min==1))
        {
            Beep(1,1);
        }
        if(smin==0 && ((*t).tm_hour+1==shour)&&((*t).tm_min==59))
        {
            Beep(1,1);
        }

        if(((*t).tm_hour==shour) && ((*t).tm_min==smin))
        {
            printf("Try to shut down.\n");
            r=access();
            if(r==TRUE)
            {
                r=SetSystemPowerState(FALSE,TRUE);
                if(r==TRUE)
                {
                    printf("Sleep success.\n");
                    running=0;
                }
                else
                {
                    printf("Cann't sleep.\nTry to shutdown.\n");
                }
                ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE,0);
                running=0;
            }
            else
            {
                printf("Privilege up failed.\n");
                running=0;
            }
        }
        Sleep(6000);
    }
    return 0;
}
void ThreadProc(void *param)
{
    char dc[254];
    char ac[10];
    char bc[10];
    int a,b;
    int aa,ab,ba,bb,cc;
    while(running)
    {
        printf("You can change the time.\nEnter hour:min\n");
        //scanf("%d%c%d",&a,&c,&b);
        //printf("your enter %d:%d\n",a,b);
        scanf("%s",dc);
        cc=strlen(dc);
        if(cc>4)
        {
            int n,m;
            aa=0;ab=0;ba=0;bb=0;
            for(n=0;(dc[n]<48)||(dc[n]>57);n++)
            {;
            }
            aa=n;

            for(;(dc[n]<=57)&&(dc[n]>=48);)
            {
                ab=n;n++;;
            }

            for(;(dc[n]<48)||(dc[n]>57);n++)
            {;
            }
            ba=n;
            for(;(dc[n]<=57)&&(dc[n]>=48);)
            {
                bb=n;n++;
            }
            if((ab>aa)&&(ba>ab)&&(bb>ba))
            {
                m=0;
                for(n=aa;n<=ab;n++)
                {
                    ac[m++]=dc[n];
                }
                ac[m]='\0';
                m=0;
                for(n=ba;n<=bb;n++)
                {
                    bc[m++]=dc[n];
                }
                bc[m]='\0';
                a=atoi(ac);
                b=atoi(bc);
                if( (a>-1) && (a<24) && (b<60) && (b>-1) )
                {
                    shour=a;smin=b;
                }
            }
        }
        if(strcmpi(dc,"quit")==0){running=0;}
        if(strcmpi(dc,"exit")==0){running=0;}
        if(strcmpi(dc,"cancel")==0){running=0;}
        if(strcmpi(dc,"stop")==0){running=0;}

        /*if((dc[0]=='q')&&(dc[1]=='u')&&(dc[2]=='i')&&(dc[3]=='t')){running=0;printf("quit");}
        if((dc[0]=='e')&&(dc[1]=='x')&&(dc[2]=='i')&&(dc[3]=='t')){running=0;printf("quit");}
        if((dc[0]=='Q')&&(dc[1]=='U')&&(dc[2]=='I')&&(dc[3]=='T')){running=0;printf("quit");}*/
        system("cls");
        printf("Shutdown at %d:%d\n",shour,smin);
    }
}

BOOL access()
{
   HANDLE hToken;
   TOKEN_PRIVILEGES tkp;
   // Get a token for this process.
   if (!OpenProcessToken(GetCurrentProcess(),
        TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
      return( FALSE );
   // Get the LUID for the shutdown privilege.
   LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
        &tkp.Privileges[0].Luid);
   tkp.PrivilegeCount = 1;  // one privilege to set
   tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
   // Get the shutdown privilege for this process.
   AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
        (PTOKEN_PRIVILEGES)NULL, 0);
   if (GetLastError() != ERROR_SUCCESS)
      return FALSE;
   // Shut down the system and force all applications to close.
    return 1;
}


int SetConsoleSize(int a,int b)
{
    COORD size={a,b};
    CONSOLE_CURSOR_INFO mycursor;
    HANDLE hOut;
    SMALL_RECT rc={0,0,a-1,b-1};
    mycursor.bVisible=0;
    mycursor.dwSize=10;
    hOut =GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleScreenBufferSize(hOut,size);
    SetConsoleWindowInfo(hOut,TRUE,&rc);
    SetConsoleCursorInfo(hOut,&mycursor);
    //CloseHandle(hOut); // 这个句柄好像不能释放
    return 0;
}





不知道有没有问题!

沙发
发表于 2018-2-27 16:49:11 | 只看该作者
...完全没看懂,但是明明可以只用两行代码实现电脑关机,为什么要这么复杂呢?

点评

这是 WINAPI……  发表于 2018-2-27 18:11
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

广播台
特别关注
快速回复 返回顶部 返回列表