定时关机小程序
写得很简陋,但是定时关机这个功能还是可以实现。我以前总结过用shutdown -i 实现定时关机,但是这总有个老火的提示框。(关机等待时间似乎能够设置的无限长,如果一直开机,你可以设置到一年以后自动关机,这也是一个优势)
(一) 定时关机(shutdown)
强制关机 shutdown -s
定时关机
第一步:shutdown -i(将弹出一个窗口)
第二步:添加你的计算机ip或计算机名
第三步:选择关机
第四步:输入警告显示时间(就是定时关机的等待时间,以秒计算,似乎没有什么限制)
第五步:填写注释(必填,或则 确定按钮将不可选),随便填就行了,确定。
要想取消 在cmd下输入 shutdown -a 不能强制关闭。
(二)
下面是c语言对 shutdown的利用,实现定时关机,和直接使用shutdown -i差不多,
只是少了个提示框。但关机前还是会提示。密码设置只是个形式。
程序代码:#include<stdio.h>
#include<dos.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#define OK 0
#define FALSE 1
int tm_set(struct time *set_time)
{
char ch;
int hour,min,curr_hour,curr_min;
struct time curr_time;
gettime(&curr_time);
system("cls");
printf("set shutdown_os time(for example : 12:00)==");
scanf("%d:%d",&hour,&min);
if(curr_time.ti_hour>=hour&&curr_time.ti_min>=min)
{
system("cls");
printf("Failed!");
sleep(3);
exit(FALSE);
}
set_time->ti_hour=hour;
set_time->ti_min=min;
return OK;
}
int tmcontrol(struct time *set_time)
{
int hour,min;
struct time curr_time;
gettime(&curr_time);
hour=curr_time.ti_hour;
min=curr_time.ti_min;
if(hour==set_time->ti_hour&&min==set_time->ti_min)
{shutdown_os(); return OK;}
else return FALSE;
}
int shutdown_os(void)
{
char *ch="shutdown -s -t 60";
system("cmd /c a.vbs");
sleep(5);
system(ch);
return OK;
}
int set_pwd()
{
char pwd[16];
char data;
int i;
FILE *fp;
clrscr();
printf("set password:");
for(i=0;i<16;i++)
{
if((data=getch())=='\r')
break;
pwd[i]=data;
putchar('*');
}
pwd[i]='\0';
system("cls");
printf("password is: ");
puts(pwd);
getch();
system("cls");
if((fp=fopen("pwd.dat","wb+"))==NULL)
return FALSE;
else { fprintf(fp,"%s",pwd);fclose(fp);}
return OK;
}
int pwd_verify()
{
char ch={0};
char rd[16]={0};
FILE *fp;
if((fp=fopen("pwd.dat","r"))==NULL)
{
set_pwd();
return OK;
}
else
{
fgets(rd,16,fp);
printf("your password:");
scanf("%s",ch);
}
if(strcmp(ch,rd)==0)
return OK;
else {
system("cls");
printf("\n wrong password!");
sleep(3);
return FALSE;
}
}
int hidpro(void)
{
char *srch="cpy.bat";
char *path;
char *hid;
if((path=searchpath(srch))==NULL)
return FALSE;
system(path);
hid="cmdow @ /hid";
system(hid);
return OK;
}
int main()
{
struct time *set_time;
int work=1;
system("cls");
if(pwd_verify()==OK)
tm_set(set_time);
else return FALSE;
if(hidpro()==OK)
{
while(work==1)
{
if(tmcontrol(set_time)==OK)
break;
sleep(30);
}
}
else
{
printf("File lost!");
return FALSE;
}
return OK;
}[ 本帖最后由 rs369007 于 2009-8-15 18:12 编辑 ]







