NetScheduleJobAdd函数添加任务计划的方法

来源:互联网 发布:unity3d max文件 编辑:程序博客网 时间:2024/05/29 11:22

//必须启动系统的Task Scheduler服务 

[cpp] view plaincopy
  1. #include "stdafx.h"  
  2. #include <stdio.h>  
  3. #include <windows.h>  
  4. #include <lmcons.h>  
  5. #include <lmat.h>  
  6. #include <IOSTREAM>  
  7.   
  8. #pragma comment(lib,"NETAPI32.LIB")  
  9.   
  10. void Jobadd() {  
  11.     DWORD JobId, ret;  
  12.     AT_INFO ai;  
  13.     char *filepath;  
  14.     long Len;  
  15.     char RootPath[MAX_PATH];  
  16.     WCHAR szFilePath[256];  
  17.     GetSystemDirectory(RootPath, MAX_PATH);  
  18.     filepath = new char[strlen(RootPath) + 11];  
  19.     strcpy(filepath, RootPath);  
  20.     strcat(filepath, "\\notepad.exe"); //完整路径  
  21.     memset(&ai, 0, sizeof(ai));  
  22.     Len = MultiByteToWideChar(CP_ACP, 0, filepath, strlen(filepath), szFilePath, sizeof(szFilePath));  
  23.     szFilePath[Len] = '\0';  
  24.     ai.Command = szFilePath;  
  25.     ai.DaysOfMonth = 0;  
  26.     ai.DaysOfWeek = 0x7F; //7F等于二进制的7个1,就是每周的7天全部运行  
  27.     ai.Flags = JOB_RUN_PERIODICALLY;  
  28.     ai.JobTime = 22 * 60 * 60 * 1000 + 21 * 60 * 1000; //22点21分,这里是以毫秒为单位的,所以需要乘这些值  
  29.     ret = NetScheduleJobAdd(NULL, LPBYTE(&ai), &JobId);  
  30.     if (ret == ERROR_SUCCESS)  
  31.         std::cout << " SUCCESS!!" << std::endl;  
  32.     else  
  33.         std::cout << "Error" << std::endl;  
  34.   
  35. }  
  36.   
  37. int main(int argc, char *argv[]) {  
  38.     Jobadd();  
  39.     return 0;  
  40. }  
0 0
原创粉丝点击