Boot Trigger Example (C++)

  1. /********************************************************************
  2. This sample schedules a task to start Notepad.exe 30 seconds after
  3. the system is started.
  4. ********************************************************************/
  5.  
  6. #define _WIN32_DCOM
  7.  
  8. #include <windows.h>
  9. #include <iostream>
  10. #include <stdio.h>
  11. #include <comdef.h>
  12. // Include the task header file.
  13. #include <taskschd.h>
  14. #pragma comment(lib, "taskschd.lib")
  15. #pragma comment(lib, "comsupp.lib")
  16.  
  17. using namespace std;
  18.  
  19. int __cdecl wmain()
  20. {
  21. // ------------------------------------------------------
  22. // Initialize COM.
  23. HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
  24. if( FAILED(hr) )
  25. {
  26. printf("\nCoInitializeEx failed: %x", hr );
  27. return ;
  28. }
  29.  
  30. // Set general COM security levels.
  31. hr = CoInitializeSecurity(
  32. NULL,
  33. -,
  34. NULL,
  35. NULL,
  36. RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
  37. RPC_C_IMP_LEVEL_IMPERSONATE,
  38. NULL,
  39. ,
  40. NULL);
  41.  
  42. if( FAILED(hr) )
  43. {
  44. printf("\nCoInitializeSecurity failed: %x", hr );
  45. CoUninitialize();
  46. return ;
  47. }
  48.  
  49. // ------------------------------------------------------
  50. // Create a name for the task.
  51. LPCWSTR wszTaskName = L"Boot Trigger Test Task";
  52.  
  53. // Get the Windows directory and set the path to Notepad.exe.
  54. wstring wstrExecutablePath = _wgetenv( L"WINDIR");
  55. wstrExecutablePath += L"\\SYSTEM32\\NOTEPAD.EXE";
  56.  
  57. // ------------------------------------------------------
  58. // Create an instance of the Task Service.
  59. ITaskService *pService = NULL;
  60. hr = CoCreateInstance( CLSID_TaskScheduler,
  61. NULL,
  62. CLSCTX_INPROC_SERVER,
  63. IID_ITaskService,
  64. (void**)&pService );
  65. if (FAILED(hr))
  66. {
  67. printf("Failed to create an instance of ITaskService: %x", hr);
  68. CoUninitialize();
  69. return ;
  70. }
  71.  
  72. // Connect to the task service.
  73. hr = pService->Connect(_variant_t(), _variant_t(),
  74. _variant_t(), _variant_t());
  75. if( FAILED(hr) )
  76. {
  77. printf("ITaskService::Connect failed: %x", hr );
  78. pService->Release();
  79. CoUninitialize();
  80. return ;
  81. }
  82.  
  83. // ------------------------------------------------------
  84. // Get the pointer to the root task folder.
  85. // This folder will hold the new task that is registered.
  86. ITaskFolder *pRootFolder = NULL;
  87. hr = pService->GetFolder( _bstr_t( L"\\") , &pRootFolder );
  88. if( FAILED(hr) )
  89. {
  90. printf("Cannot get Root Folder pointer: %x", hr );
  91. pService->Release();
  92. CoUninitialize();
  93. return ;
  94. }
  95.  
  96. // If the same task exists, remove it.
  97. pRootFolder->DeleteTask( _bstr_t( wszTaskName), );
  98.  
  99. // Create the task builder object to create the task.
  100. ITaskDefinition *pTask = NULL;
  101. hr = pService->NewTask( , &pTask );
  102.  
  103. pService->Release(); // COM clean up. Pointer is no longer used.
  104. if (FAILED(hr))
  105. {
  106. printf("Failed to create a task definition: %x", hr);
  107. pRootFolder->Release();
  108. CoUninitialize();
  109. return ;
  110. }
  111.  
  112. // ------------------------------------------------------
  113. // Get the registration info for setting the identification.
  114. IRegistrationInfo *pRegInfo= NULL;
  115. hr = pTask->get_RegistrationInfo( &pRegInfo );
  116. if( FAILED(hr) )
  117. {
  118. printf("\nCannot get identification pointer: %x", hr );
  119. pRootFolder->Release();
  120. pTask->Release();
  121. CoUninitialize();
  122. return ;
  123. }
  124.  
  125. hr = pRegInfo->put_Author(L"Author Name");
  126. pRegInfo->Release();
  127. if( FAILED(hr) )
  128. {
  129. printf("\nCannot put identification info: %x", hr );
  130. pRootFolder->Release();
  131. pTask->Release();
  132. CoUninitialize();
  133. return ;
  134. }
  135.  
  136. // ------------------------------------------------------
  137. // Create the settings for the task
  138. ITaskSettings *pSettings = NULL;
  139. hr = pTask->get_Settings( &pSettings );
  140. if( FAILED(hr) )
  141. {
  142. printf("\nCannot get settings pointer: %x", hr );
  143. pRootFolder->Release();
  144. pTask->Release();
  145. CoUninitialize();
  146. return ;
  147. }
  148.  
  149. // Set setting values for the task.
  150. hr = pSettings->put_StartWhenAvailable(VARIANT_TRUE);
  151. pSettings->Release();
  152. if( FAILED(hr) )
  153. {
  154. printf("\nCannot put setting info: %x", hr );
  155. pRootFolder->Release();
  156. pTask->Release();
  157. CoUninitialize();
  158. return ;
  159. }
  160.  
  161. // ------------------------------------------------------
  162. // Get the trigger collection to insert the boot trigger.
  163. ITriggerCollection *pTriggerCollection = NULL;
  164. hr = pTask->get_Triggers( &pTriggerCollection );
  165. if( FAILED(hr) )
  166. {
  167. printf("\nCannot get trigger collection: %x", hr );
  168. pRootFolder->Release();
  169. pTask->Release();
  170. CoUninitialize();
  171. return ;
  172. }
  173.  
  174. // Add the boot trigger to the task.
  175. ITrigger *pTrigger = NULL;
  176. hr = pTriggerCollection->Create( TASK_TRIGGER_BOOT, &pTrigger );
  177. pTriggerCollection->Release();
  178. if( FAILED(hr) )
  179. {
  180. printf("\nCannot create the trigger: %x", hr );
  181. pRootFolder->Release();
  182. pTask->Release();
  183. CoUninitialize();
  184. return ;
  185. }
  186.  
  187. IBootTrigger *pBootTrigger = NULL;
  188. hr = pTrigger->QueryInterface(
  189. IID_IBootTrigger, (void**) &pBootTrigger );
  190. pTrigger->Release();
  191. if( FAILED(hr) )
  192. {
  193. printf("\nQueryInterface call failed for IBootTrigger: %x", hr );
  194. pRootFolder->Release();
  195. pTask->Release();
  196. CoUninitialize();
  197. return ;
  198. }
  199.  
  200. hr = pBootTrigger->put_Id( _bstr_t( L"Trigger1" ) );
  201. if( FAILED(hr) )
  202. printf("\nCannot put the trigger ID: %x", hr);
  203.  
  204. // Set the task to start at a certain time. The time
  205. // format should be YYYY-MM-DDTHH:MM:SS(+-)(timezone).
  206. // For example, the start boundary below
  207. // is January 1st 2005 at 12:05
  208. hr = pBootTrigger->put_StartBoundary( _bstr_t(L"2005-01-01T12:05:00") );
  209. if( FAILED(hr) )
  210. printf("\nCannot put the start boundary: %x", hr);
  211.  
  212. hr = pBootTrigger->put_EndBoundary( _bstr_t(L"2015-05-02T08:00:00") );
  213. if( FAILED(hr) )
  214. printf("\nCannot put the end boundary: %x", hr);
  215.  
  216. // Delay the task to start 30 seconds after system start.
  217. hr = pBootTrigger->put_Delay( L"PT30S" );
  218. pBootTrigger->Release();
  219. if( FAILED(hr) )
  220. {
  221. printf("\nCannot put delay for boot trigger: %x", hr );
  222. pRootFolder->Release();
  223. pTask->Release();
  224. CoUninitialize();
  225. return ;
  226. }
  227.  
  228. // ------------------------------------------------------
  229. // Add an Action to the task. This task will execute Notepad.exe.
  230. IActionCollection *pActionCollection = NULL;
  231.  
  232. // Get the task action collection pointer.
  233. hr = pTask->get_Actions( &pActionCollection );
  234. if( FAILED(hr) )
  235. {
  236. printf("\nCannot get Task collection pointer: %x", hr );
  237. pRootFolder->Release();
  238. pTask->Release();
  239. CoUninitialize();
  240. return ;
  241. }
  242.  
  243. // Create the action, specifying it as an executable action.
  244. IAction *pAction = NULL;
  245. hr = pActionCollection->Create( TASK_ACTION_EXEC, &pAction );
  246. pActionCollection->Release();
  247. if( FAILED(hr) )
  248. {
  249. printf("\nCannot create the action: %x", hr );
  250. pRootFolder->Release();
  251. pTask->Release();
  252. CoUninitialize();
  253. return ;
  254. }
  255.  
  256. IExecAction *pExecAction = NULL;
  257. // QI for the executable task pointer.
  258. hr = pAction->QueryInterface(
  259. IID_IExecAction, (void**) &pExecAction );
  260. pAction->Release();
  261. if( FAILED(hr) )
  262. {
  263. printf("\nQueryInterface call failed for IExecAction: %x", hr );
  264. pRootFolder->Release();
  265. pTask->Release();
  266. CoUninitialize();
  267. return ;
  268. }
  269.  
  270. // Set the path of the executable to Notepad.exe.
  271. hr = pExecAction->put_Path( _bstr_t( wstrExecutablePath.c_str() ) );
  272. pExecAction->Release();
  273. if( FAILED(hr) )
  274. {
  275. printf("\nCannot set path of executable: %x", hr );
  276. pRootFolder->Release();
  277. pTask->Release();
  278. CoUninitialize();
  279. return ;
  280. }
  281.  
  282. // ------------------------------------------------------
  283. // Save the task in the root folder.
  284. IRegisteredTask *pRegisteredTask = NULL;
  285. VARIANT varPassword;
  286. varPassword.vt = VT_EMPTY;
  287. hr = pRootFolder->RegisterTaskDefinition(
  288. _bstr_t( wszTaskName ),
  289. pTask,
  290. TASK_CREATE_OR_UPDATE,
  291. _variant_t(L"Local Service"),
  292. varPassword,
  293. TASK_LOGON_SERVICE_ACCOUNT,
  294. _variant_t(L""),
  295. &pRegisteredTask);
  296. if( FAILED(hr) )
  297. {
  298. printf("\nError saving the Task : %x", hr );
  299. pRootFolder->Release();
  300. pTask->Release();
  301. CoUninitialize();
  302. return ;
  303. }
  304.  
  305. printf("\n Success! Task successfully registered. " );
  306.  
  307. // Clean up.
  308. pRootFolder->Release();
  309. pTask->Release();
  310. pRegisteredTask->Release();
  311. CoUninitialize();
  312. return ;
  313. }

