2个回答
展开全部
#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;
}
几年前写过一个,可以用。时间到了就尝试睡眠,睡眠不成功就尝试关机。
中途可以修改关机时间,使用多线程实现时间改变。
2015-11-21
展开全部
可以去百度system函数,那里有很详细的代码。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询