C++ 用TaskScheduler.dll 创建 windows任务计划
时间: 2024-05-13 16:18:45 浏览: 206
可以使用Task Scheduler API来创建Windows任务计划。以下是一个简单的示例```c++
#include <windows.h>
#include <tasksd.h>
#include <comutil.h>
#pragma comment, "taskschd.lib")
#pragma comment(lib,comsuppw.lib")
int main()
{
CoInitialize);
ITaskService *pService = NULL HRESULT hr = CoCreateInstance(CLSID_Task, NULL, CLSCTX_INPROC_SERVER, IIDaskService, (void**)&pService);
if (FAILED(hr))
printf("Failed to create an instance of ITaskService: %x", hr);
return 1;
}
hr = pService->Connect(_variant_t(), _variant_t(), _variant_t(), _variant_t());
if (FAILED(hr))
{
printf("Failed to connect to Task Scheduler: %x", hr);
pService->Release();
return 1;
}
ITaskFolder *pRootFolder = NULL;
hr = pService->GetFolder(_bstr_t(L"\\"), &pRootFolder);
if (FAILED(hr))
{
printf("Failed to get root folder: %x", hr);
pService->Release();
return 1;
}
ITaskDefinition *pTask = NULL;
hr = pService->NewTask(0, &pTask);
if (FAILED(hr))
{
printf("Failed to create a new task definition: %x", hr);
pRootFolder->Release();
pService->Release();
return 1;
}
IRegistrationInfo *pRegInfo = NULL;
hr = pTask->get_RegistrationInfo(&pRegInfo);
if (FAILED(hr))
{
printf("Failed to get registration info: %x", hr);
pTask->Release();
pRootFolder->Release();
pService->Release();
return 1;
}
hr = pRegInfo->put_Author(_bstr_t(L"Your Name"));
pRegInfo->Release();
if (FAILED(hr))
{
printf("Failed to set author: %x", hr);
pTask->Release();
pRootFolder->Release();
pService->Release();
return 1;
}
ITaskSettings *pSettings = NULL;
hr = pTask->get_Settings(&pSettings);
if (FAILED(hr))
{
printf("Failed to get task settings: %x", hr);
pTask->Release();
pRootFolder->Release();
pService->Release();
return 1;
}
hr = pSettings->put_StartWhenAvailable(VARIANT_TRUE);
pSettings->Release();
if (FAILED(hr))
{
printf("Failed to set start when available: %x", hr);
pTask->Release();
pRootFolder->Release();
pService->Release();
return 1;
}
IPrincipal *pPrincipal = NULL;
hr = pTask->get_Principal(&pPrincipal);
if (FAILED(hr))
{
printf("Failed to get principal: %x", hr);
pTask->Release();
pRootFolder->Release();
pService->Release();
return 1;
}
hr = pPrincipal->put_RunLevel(TASK_RUNLEVEL_HIGHEST);
pPrincipal->Release();
if (FAILED(hr))
{
printf("Failed to set run level: %x", hr);
pTask->Release();
pRootFolder->Release();
pService->Release();
return 1;
}
IActionCollection *pActions = NULL;
hr = pTask->get_Actions(&pActions);
if (FAILED(hr))
{
printf("Failed to get action collection: %x", hr);
pTask->Release();
pRootFolder->Release();
pService->Release();
return 1;
}
IAction *pAction = NULL;
hr = pActions->Create(TASK_ACTION_EXEC, &pAction);
pActions->Release();
if (FAILED(hr))
{
printf("Failed to create action: %x", hr);
pTask->Release();
pRootFolder->Release();
pService->Release();
return 1;
}
IExecAction *pExecAction = NULL;
hr = pAction->QueryInterface(IID_IExecAction, (void**)&pExecAction);
pAction->Release();
if (FAILED(hr))
{
printf("Failed to query interface for IExecAction: %x", hr);
pTask->Release();
pRootFolder->Release();
pService->Release();
return 1;
}
hr = pExecAction->put_Path(_bstr_t(L"C:\\path\\to\\executable.exe"));
if (FAILED(hr))
{
printf("Failed to set executable path: %x", hr);
pExecAction->Release();
pTask->Release();
pRootFolder->Release();
pService->Release();
return 1;
}
ITaskDefinition *pTaskDef = NULL;
hr = pTask->QueryInterface(IID_ITaskDefinition, (void**)&pTaskDef);
pTask->Release();
if (FAILED(hr))
{
printf("Failed to query interface for ITaskDefinition: %x", hr);
pRootFolder->Release();
pService->Release();
return 1;
}
ITriggerCollection *pTriggerCollection = NULL;
hr = pTaskDef->get_Triggers(&pTriggerCollection);
if (FAILED(hr))
{
printf("Failed to get trigger collection: %x", hr);
pTaskDef->Release();
pRootFolder->Release();
pService->Release();
return 1;
}
ITrigger *pTrigger = NULL;
hr = pTriggerCollection->Create(TASK_TRIGGER_DAILY, &pTrigger);
pTriggerCollection->Release();
if (FAILED(hr))
{
printf("Failed to create trigger: %x", hr);
pTaskDef->Release();
pRootFolder->Release();
pService->Release();
return 1;
}
IDailyTrigger *pDailyTrigger = NULL;
hr = pTrigger->QueryInterface(IID_IDailyTrigger, (void**)&pDailyTrigger);
pTrigger->Release();
if (FAILED(hr))
{
printf("Failed to query interface for IDailyTrigger: %x", hr);
pTaskDef->Release();
pRootFolder->Release();
pService->Release();
return 1;
}
hr = pDailyTrigger->put_Id(_bstr_t(L"DailyTrigger"));
if (FAILED(hr))
{
printf("Failed to set trigger ID: %x", hr);
pDailyTrigger->Release();
pTaskDef->Release();
pRootFolder->Release();
pService->Release();
return 1;
}
SYSTEMTIME st;
GetLocalTime(&st);
st.wHour = 12;
st.wMinute = 0;
st.wSecond = 0;
st.wMilliseconds = 0;
DATE start_date;
SystemTimeToVariantTime(&st, &start_date);
hr = pDailyTrigger->put_StartBoundary(_bstr_t(start_date));
pDailyTrigger->Release();
if (FAILED(hr))
{
printf("Failed to set start boundary: %x", hr);
pTaskDef->Release();
pRootFolder->Release();
pService->Release();
return 1;
}
IRegisteredTask *pRegisteredTask = NULL;
hr = pRootFolder->RegisterTaskDefinition(_bstr_t(L"MyTask"), pTaskDef, TASK_CREATE_OR_UPDATE, _variant_t(), _variant_t(), TASK_LOGON_INTERACTIVE_TOKEN, _variant_t(), &pRegisteredTask);
pTaskDef->Release();
pRootFolder->Release();
pService->Release();
if (FAILED(hr))
{
printf("Failed to register task definition: %x", hr);
return 1;
}
printf("Task successfully registered.");
pRegisteredTask->Release();
CoUninitialize();
return 0;
}
```
这个例子使用了COM和ATL,需要添加对comsuppw.lib和taskschd.lib库的链接。如果需要更改任务计划的设置,只需修改相应的属性即可。最后,调用ITaskFolder::RegisterTaskDefinition方法来注册任务计划。
阅读全文