设定程序随windows启动的更多相关文章

  1. DSAPI 添加删除程序到Windows启动

    使用DSAPI.dll中文件类里现成的功能,将使你可以快速高效地实现将程序加入Windows启动项或从启动项中删除. 简单也是非常地简单,但由于是比较独立的功能,所以单独发表为整个篇幅.  DSAPI ...

  2. 应用程序无法正常启动0xc0150002(windows server 2003)

    windows server 2003运行一个程序时出现 "应用程序无法正常启动0xc0150002"的错误提示; 解决方案: 下载地址:http://www.microsoft. ...

  3. 当程序以Windows Services形式启动时当前路径不对

    当程序以Windows Services形式启动时当前路径不对 @(操作系统)[博客|dotNet] 很多时候我们需要将我们的程序写成利用Windows服务的形式来让它能够自启动.今天遇到一个问题,当 ...

  4. Windows 7下一个:该应用程序不能正常启动(0xc0150002)

             在新系统中正确安装QQ2010无法执行,同一时候安装的TM2009也无法执行. 相同显示为"应用程序无法正常启动(0xc0150002). 请单击"确定" ...

  5. Windows Server2008安装mysql5.6出现程序无法正常启动(0xc000007b)

    下载 到官网下载mysql5.6版本,msi安装包只有32位无64位 移动到指定文件夹下,解压文件 添加环境变量 变量名:MYSQL_HOME 变量值:C:\Program Files\mysql 即 ...

  6. 在Windows平台用visual studio编译的可执行文件部署时报:应用程序无法正常启动0xc000007b(跟DirectX9无关的原因)

    最近在做EasyDarwin开源流媒体服务器Windows版本编译与部署时发现一个问题,在开发机本机运行都很正常,但是部署到目标机器(未安装vs等开发环境)时,莫名其妙报出了"应用程序无法正 ...

  7. 原创 C++应用程序在Windows下的编译、链接:第一部分 概述

    本文是对C++应用程序在Windows下的编译.链接的深入理解和分析,文章的目录如下: 我们先看第一章概述部分. 1概述 1.1编译工具简介 cl.exe是windows平台下的编译器,link.ex ...

  8. GRUB损坏后,如何修复windows启动mbr

    今天使用Ghost装系统遇到windows7不能启动的问题,采用下面帖子中的部分命令搞定之. 我自己是直接使用: 插入windows7安装光盘,从光盘启动,在光盘启动完成后,按下shift+f10键, ...

  9. C++应用程序在Windows下的编译、链接(一)概述

    C++应用程序在Windows下的编译.链接(一)概述 本文是对C++应用程序在Windows下的编译.链接的深入理解和分析,文章的目录如下: 我们先看第一章概述部分. 1概述 1.1编译工具简介 c ...

