定时关机,用srvany安装成服务使用。
示例代码工程,基于vs2010
/Files/alantop/shutoff.rar
// shutoff.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <atltime.h>
#include <windows.h>
#include <stdio.h>
#include <conio.h>
void shutdown()
{
//从config中读取关机参数
CString cs = "shutoff";
CString hour = "hour";
CString filename = ".\\config.ini";
int offHour =::GetPrivateProfileInt(cs,hour,0,filename);
CString min = "min";
int offMin = ::GetPrivateProfileInt(cs,min,0,filename);
//判断时间并关机
CTime t=CTime::GetCurrentTime();
CTime t1(t.GetYear(),t.GetMonth(),t.GetDay(),offHour,offMin,t.GetSecond());
CTimeSpan ts = t1 - t;
printf("还有%d小时%d分钟关机.\n", ts.GetHours(),ts.GetMinutes() );
int nHour = t.GetHour();
int nMin = t.GetMinute();
char command[] ="c:\\windows\\system32\\shutdown.exe -s -t 60";
if (nHour == offHour && nMin == nHour)
system(command);
}
VOID CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime)
{
//printf("定时器函数开始工作\n");
shutdown();
}
DWORD CALLBACK Thread(PVOID pvoid)
{
BOOL bRet;
MSG msg;
PeekMessage(&msg,NULL,WM_USER,WM_USER,PM_NOREMOVE);
UINT timerid=::SetTimer(NULL,111,10000,TimerProc);
while ((bRet = GetMessage(&msg,NULL,0,0))!=0)
{
if (bRet==-1)
{
// handle the error and possibly exit
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
KillTimer(NULL,timerid);
printf("线程函数结束\n" );
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
CString cs = "shutoff";
CString hour = "hour";
CString filename = ".\\config.ini";
int offHour =::GetPrivateProfileInt(cs,hour,0,filename);
CString min = "min";
int offMin = ::GetPrivateProfileInt(cs,min,0,filename);
DWORD dwThreadId;
printf("定时关机程序启动,计算机将在%d小时%d关机。\n",offHour, offMin );
HANDLE hThread = CreateThread(NULL,0,Thread,0,0,&dwThreadId);
_getch();
return 0;
}