//必须启动系统的Task Scheduler服务
- #include "stdafx.h"
- #include <stdio.h>
- #include <windows.h>
- #include <lmcons.h>
- #include <lmat.h>
- #include <IOSTREAM>
- #pragma comment(lib,"NETAPI32.LIB")
- void Jobadd() {
- DWORD JobId, ret;
- AT_INFO ai;
- char *filepath;
- long Len;
- char RootPath[MAX_PATH];
- WCHAR szFilePath[256];
- GetSystemDirectory(RootPath, MAX_PATH);
- filepath = new char[strlen(RootPath) + 11];
- strcpy(filepath, RootPath);
- strcat(filepath, "\\notepad.exe"); //完整路径
- memset(&ai, 0, sizeof(ai));
- Len = MultiByteToWideChar(CP_ACP, 0, filepath, strlen(filepath), szFilePath, sizeof(szFilePath));
- szFilePath[Len] = '\0';
- ai.Command = szFilePath;
- ai.DaysOfMonth = 0;
- ai.DaysOfWeek = 0x7F; //7F等于二进制的7个1,就是每周的7天全部运行
- ai.Flags = JOB_RUN_PERIODICALLY;
- ai.JobTime = 22 * 60 * 60 * 1000 + 21 * 60 * 1000; //22点21分,这里是以毫秒为单位的,所以需要乘这些值
- ret = NetScheduleJobAdd(NULL, LPBYTE(&ai), &JobId);
- if (ret == ERROR_SUCCESS)
- std::cout << " SUCCESS!!" << std::endl;
- else
- std::cout << "Error" << std::endl;
- }
- int main(int argc, char *argv[]) {
- Jobadd();
- return 0;
- }