随机推荐

  1. ovirt 替换自主签署证书

    需求我自己写了一个python后台,添加上了ovirt 引擎web上,如图 但第一次访问时需要,需要接受两次不安全连接,ovirt  web使用https,我往里面加http,加不进去. 只能同样使用 ...

  2. cls

    class : python中cls代表的是类的本身,相对应的self则是类的一个实例对象. class Person(object): def __init__(self, name, age): ...

  3. 1006 Sign In and Sign Out (25 分)

    At the beginning of every day, the first person who signs in the computer room will unlock the door, ...

  4. docker、docker-compose安装,卸载

    docker win10安装 一.安装 https://www.docker.com/docker-windows 二.设置 控制面板-->程序-->Hyper-V linux安装: ht ...

  5. 8.2 常见api:String类的使用

    /* * String:字符串类 * 由多个字符组成的一串数据 * 字符串其本质是一个字符数组 * * 构造方法: * String(String original):把字符串数据封装成字符串对象 * ...

  6. C语言实现顺序栈以及栈的特点

    什么是栈? 同顺序表和链表一样,栈也是用来存储逻辑关系为 "一对一" 数据的线性存储结构,如下图所示. 从上图我们看到,栈存储结构与之前所学的线性存储结构有所差异,这缘于栈对数据 ...

  7. 04 jmeter使用方式3种

    1.手工添加配置元件编写 2.jmeter+badboy 工具录制---不建议使用 3.设置代理服务器(jmeter添加‘非测试元件-http代理服务器’,再添加一个线程组用来保留代理抓取的url,设 ...

  8. Linux c++ vim环境搭建系列(4)——vim插件安装配置使用

    4. 插件 主要是c++相关的. ~/.vimrc文件在GitHub上有:https://github.com/whuwzp/vim_config 以下内容参考: https://github.com ...

  9. XFS文件系统的备份与恢复

    永久修改主机名:hostnamectl set-hostname oldboy临时修改主机名:hostname xfsdump备份xfsdump -f 备份的文件位置 要备份的分区或者磁盘 免交互备份 ...

  10. 植物大战僵尸的代码如何使用python来实现

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:程序IT圈 PS:如有需要Python学习资料的小伙伴可以加点击下方链 ...