Logon Trigger Example (C++)
This C++ example shows how to create a task that is scheduled to execute Notepad when a user logs on. The task contains a logon trigger that specifies a start boundary for the task to start and a user identifier that specifies the user. The task is registered using the Administrators group as a security context to run the task.
The following procedure describes how to schedule a task to start an executable when a user logs on.
To schedule Notepad to start when a user logs on
-
Initialize COM and set general COM security.
-
Create the ITaskService object.
This object allows you to create tasks in a specified folder.
-
Get a task folder to create a task in.
Use the ITaskService::GetFolder method to get the folder, and the ITaskService::NewTask method to create the ITaskDefinition object.
-
Define information about the task using the ITaskDefinition object, such as the registration information for the task.
Use the RegistrationInfo property of ITaskDefinition and other properties of the ITaskDefinition interface to define the task information.
-
Create a logon trigger using the Triggers property of ITaskDefinition to access the ITriggerCollection interface for the task.
Use the ITriggerCollection::Create method to specify that you want to create a logon trigger. You can set the start boundary and the UserId property for the trigger so that the task's actions will be scheduled to execute when the user logs on after the start boundary.
-
Create an action for the task to execute by using the Actions property of ITaskDefinition to access the IActionCollection interface for the task. Use the IActionCollection::Create method to specify the type of action that you want to create. This example uses an IExecAction object, which represents an action that executes a command-line operation.
-
Register the task using the ITaskFolder::RegisterTaskDefinition method.
The following C++ example shows how to schedule a task to execute Notepad when a user logs on.
/********************************************************************** This sample schedules a task to start notepad.exe when a user logs on. **********************************************************************/ #define _WIN32_DCOM #include <windows.h> #include <iostream> #include <stdio.h> #include <comdef.h> // Include the task header file. #include <taskschd.h> #pragma comment(lib, "taskschd.lib") #pragma comment(lib, "comsupp.lib") using namespace std; int __cdecl wmain() { // ------------------------------------------------------ // Initialize COM. HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); if( FAILED(hr) ) { printf("\nCoInitializeEx failed: %x", hr ); return 1; } // Set general COM security levels. hr = CoInitializeSecurity( NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT_PRIVACY, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, 0, NULL); if( FAILED(hr) ) { printf("\nCoInitializeSecurity failed: %x", hr ); CoUninitialize(); return 1; } // ------------------------------------------------------ // Create a name for the task. LPCWSTR wszTaskName = L"Logon Trigger Test Task"; // Get the windows directory and set the path to notepad.exe. wstring wstrExecutablePath = _wgetenv( L"WINDIR"); wstrExecutablePath += L"\\SYSTEM32\\NOTEPAD.EXE"; // ------------------------------------------------------ // Create an instance of the Task Service. ITaskService *pService = NULL; hr = CoCreateInstance( CLSID_TaskScheduler, NULL, CLSCTX_INPROC_SERVER, IID_ITaskService, (void**)&pService ); if (FAILED(hr)) { printf("Failed to create an instance of ITaskService: %x", hr); CoUninitialize(); return 1; } // Connect to the task service. hr = pService->Connect(_variant_t(), _variant_t(), _variant_t(), _variant_t()); if( FAILED(hr) ) { printf("ITaskService::Connect failed: %x", hr ); pService->Release(); CoUninitialize(); return 1; } // ------------------------------------------------------ // Get the pointer to the root task folder. This folder will hold the // new task that is registered. ITaskFolder *pRootFolder = NULL; hr = pService->GetFolder( _bstr_t( L"\\") , &pRootFolder ); if( FAILED(hr) ) { printf("Cannot get Root Folder pointer: %x", hr ); pService->Release(); CoUninitialize(); return 1; } // If the same task exists, remove it. pRootFolder->DeleteTask( _bstr_t( wszTaskName), 0 ); // Create the task builder object to create the task. ITaskDefinition *pTask = NULL; hr = pService->NewTask( 0, &pTask ); pService->Release(); // COM clean up. Pointer is no longer used. if (FAILED(hr)) { printf("Failed to create a task definition: %x", hr); pRootFolder->Release(); CoUninitialize(); return 1; } // ------------------------------------------------------ // Get the registration info for setting the identification. IRegistrationInfo *pRegInfo= NULL; hr = pTask->get_RegistrationInfo( &pRegInfo ); if( FAILED(hr) ) { printf("\nCannot get identification pointer: %x", hr ); pRootFolder->Release(); pTask->Release(); CoUninitialize(); return 1; } hr = pRegInfo->put_Author(L"Author Name"); pRegInfo->Release(); if( FAILED(hr) ) { printf("\nCannot put identification info: %x", hr ); pRootFolder->Release(); pTask->Release(); CoUninitialize(); return 1; } // ------------------------------------------------------ // Create the settings for the task ITaskSettings *pSettings = NULL; hr = pTask->get_Settings( &pSettings ); if( FAILED(hr) ) { printf("\nCannot get settings pointer: %x", hr ); pRootFolder->Release(); pTask->Release(); CoUninitialize(); return 1; } // Set setting values for the task. hr = pSettings->put_StartWhenAvailable(VARIANT_TRUE); pSettings->Release(); if( FAILED(hr) ) { printf("\nCannot put setting info: %x", hr ); pRootFolder->Release(); pTask->Release(); CoUninitialize(); return 1; } // ------------------------------------------------------ // Get the trigger collection to insert the logon trigger. ITriggerCollection *pTriggerCollection = NULL; hr = pTask->get_Triggers( &pTriggerCollection ); if( FAILED(hr) ) { printf("\nCannot get trigger collection: %x", hr ); pRootFolder->Release(); pTask->Release(); CoUninitialize(); return 1; } // Add the logon trigger to the task. ITrigger *pTrigger = NULL; hr = pTriggerCollection->Create( TASK_TRIGGER_LOGON, &pTrigger ); pTriggerCollection->Release(); if( FAILED(hr) ) { printf("\nCannot create the trigger: %x", hr ); pRootFolder->Release(); pTask->Release(); CoUninitialize(); return 1; } ILogonTrigger *pLogonTrigger = NULL; hr = pTrigger->QueryInterface( IID_ILogonTrigger, (void**) &pLogonTrigger ); pTrigger->Release(); if( FAILED(hr) ) { printf("\nQueryInterface call failed for ILogonTrigger: %x", hr ); pRootFolder->Release(); pTask->Release(); CoUninitialize(); return 1; } hr = pLogonTrigger->put_Id( _bstr_t( L"Trigger1" ) ); if( FAILED(hr) ) printf("\nCannot put the trigger ID: %x", hr); // Set the task to start at a certain time. The time // format should be YYYY-MM-DDTHH:MM:SS(+-)(timezone). // For example, the start boundary below // is January 1st 2005 at 12:05 hr = pLogonTrigger->put_StartBoundary( _bstr_t(L"2005-01-01T12:05:00") ); if( FAILED(hr) ) printf("\nCannot put the start boundary: %x", hr); hr = pLogonTrigger->put_EndBoundary( _bstr_t(L"2015-05-02T08:00:00") ); if( FAILED(hr) ) printf("\nCannot put the end boundary: %x", hr); // Define the user. The task will execute when the user logs on. // The specified user must be a user on this computer. hr = pLogonTrigger->put_UserId( _bstr_t( L"DOMAIN\\UserName" ) ); pLogonTrigger->Release(); if( FAILED(hr) ) { printf("\nCannot add user ID to logon trigger: %x", hr ); pRootFolder->Release(); pTask->Release(); CoUninitialize(); return 1; } // ------------------------------------------------------ // Add an Action to the task. This task will execute notepad.exe. IActionCollection *pActionCollection = NULL; // Get the task action collection pointer. hr = pTask->get_Actions( &pActionCollection ); if( FAILED(hr) ) { printf("\nCannot get Task collection pointer: %x", hr ); pRootFolder->Release(); pTask->Release(); CoUninitialize(); return 1; } // Create the action, specifying that it is an executable action. IAction *pAction = NULL; hr = pActionCollection->Create( TASK_ACTION_EXEC, &pAction ); pActionCollection->Release(); if( FAILED(hr) ) { printf("\nCannot create the action: %x", hr ); pRootFolder->Release(); pTask->Release(); CoUninitialize(); return 1; } IExecAction *pExecAction = NULL; // QI for the executable task pointer. hr = pAction->QueryInterface( IID_IExecAction, (void**) &pExecAction ); pAction->Release(); if( FAILED(hr) ) { printf("\nQueryInterface call failed for IExecAction: %x", hr ); pRootFolder->Release(); pTask->Release(); CoUninitialize(); return 1; } // Set the path of the executable to notepad.exe. hr = pExecAction->put_Path( _bstr_t( wstrExecutablePath.c_str() ) ); pExecAction->Release(); if( FAILED(hr) ) { printf("\nCannot set path of executable: %x", hr ); pRootFolder->Release(); pTask->Release(); CoUninitialize(); return 1; } // ------------------------------------------------------ // Save the task in the root folder. IRegisteredTask *pRegisteredTask = NULL; hr = pRootFolder->RegisterTaskDefinition( _bstr_t( wszTaskName ), pTask, TASK_CREATE_OR_UPDATE, _variant_t(L"Builtin\\Administrators"), _variant_t(), TASK_LOGON_GROUP, _variant_t(L""), &pRegisteredTask); if( FAILED(hr) ) { printf("\nError saving the Task : %x", hr ); pRootFolder->Release(); pTask->Release(); CoUninitialize(); return 1; } printf("\n Success! Task successfully registered. " ); // Clean up pRootFolder->Release(); pTask->Release(); pRegisteredTask->Release(); CoUninitialize(); return 0; }
原文地址:https://www.cnblogs.com/liujx2019/p/11812059.html
- .NET4.0下网站应用程序用UrlRewriter.dll重写无后缀路径 (在IIS7.5中的配置方法)
- 微信搜索新发现:iPhone 内存不足看这里!
- 建立可扩展的silverlight 应用框架 step-6
- .NET4.0下网站应用程序用UrlRewriter.dll重写无后缀路径 (在IIS7.5中的配置方法)
- 苹果为你的心跳开发一个读者
- 建立可扩展的silverlight 应用框架 step-5
- 安卓 iOS 版双双更新!还带来一大波小游戏
- 建立可扩展的silverlight 应用框架 step-4
- 全自动驾驶,吹牛容易实现难!有90%的人都不了解这些细节
- .NET4.0下web应用程序用UrlRewriter.dll重写无后缀路径
- Silverlight中摄像头的运用—part2
- 区块链小白投资入门操作指南(上)
- 《我的WCF之旅》博文系列汇总
- 网站出现“Service Unavailable”提示该如何解决
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Python 为什么不支持 switch 语句?
- centos安装jdk1.8时出现没有/lib/ld-linux.so.2:这个文件的原因分析
- Linux多线程编程快速入门
- Linux添加swap分区的方法
- linux下vsftpd的安装及配置使用详细步骤(推荐)
- Linux分区格式化的命令
- Linux调试工具GDB使用简易教程
- Linux 挂载分区的方法
- 使用Python生成Shell命令,批量执行程序代码解析
- 基于Ubuntu 18.04配置固定IP的方法教程
- 详细解读linux下swap分区的作用
- Linux下将源文件编译成目标文件的过程解析
- Linux使用vim编辑文件保存时报E514:write error (file system full?)问题解决
- Linux下怎么切换使用两个版本的JDK
- Linux下Nginx安装的方法(pcre和openssl)