windows下添加任务计划程序

用Qt6写个程序,想把自己设置为登录windows后自动运行,且以管理员权限运行,最后用windows任务计划实现。

C++
#include <windows.h>
#include <comdef.h>
#include <objbase.h>
#include <taskschd.h>
#include <tchar.h>
#include <QDebug>
#include <winerror.h>
#include <QString>

//任务计划需要这个库文件
#pragma comment(lib, "taskschd.lib")

QString taskErrMessage = ""; //保存错误信息

// 下面的函数在任务计划的itcat目录创建了个名为itcat_amp的任务计划,登录时触发,以最高权限运行。
// 每一步都要判断是否出错,出错就得释放COM对像并退出,嫌麻烦,用了被妖魔化的goto语句,
// 一出错就跳到释放资源的块。 
// taskschd是COM,程序开始要初始化COM环境 用 CoInitialize(NULL);
// 程序结束要调用CoUninitialize(); 释放COM环境。
void createTask(QString sFilePath){
    ITaskService* pService = NULL;
    ITaskFolder* pRootFolder = NULL;
    ITaskFolder *pItcatFolder = NULL;

    ITaskDefinition *pTask = NULL;

    IRegistrationInfo *pRegInfo= NULL;
    ITaskSettings *pSettings = NULL;

    ITriggerCollection *pTriggerCollection = NULL;
    ITrigger *pTrigger = NULL;
    ILogonTrigger *pLogonTrigger = NULL;

    IActionCollection *pActionCollection = NULL;
    IAction *pAction = NULL;
    IExecAction *pExecAction = NULL;

    IPrincipal* pPrin = NULL;

    IRegisteredTask *pRegisteredTask = NULL;

    long lcount = 0;
    bool bFind = false;

    taskErrMessage = "";

    HRESULT hr = CoCreateInstance(CLSID_TaskScheduler, 
                                  NULL,
                                  CLSCTX_INPROC,
                                  IID_ITaskService,
                                  (void**)&pService);
    if (FAILED(hr)) {
        taskErrMessage = "创建COM对像失败!";
        return;
    }

    hr = pService->Connect(_variant_t(), _variant_t(), _variant_t(), _variant_t());
    if (FAILED(hr)) {
        taskErrMessage = "连接计划任务服务失败!";
        goto end;
    }
    hr = pService->GetFolder(_bstr_t(L"\\"), &pRootFolder);
    if (FAILED(hr)) {
        taskErrMessage = "获取根目录失败";
        goto end;
    }

    //获取子目录,如果子目录不存在就创建。
    hr = pRootFolder->GetFolder(_bstr_t(L"itcat"),&pItcatFolder);
    if(FAILED(hr)){
        QString s = "itcat";
        BSTR bstr_name = bstr_t(s.toStdWString().c_str());
        hr = pRootFolder->CreateFolder(bstr_name,_variant_t(),&pItcatFolder);
        if(SUCCEEDED(hr))
            qDebug() << "创建itcat目录成功";
        else{
            taskErrMessage = "创建子目录失败!";
            goto end;
        }
    }

    //创建任务开始
    pService->NewTask(0,&pTask);

    hr = pTask->get_RegistrationInfo( &pRegInfo );
    if( FAILED(hr) )
    {
        taskErrMessage = "创建注册信息失败!";
        goto end;
    }

    pRegInfo->put_Author(_bstr_t(L"ITCat"));
    pRegInfo->Release();

    hr = pTask->get_Settings( &pSettings );
    if( FAILED(hr) )
    {
        taskErrMessage = "创建Setting失败!";
        goto end;
    }

    //  Set setting values for the task.
    pSettings->put_StartWhenAvailable(VARIANT_TRUE);
    pSettings->Release();

    hr = pTask->get_Triggers(&pTriggerCollection);
    if( FAILED(hr) )
    {
        taskErrMessage = "获取触发器集合失败!";
        goto end;
    }

    hr = pTriggerCollection->Create( TASK_TRIGGER_LOGON, &pTrigger );
    pTriggerCollection->Release();
    if( FAILED(hr) )
    {
        taskErrMessage = "创建触发器失败!";
        goto end;
    }

    hr = pTrigger->QueryInterface(IID_ILogonTrigger, (void**) &pLogonTrigger );
    pTrigger->Release();
    if( FAILED(hr) )
    {
        taskErrMessage = "获取登录触发器失败!";
        goto end;
    }
    pLogonTrigger->Release();


    hr = pTask->get_Actions( &pActionCollection );
    if( FAILED(hr) )
    {
        taskErrMessage = "获取Action集合失败!";
        goto end;
    }

    hr = pActionCollection->Create( TASK_ACTION_EXEC, &pAction );
    pActionCollection->Release();
    if( FAILED(hr) )
    {
        taskErrMessage  = "创建Action失败!";
        goto end;
    }

    hr = pAction->QueryInterface(IID_IExecAction, (void**) &pExecAction );
    pAction->Release();
    if( FAILED(hr) )
    {
        taskErrMessage = "获取ExeAction失败!";
        goto end;
    }

    //  Set the path of the executable to notepad.exe.
    hr = pExecAction->put_Path( _bstr_t(sFilePath.toStdWString().c_str()));
    pExecAction->Release();
    if( FAILED(hr) )
    {
        taskErrMessage = "设置可执行文件路径失败!";
        goto end;
    }

    hr = pTask->get_Principal(&pPrin);
    if(SUCCEEDED(hr)){
        pPrin->put_RunLevel(TASK_RUNLEVEL_TYPE::TASK_RUNLEVEL_HIGHEST);
        pPrin->Release();
    }

    hr = pItcatFolder->RegisterTaskDefinition(
        _bstr_t(L"itcat_amp"),
        pTask,
        TASK_CREATE_OR_UPDATE,
        _variant_t(L"S-1-5-32-544"),
        _variant_t(),
        TASK_LOGON_GROUP,
        _variant_t(L""),
        &pRegisteredTask);
    if( FAILED(hr) )
    {
        taskErrMessage = "注册任务失败!";
        if(hr == E_ACCESSDENIED){
            qDebug()<<"无权限!";
        }
    }

    end:
    qDebug()<< taskErrMessage;
    if (pTask!=NULL)  pTask->Release();
    if (pRegisteredTask!=NULL) pRegisteredTask->Release();
    if (pItcatFolder!=NULL)  pItcatFolder->Release();
    if (pRootFolder!=NULL) pRootFolder->Release();
    if (pService!=NULL) pService->Release();
}

Loading

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注