设定程序随windows启动
- /********************************************************************
- This sample schedules a task to start Notepad.exe 30 seconds after
- the system is started.
- ********************************************************************/
- #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 ;
- }
- // Set general COM security levels.
- hr = CoInitializeSecurity(
- NULL,
- -,
- NULL,
- NULL,
- RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
- RPC_C_IMP_LEVEL_IMPERSONATE,
- NULL,
- ,
- NULL);
- if( FAILED(hr) )
- {
- printf("\nCoInitializeSecurity failed: %x", hr );
- CoUninitialize();
- return ;
- }
- // ------------------------------------------------------
- // Create a name for the task.
- LPCWSTR wszTaskName = L"Boot 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 ;
- }
- // 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 ;
- }
- // ------------------------------------------------------
- // 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 ;
- }
- // If the same task exists, remove it.
- pRootFolder->DeleteTask( _bstr_t( wszTaskName), );
- // Create the task builder object to create the task.
- ITaskDefinition *pTask = NULL;
- hr = pService->NewTask( , &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 ;
- }
- // ------------------------------------------------------
- // 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 ;
- }
- 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 ;
- }
- // ------------------------------------------------------
- // 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 ;
- }
- // 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 ;
- }
- // ------------------------------------------------------
- // Get the trigger collection to insert the boot 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 ;
- }
- // Add the boot trigger to the task.
- ITrigger *pTrigger = NULL;
- hr = pTriggerCollection->Create( TASK_TRIGGER_BOOT, &pTrigger );
- pTriggerCollection->Release();
- if( FAILED(hr) )
- {
- printf("\nCannot create the trigger: %x", hr );
- pRootFolder->Release();
- pTask->Release();
- CoUninitialize();
- return ;
- }
- IBootTrigger *pBootTrigger = NULL;
- hr = pTrigger->QueryInterface(
- IID_IBootTrigger, (void**) &pBootTrigger );
- pTrigger->Release();
- if( FAILED(hr) )
- {
- printf("\nQueryInterface call failed for IBootTrigger: %x", hr );
- pRootFolder->Release();
- pTask->Release();
- CoUninitialize();
- return ;
- }
- hr = pBootTrigger->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 = pBootTrigger->put_StartBoundary( _bstr_t(L"2005-01-01T12:05:00") );
- if( FAILED(hr) )
- printf("\nCannot put the start boundary: %x", hr);
- hr = pBootTrigger->put_EndBoundary( _bstr_t(L"2015-05-02T08:00:00") );
- if( FAILED(hr) )
- printf("\nCannot put the end boundary: %x", hr);
- // Delay the task to start 30 seconds after system start.
- hr = pBootTrigger->put_Delay( L"PT30S" );
- pBootTrigger->Release();
- if( FAILED(hr) )
- {
- printf("\nCannot put delay for boot trigger: %x", hr );
- pRootFolder->Release();
- pTask->Release();
- CoUninitialize();
- return ;
- }
- // ------------------------------------------------------
- // 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 ;
- }
- // Create the action, specifying it as 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 ;
- }
- 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 ;
- }
- // 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 ;
- }
- // ------------------------------------------------------
- // Save the task in the root folder.
- IRegisteredTask *pRegisteredTask = NULL;
- VARIANT varPassword;
- varPassword.vt = VT_EMPTY;
- hr = pRootFolder->RegisterTaskDefinition(
- _bstr_t( wszTaskName ),
- pTask,
- TASK_CREATE_OR_UPDATE,
- _variant_t(L"Local Service"),
- varPassword,
- TASK_LOGON_SERVICE_ACCOUNT,
- _variant_t(L""),
- &pRegisteredTask);
- if( FAILED(hr) )
- {
- printf("\nError saving the Task : %x", hr );
- pRootFolder->Release();
- pTask->Release();
- CoUninitialize();
- return ;
- }
- printf("\n Success! Task successfully registered. " );
- // Clean up.
- pRootFolder->Release();
- pTask->Release();
- pRegisteredTask->Release();
- CoUninitialize();
- return ;
- }
设定程序随windows启动的更多相关文章
- DSAPI 添加删除程序到Windows启动
使用DSAPI.dll中文件类里现成的功能,将使你可以快速高效地实现将程序加入Windows启动项或从启动项中删除. 简单也是非常地简单,但由于是比较独立的功能,所以单独发表为整个篇幅. DSAPI ...
- 应用程序无法正常启动0xc0150002(windows server 2003)
windows server 2003运行一个程序时出现 "应用程序无法正常启动0xc0150002"的错误提示; 解决方案: 下载地址:http://www.microsoft. ...
- 当程序以Windows Services形式启动时当前路径不对
当程序以Windows Services形式启动时当前路径不对 @(操作系统)[博客|dotNet] 很多时候我们需要将我们的程序写成利用Windows服务的形式来让它能够自启动.今天遇到一个问题,当 ...
- Windows 7下一个:该应用程序不能正常启动(0xc0150002)
在新系统中正确安装QQ2010无法执行,同一时候安装的TM2009也无法执行. 相同显示为"应用程序无法正常启动(0xc0150002). 请单击"确定" ...
- Windows Server2008安装mysql5.6出现程序无法正常启动(0xc000007b)
下载 到官网下载mysql5.6版本,msi安装包只有32位无64位 移动到指定文件夹下,解压文件 添加环境变量 变量名:MYSQL_HOME 变量值:C:\Program Files\mysql 即 ...
- 在Windows平台用visual studio编译的可执行文件部署时报:应用程序无法正常启动0xc000007b(跟DirectX9无关的原因)
最近在做EasyDarwin开源流媒体服务器Windows版本编译与部署时发现一个问题,在开发机本机运行都很正常,但是部署到目标机器(未安装vs等开发环境)时,莫名其妙报出了"应用程序无法正 ...
- 原创 C++应用程序在Windows下的编译、链接:第一部分 概述
本文是对C++应用程序在Windows下的编译.链接的深入理解和分析,文章的目录如下: 我们先看第一章概述部分. 1概述 1.1编译工具简介 cl.exe是windows平台下的编译器,link.ex ...
- GRUB损坏后,如何修复windows启动mbr
今天使用Ghost装系统遇到windows7不能启动的问题,采用下面帖子中的部分命令搞定之. 我自己是直接使用: 插入windows7安装光盘,从光盘启动,在光盘启动完成后,按下shift+f10键, ...
- C++应用程序在Windows下的编译、链接(一)概述
C++应用程序在Windows下的编译.链接(一)概述 本文是对C++应用程序在Windows下的编译.链接的深入理解和分析,文章的目录如下: 我们先看第一章概述部分. 1概述 1.1编译工具简介 c ...
随机推荐
- ovirt 替换自主签署证书
需求我自己写了一个python后台,添加上了ovirt 引擎web上,如图 但第一次访问时需要,需要接受两次不安全连接,ovirt web使用https,我往里面加http,加不进去. 只能同样使用 ...
- cls
class : python中cls代表的是类的本身,相对应的self则是类的一个实例对象. class Person(object): def __init__(self, name, age): ...
- 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, ...
- docker、docker-compose安装,卸载
docker win10安装 一.安装 https://www.docker.com/docker-windows 二.设置 控制面板-->程序-->Hyper-V linux安装: ht ...
- 8.2 常见api:String类的使用
/* * String:字符串类 * 由多个字符组成的一串数据 * 字符串其本质是一个字符数组 * * 构造方法: * String(String original):把字符串数据封装成字符串对象 * ...
- C语言实现顺序栈以及栈的特点
什么是栈? 同顺序表和链表一样,栈也是用来存储逻辑关系为 "一对一" 数据的线性存储结构,如下图所示. 从上图我们看到,栈存储结构与之前所学的线性存储结构有所差异,这缘于栈对数据 ...
- 04 jmeter使用方式3种
1.手工添加配置元件编写 2.jmeter+badboy 工具录制---不建议使用 3.设置代理服务器(jmeter添加‘非测试元件-http代理服务器’,再添加一个线程组用来保留代理抓取的url,设 ...
- Linux c++ vim环境搭建系列(4)——vim插件安装配置使用
4. 插件 主要是c++相关的. ~/.vimrc文件在GitHub上有:https://github.com/whuwzp/vim_config 以下内容参考: https://github.com ...
- XFS文件系统的备份与恢复
永久修改主机名:hostnamectl set-hostname oldboy临时修改主机名:hostname xfsdump备份xfsdump -f 备份的文件位置 要备份的分区或者磁盘 免交互备份 ...
- 植物大战僵尸的代码如何使用python来实现
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:程序IT圈 PS:如有需要Python学习资料的小伙伴可以加点击下方链 ...