#include <windows.h>
#include <initguid.h>
#include <ole2.h>
#include <mstask.h>
#include <msterr.h>
#include <wchar.h>
#include<stdio.h>
//
//函数名称:DeleteTask
//函数功能:删除指定的任务计划
//参数说明:lpcwszTaskName:要删除的任务计划的名称
//
HRESULT DeleteTask(LPCWSTR lpcwszTaskName);
//
//函数名称:CreateTask
//函数功能:创建新的任务计划
//参数说明:lpcwszTaskName:要创建的任务计划的名称
// lpcwszApplicationName:要创建的任务计划的应用程序路径
//
HRESULT CreateTask(LPCWSTR lpcwszTaskName,LPCWSTR lpcwszApplicationName);
int main(int argc, char **argv)
{
HRESULT hr = S_OK;
LPCWSTR lpcwszTaskName = L"Test Task";
LPCWSTR lpcwszApplicationName = L"C://Windows//notepad.exe";
//hr = CreateTask(lpcwszTaskName,lpcwszApplicationName);//创建新的任务计划
hr = DeleteTask(lpcwszTaskName);//删除指定的任务计划
if (FAILED(hr))
{
wprintf(L"Failed calling ITask::Run, error = 0x%x/n",hr);
}
return 0;
}
//删除指定的计划任务
HRESULT DeleteTask(LPCWSTR lpcwszTaskName)
{
HRESULT hr = S_OK;
///
// Call CoInitialize to initialize the COM library and then
// CoCreateInstance to get the Task Scheduler object.
///
ITaskScheduler *pITS;
hr = CoInitialize(NULL);
if (SUCCEEDED(hr))
{
hr = CoCreateInstance(CLSID_CTaskScheduler,
NULL,
CLSCTX_INPROC_SERVER,
IID_ITaskScheduler,
(void **) &pITS);
if (FAILED(hr))
{
CoUninitialize();
return 1;
}
}
else
{
return 1;
}
//删除指定的任务计划
hr = pITS->Delete(lpcwszTaskName);
if (FAILED(hr))
{
//wprintf(L"Failed calling ITask::Run, error = 0x%x/n",hr);
CoUninitialize();
return hr;
}
CoUninitialize();
return S_OK;
}
HRESULT CreateTask(LPCWSTR lpcwszTaskName,LPCWSTR lpcwszApplicationName)
{
HRESULT hr = S_OK;
///
// Call CoInitialize to initialize the COM library and then
// CoCreateInstance to get the Task Scheduler object.
///
ITaskScheduler *pITS;
hr = CoInitialize(NULL);
if (SUCCEEDED(hr))
{
hr = CoCreateInstance(CLSID_CTaskScheduler,
NULL,
CLSCTX_INPROC_SERVER,
IID_ITaskScheduler,
(void **) &pITS);
if (FAILED(hr))
{
CoUninitialize();
return 1;
}
}
else
{
return 1;
}
//创建一个新的空白计划任务
ITask *pITask;
IPersistFile *pIPersistFile;
hr = pITS->NewWorkItem(lpcwszTaskName, // Name of task
CLSID_CTask, // Class identifier
IID_ITask, // Interface identifier
(IUnknown**)&pITask); // Address of task interface
if (FAILED(hr))
{
CoUninitialize();
return hr;
}
hr = pITask->QueryInterface(IID_IPersistFile,
(void **)&pIPersistFile);
pITask->Release();
if (FAILED(hr))
{
CoUninitialize();
return hr;
}
hr = pIPersistFile->Save(NULL,
TRUE);
pIPersistFile->Release();
if (FAILED(hr))
{
CoUninitialize();
return hr;
}
//为上面创建的任务计划指定应用程序路径
hr = pITS->Activate(lpcwszTaskName,
IID_ITask,
(IUnknown**) &pITask);
// Release the ITaskScheduler interface.
pITS->Release();
if (FAILED(hr))
{
CoUninitialize();
return hr;
}
hr = pITask->SetApplicationName(lpcwszApplicationName);
if (FAILED(hr))
{
CoUninitialize();
return hr;
}
hr = pITask->QueryInterface(IID_IPersistFile,
(void **)&pIPersistFile);
// Release the ITask interface.
pITask->Release();
hr = pIPersistFile->Save(NULL,
TRUE);
if (FAILED(hr))
{
CoUninitialize();
return hr;
}
// Release the IPersistFile interface.
pIPersistFile->Release();
CoUninitialize();
return hr;
}
#include <initguid.h>
#include <ole2.h>
#include <mstask.h>
#include <msterr.h>
#include <wchar.h>
#include<stdio.h>
//
//函数名称:DeleteTask
//函数功能:删除指定的任务计划
//参数说明:lpcwszTaskName:要删除的任务计划的名称
//
HRESULT DeleteTask(LPCWSTR lpcwszTaskName);
//
//函数名称:CreateTask
//函数功能:创建新的任务计划
//参数说明:lpcwszTaskName:要创建的任务计划的名称
// lpcwszApplicationName:要创建的任务计划的应用程序路径
//
HRESULT CreateTask(LPCWSTR lpcwszTaskName,LPCWSTR lpcwszApplicationName);
int main(int argc, char **argv)
{
HRESULT hr = S_OK;
LPCWSTR lpcwszTaskName = L"Test Task";
LPCWSTR lpcwszApplicationName = L"C://Windows//notepad.exe";
//hr = CreateTask(lpcwszTaskName,lpcwszApplicationName);//创建新的任务计划
hr = DeleteTask(lpcwszTaskName);//删除指定的任务计划
if (FAILED(hr))
{
wprintf(L"Failed calling ITask::Run, error = 0x%x/n",hr);
}
return 0;
}
//删除指定的计划任务
HRESULT DeleteTask(LPCWSTR lpcwszTaskName)
{
HRESULT hr = S_OK;
///
// Call CoInitialize to initialize the COM library and then
// CoCreateInstance to get the Task Scheduler object.
///
ITaskScheduler *pITS;
hr = CoInitialize(NULL);
if (SUCCEEDED(hr))
{
hr = CoCreateInstance(CLSID_CTaskScheduler,
NULL,
CLSCTX_INPROC_SERVER,
IID_ITaskScheduler,
(void **) &pITS);
if (FAILED(hr))
{
CoUninitialize();
return 1;
}
}
else
{
return 1;
}
//删除指定的任务计划
hr = pITS->Delete(lpcwszTaskName);
if (FAILED(hr))
{
//wprintf(L"Failed calling ITask::Run, error = 0x%x/n",hr);
CoUninitialize();
return hr;
}
CoUninitialize();
return S_OK;
}
HRESULT CreateTask(LPCWSTR lpcwszTaskName,LPCWSTR lpcwszApplicationName)
{
HRESULT hr = S_OK;
///
// Call CoInitialize to initialize the COM library and then
// CoCreateInstance to get the Task Scheduler object.
///
ITaskScheduler *pITS;
hr = CoInitialize(NULL);
if (SUCCEEDED(hr))
{
hr = CoCreateInstance(CLSID_CTaskScheduler,
NULL,
CLSCTX_INPROC_SERVER,
IID_ITaskScheduler,
(void **) &pITS);
if (FAILED(hr))
{
CoUninitialize();
return 1;
}
}
else
{
return 1;
}
//创建一个新的空白计划任务
ITask *pITask;
IPersistFile *pIPersistFile;
hr = pITS->NewWorkItem(lpcwszTaskName, // Name of task
CLSID_CTask, // Class identifier
IID_ITask, // Interface identifier
(IUnknown**)&pITask); // Address of task interface
if (FAILED(hr))
{
CoUninitialize();
return hr;
}
hr = pITask->QueryInterface(IID_IPersistFile,
(void **)&pIPersistFile);
pITask->Release();
if (FAILED(hr))
{
CoUninitialize();
return hr;
}
hr = pIPersistFile->Save(NULL,
TRUE);
pIPersistFile->Release();
if (FAILED(hr))
{
CoUninitialize();
return hr;
}
//为上面创建的任务计划指定应用程序路径
hr = pITS->Activate(lpcwszTaskName,
IID_ITask,
(IUnknown**) &pITask);
// Release the ITaskScheduler interface.
pITS->Release();
if (FAILED(hr))
{
CoUninitialize();
return hr;
}
hr = pITask->SetApplicationName(lpcwszApplicationName);
if (FAILED(hr))
{
CoUninitialize();
return hr;
}
hr = pITask->QueryInterface(IID_IPersistFile,
(void **)&pIPersistFile);
// Release the ITask interface.
pITask->Release();
hr = pIPersistFile->Save(NULL,
TRUE);
if (FAILED(hr))
{
CoUninitialize();
return hr;
}
// Release the IPersistFile interface.
pIPersistFile->Release();
CoUninitialize();
return hr;
}
//(windows7)上面代码(1.0版本的接口)在windows/tasks,目录建立。删除.job文件,并不会添加到控制面板的任务计划中,应该是还需要其他步骤(未找到完整例子)
下面的是MS的代码(2.0版本的接口)